예제 #1
0
파일: test.c 프로젝트: jcaose/evcom
int
pingpong (struct sockaddr *address)
{
  int r;
  evcom_stream client;

  successful_ping_count = 0;
  nconnections = 0;
  got_server_close = 0;

  evcom_server_init(&server);
  server.on_connection = pingpong_on_server_connection;
  server.on_close = common_on_server_close;

  r = evcom_server_listen(&server, address, 10);
  assert(r == 0);
  evcom_server_attach(EV_DEFAULT_ &server);

  evcom_stream_init(&client);
  client.on_read    = pingpong_on_client_read;
  client.on_connect = pingpong_on_client_connect;
  client.on_close   = pingpong_on_client_close;
  client.on_timeout = common_on_client_timeout;
  evcom_stream_reset_timeout(&client, PINGPONG_TIMEOUT);

  assert(EVCOM_INITIALIZED == evcom_stream_state(&client));

#if EVCOM_HAVE_GNUTLS
  if (use_tls) anon_tls_client(&client);
#endif

  r = evcom_stream_connect(&client, address);
  assert(r == 0 && "problem connecting");
  evcom_stream_attach(EV_DEFAULT_ &client);

  ev_loop(EV_DEFAULT_ 0);

  printf("successful_ping_count = %d\n", successful_ping_count);
  assert(successful_ping_count == EXCHANGES + 1);
  assert(nconnections == 1);
  assert(got_server_close);

  return 0;
}
예제 #2
0
파일: evcom.c 프로젝트: bernd/node
static void 
stream_event (EV_P_ ev_io *w, int revents)
{
  evcom_stream *stream = w->data;

  if (revents & EV_READ) {
    while (stream->recv_action) {
      int r = stream->recv_action(stream);
      if (r == AGAIN) break;
    }
  }

  if (revents & EV_WRITE) {
    while (stream->send_action) {
      int r = stream->send_action(stream);
      if (r == AGAIN) break;
    }
  }

  if (stream->send_action == NULL) {
    ev_io_stop(EV_A_ &stream->write_watcher);
  }

  if (stream->recv_action == NULL) {
    ev_io_stop(EV_A_ &stream->read_watcher);
  }

  if (stream->sendfd < 0 && stream->recvfd < 0) {
    ev_timer_stop(EV_A_ &stream->timeout_watcher);

    if (stream->server && (stream->server->flags & EVCOM_TOO_MANY_CONN)) {
#if EV_MULTIPLICITY
      struct ev_loop *loop = stream->server->loop;
#endif
      stream->server->flags &= ~EVCOM_TOO_MANY_CONN;
      evcom_server_attach(EV_A_ stream->server);
    }
    too_many_connections = 0;

    if (stream->on_close) stream->on_close(stream);
  }
}
예제 #3
0
파일: test.c 프로젝트: jcaose/evcom
int
zero_stream (struct sockaddr *address, size_t to_write)
{
  int r;

  assert(to_write >= 1024); // should be kind of big at least.
  zero_to_write = to_write;
  got_server_close = 0;
  zero_written = 0;
  zero_read = 0;
  zero_client_closed = 0;

  evcom_server_init(&server);
  server.on_connection = make_echo_connection;
  server.on_close      = common_on_server_close;

  evcom_server_listen(&server, address, 1000);
  evcom_server_attach(EV_DEFAULT_ &server);

  evcom_stream client;
  evcom_stream_init(&client);
  client.on_read    = zero_recv;
  client.on_connect = zero_start;
  client.on_close   = zero_close;
  client.on_timeout = error_out;
  evcom_stream_reset_timeout(&client, ZERO_TIMEOUT);
#if EVCOM_HAVE_GNUTLS
  if (use_tls) anon_tls_client(&client);
#endif
  r = evcom_stream_connect(&client, address);
  assert(r == 0 && "problem connecting");
  evcom_stream_attach(EV_DEFAULT_ &client);

  ev_loop(EV_DEFAULT_ 0);

  assert(got_server_close);
  assert(zero_written == zero_to_write);
  assert(zero_read == zero_to_write);
  assert(zero_client_closed) ;

  return 0;
}
예제 #4
0
파일: test.c 프로젝트: jcaose/evcom
int
connint (struct sockaddr *address)
{
  int r;

  nconnections = 0;
  got_server_close = 0;

  evcom_server_init(&server);
  server.on_connection = connint_on_connection;
  server.on_close = common_on_server_close;

  evcom_server_listen(&server, address, 1000);
  evcom_server_attach(EV_DEFAULT_ &server);

  evcom_stream clients[NCONN];
  int i;
  for (i = 0; i < NCONN; i++) {
    evcom_stream *client = &clients[i];
    evcom_stream_init(client);
    client->on_read    = connint_on_client_read;
    client->on_connect = connint_on_client_connect;
    client->on_close   = connint_on_client_close;
    client->on_timeout = common_on_client_timeout;
    evcom_stream_reset_timeout(client, CONNINT_TIMEOUT);
#if EVCOM_HAVE_GNUTLS
    if (use_tls) anon_tls_client(client);
#endif
    r = evcom_stream_connect(client, address);
    assert(r == 0 && "problem connecting");
    evcom_stream_attach(EV_DEFAULT_ client);
  }

  ev_loop(EV_DEFAULT_ 0);

  assert(nconnections == NCONN);
  assert(got_server_close);

  return 0;
}