Esempio n. 1
0
static void _tcp_accept(uv_stream_t *master, int status, bool tls)
{
	if (status != 0) {
		return;
	}
	uv_stream_t *client = handle_alloc(master->loop);
	if (!client) {
		return;
	}
	memset(client, 0, sizeof(*client));
	io_create(master->loop, (uv_handle_t *)client, SOCK_STREAM);
	if (uv_accept(master, client) != 0) {
		uv_close((uv_handle_t *)client, io_free);
		return;
	}

	/* Set deadlines for TCP connection and start reading.
	 * It will re-check every half of a request time limit if the connection
	 * is idle and should be terminated, this is an educated guess. */
	struct session *session = client->data;
	session->has_tls = tls;
	if (tls && !session->tls_ctx) {
		session->tls_ctx = tls_new(master->loop->data);
	}
	uv_timer_t *timer = &session->timeout;
	uv_timer_init(master->loop, timer);
	timer->data = client;
	uv_timer_start(timer, tcp_timeout_trigger, KR_CONN_RTT_MAX/2, KR_CONN_RTT_MAX/2);
	io_start_read((uv_handle_t *)client);
}
Esempio n. 2
0
static void on_read(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
    uv_handle_type pending) {
  int r;
  uv_buf_t outbuf;
  uv_err_t err;

  if (nread == 0) {
    /* Everything OK, but nothing read. */
    free(buf.base);
    return;
  }

  if (nread < 0) {
    err = uv_last_error(pipe->loop);
    if (err.code == UV_EOF) {
      free(buf.base);
      return;
    }

    printf("error recving on channel: %s\n", uv_strerror(err));
    abort();
  }

  fprintf(stderr, "got %d bytes\n", (int)nread);

  if (!tcp_server_listening) {
    ASSERT(nread > 0 && buf.base && pending != UV_UNKNOWN_HANDLE);
    read2_cb_called++;

    /* Accept the pending TCP server, and start listening on it. */
    ASSERT(pending == UV_TCP);
    r = uv_tcp_init(uv_default_loop(), &tcp_server);
    ASSERT(r == 0);

    r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server);
    ASSERT(r == 0);

    r = uv_listen((uv_stream_t*)&tcp_server, 12, on_connection);
    ASSERT(r == 0);

    tcp_server_listening = 1;

    /* Make sure that the expected data is correctly multiplexed. */
    ASSERT(memcmp("hello\n", buf.base, nread) == 0);

    outbuf = uv_buf_init("world\n", 6);
    r = uv_write(&write_req, (uv_stream_t*)pipe, &outbuf, 1, NULL);
    ASSERT(r == 0);

    /* Create a bunch of connections to get both servers to accept. */
    make_many_connections();
  } else if (memcmp("accepted_connection\n", buf.base, nread) == 0) {
    /* Remote server has accepted a connection.  Close the channel. */
    ASSERT(pending == UV_UNKNOWN_HANDLE);
    remote_conn_accepted = 1;
    uv_close((uv_handle_t*)&channel, NULL);
  }

  free(buf.base);
}
Esempio n. 3
0
int SocketAccept(uv_stream_t *const sstream, struct tls *const ssecure, SocketRef *const out) {
	SocketRef socket = calloc(1, sizeof(struct Socket));
	if(!socket) return UV_ENOMEM;
	int rc = uv_tcp_init(async_loop, socket->stream);
	if(rc < 0) goto cleanup;
	rc = uv_accept(sstream, (uv_stream_t *)socket->stream);
	if(rc < 0) goto cleanup;
	if(ssecure) {
		uv_os_fd_t fd;
		rc = uv_fileno((uv_handle_t *)socket->stream, &fd);
		if(rc < 0) goto cleanup;
		for(;;) {
			int event = tls_accept_socket(ssecure, &socket->secure, fd);
			if(0 == event) break;
			rc = tls_poll((uv_stream_t *)socket->stream, event);
			if(rc < 0) goto cleanup;
		}
	}
	socket->rdmem = NULL;
	*socket->rd = uv_buf_init(NULL, 0);
	*socket->wr = uv_buf_init(NULL, 0);
	*out = socket; socket = NULL;
cleanup:
	SocketFree(&socket);
	return rc;
}
Esempio n. 4
0
static void connection_cb(uv_stream_t *server, int status) {
  uv_tcp_t *client_handle = gpr_malloc(sizeof(uv_tcp_t));
  GPR_ASSERT(0 == status);
  GPR_ASSERT(0 == uv_tcp_init(uv_default_loop(), client_handle));
  GPR_ASSERT(0 == uv_accept(server, (uv_stream_t *)client_handle));
  uv_close((uv_handle_t *)client_handle, close_cb);
}
Esempio n. 5
0
static void onconnection(uv_stream_t *server, int status) {
  CHECK(status, "onconnection");

  int r = 0;
  uv_shutdown_t *shutdown_req;

  /* 4. Accept client connection */
  log_info("Accepting Connection");

  /* 4.1. Init client connection using `server->loop`, passing the client handle */
  uv_tcp_t *client = malloc(sizeof(uv_tcp_t));
  r = uv_tcp_init(server->loop, client);
  CHECK(r, "uv_tcp_init");

  /* 4.2. Accept the now initialized client connection */
  r = uv_accept(server, (uv_stream_t*) client);
  if (r) {
    log_error("trying to accept connection %d", r);

    shutdown_req = malloc(sizeof(uv_shutdown_t));
    r = uv_shutdown(shutdown_req, (uv_stream_t*) client, shutdown_cb);
    CHECK(r, "uv_shutdown");
  }

  /* 5. Start reading data from client */
  r = uv_read_start((uv_stream_t*) client, alloc_cb, read_cb);
  CHECK(r, "uv_read_start");
}
Esempio n. 6
0
File: server.c Progetto: torque/reki
static void Server_newTCPConnection( uv_stream_t *server, int status ) {
	if ( status < 0 ) {
		log_warn( "Connection error: %s", uv_err_name( status ) );
		return;
	}
	dbg_info( "Connection received." );

	ClientConnection *client = Client_new( );
	if ( !client ) goto badClient;

	client->handle.tcpHandle = malloc( sizeof(*client->handle.tcpHandle) );
	if ( !client->handle.tcpHandle ) goto badHandle;

	uv_tcp_init( server->loop, client->handle.tcpHandle );
	client->handle.tcpHandle->data = client;

	if ( uv_accept( server, client->handle.stream ) == 0 ) {
		client->server = server->data;
#if defined(CLIENTTIMEINFO)
		client->startTime = uv_now( server->loop );
#endif
		Client_handleConnection( client );
	} else {
		Client_terminate( client );
	}
	return;

badHandle:
	Client_free( client );
badClient:
	return;
}
Esempio n. 7
0
int oc_cluster_accept(oc_cluster_t *cluster, uv_stream_t* server) {
  uv_tcp_t *client;
  oc_worker_t *worker;
  int err;

  client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
  if (client == NULL) {
    // oom
    return -1;
  }

  uv_tcp_init(cluster->loop, client);

  err = uv_accept(server, (uv_stream_t*)client);
  if (err != 0) {
    err = -1;
    goto cleanup;
  }

  worker = pick_worker(cluster->workers, cluster->count);

  oc_worker_forward(worker, client);

  return 0;

cleanup:
  uv_close((uv_handle_t*)client, NULL);
  return err;
}
Esempio n. 8
0
static void
api_newconn (uv_stream_t *req, int status)
{
    Api *api = (Api *) req;
    int rc = 0;

    debug ("init client");
    rc = uv_pipe_init ((SERVER_FROM_API (api))->loop, &api->client, 0);
    if (rc < 0)
        goto error;

    debug ("accept client");
    rc = uv_accept ((uv_stream_t *) api, (uv_stream_t *) &api->client);
    if (rc < 0)
        goto error;

    debug ("listen client");
    rc = uv_read_start ((uv_stream_t *) &api->client, api_alloc_cb, api_cb);
    if (rc < 0)
        goto error;

    return;

error:
    debug ("closing client");
    uv_close ((uv_handle_t *) &api->client, NULL);
    if (rc)
        debug ("error: %s", uv_strerror (rc));
    return;
}
Esempio n. 9
0
static void do_accept(uv_req_t* req, int64_t skew, int status) {
  uv_handle_t* server;
  uv_handle_t* accepted_handle = (uv_handle_t*)malloc(sizeof *accepted_handle);
  int r;

  ASSERT(req != NULL);
  ASSERT(status == 0);
  ASSERT(accepted_handle != NULL);

  server = (uv_handle_t*)req->data;
  r = uv_accept(server, accepted_handle, close_cb, NULL);
  ASSERT(r == 0);

  do_accept_called++;

  /* Immediately close the accepted handle. */
  uv_close(accepted_handle);

  /* After accepting the two clients close the server handle */
  if (do_accept_called == 2) {
    uv_close(server);
  }

  free(req);
}
Esempio n. 10
0
/**
 * CB for new connection requests.
 */
void on_connection(uv_stream_t *server, int status)
{
  if (status < 0) {
    fprintf(stderr, "New connection error: %s\n", 
            uv_strerror(uv_last_error(server->loop)));
    return;
  }
  
  client_t *clnt = (client_t *)malloc(sizeof(client_t));
  client_init(clnt);
  uv_tcp_init(server->loop, &clnt->source);

  if (uv_accept(server, (uv_stream_t *)&clnt->source) != 0) {
    fprintf(stderr, "Accept failed: %s\n", 
            uv_strerror(uv_last_error(server->loop)));
    uv_close((uv_handle_t *)&clnt->source, NULL);
    free(clnt);
    return;
  }

  // succesfull accept here
  clnt->source.data = (void *)clnt;
  uv_read_start((uv_stream_t *)&clnt->source, alloc_buffer, on_read);

  // unref server to force program termination when client disconnects
  uv_unref((uv_handle_t *)server);
}
Esempio n. 11
0
/* _http_conn_new(): create http connection.
*/
static void
_http_conn_new(u3_http *htp_u)
{
  u3_hcon *hon_u = c3_malloc(sizeof(*hon_u));

  uv_tcp_init(u3L, &hon_u->wax_u);

  c3_w ret_w;
  ret_w = uv_accept((uv_stream_t*)&htp_u->wax_u,
                    (uv_stream_t*)&hon_u->wax_u);
  if (ret_w == UV_EOF)
  {
    uL(fprintf(uH, "http: accept: ERROR\n"));

    uv_close((uv_handle_t*)&hon_u->wax_u, _http_conn_free_early);
  }
  else {
    uv_read_start((uv_stream_t*)&hon_u->wax_u,
                  _http_alloc,
                  _http_conn_read_cb);

    hon_u->coq_l = htp_u->coq_l++;
    hon_u->seq_l = 1;

    hon_u->ruc_u = 0;
    hon_u->req_u = 0;
    hon_u->qer_u = 0;

    hon_u->htp_u = htp_u;
    hon_u->nex_u = htp_u->hon_u;
    htp_u->hon_u = hon_u;
  }
}
Esempio n. 12
0
int lua_uv_stream_accept(lua_State * L) {
	uv_stream_t * server = lua_generic_object<uv_stream_t>(L, 1);
	uv_stream_t * client = lua_generic_object<uv_stream_t>(L, 2);
	uv_accept(server, client);
	lua_uv_ok(L);
	return 0;
}
Esempio n. 13
0
static void connection_cb(uv_stream_t* s, int status) {
  uv_stream_t* stream;
  int r;

  ASSERT(server == s);
  ASSERT(status == 0);

  if (type == TCP) {
    stream = (uv_stream_t*)malloc(sizeof(uv_tcp_t));
    r = uv_tcp_init(loop, (uv_tcp_t*)stream);
    ASSERT(r == 0);
  } else {
    stream = (uv_stream_t*)malloc(sizeof(uv_pipe_t));
    r = uv_pipe_init(loop, (uv_pipe_t*)stream);
    ASSERT(r == 0);
  }

  r = uv_accept(s, stream);
  ASSERT(r == 0);

  r = uv_read_start(stream, buf_alloc, read_cb);
  ASSERT(r == 0);

  read_sockets++;
  max_read_sockets++;
}
Esempio n. 14
0
static void do_accept(uv_handle_t* timer_handle, int status) {
  uv_handle_t* server;
  uv_handle_t* accepted_handle = (uv_handle_t*)malloc(sizeof *accepted_handle);
  int r;

  ASSERT(timer_handle != NULL);
  ASSERT(status == 0);
  ASSERT(accepted_handle != NULL);

  server = (uv_handle_t*)timer_handle->data;
  r = uv_accept(server, accepted_handle, close_cb, NULL);
  ASSERT(r == 0);

  do_accept_called++;

  /* Immediately close the accepted handle. */
  r = uv_close(accepted_handle);
  ASSERT(r == 0);

  /* After accepting the two clients close the server handle */
  if (do_accept_called == 2) {
    r = uv_close(server);
    ASSERT(r == 0);
  }

  /* Dispose the timer. */
  r = uv_close(timer_handle);
  ASSERT(r == 0);
}
Esempio n. 15
0
void connection_cb(uv_stream_t *server, int status) {
  int r;
  if (status) {
    fprintf(stderr, "connection error %d", status);
    return;
  }

  // main diff between uv_tcp_t and uv_pipe_t is the ipc flag and the pipe_fname pointing to local sock file
  uv_pipe_t *client = (uv_pipe_t*) malloc(sizeof(uv_pipe_t));
  uv_pipe_init(loop, client, NOIPC);
  r = uv_accept(server, (uv_stream_t*) client);

  if (r == 0) {
    uv_write_t *write_req = (uv_write_t*) malloc(sizeof(uv_write_t));
    dummy_buf = uv_buf_init(".", 1);

    struct child_worker *worker = &workers[round_robin_counter];

    // Extended write function for sending handles over a pipe
    // https://github.com/thlorenz/libuv-dox/blob/master/methods.md#uv_write2
    uv_write2(write_req, (uv_stream_t*) &worker->pipe, &dummy_buf, 1 /*nbufs*/, (uv_stream_t*) client, NULL);

    round_robin_counter = (round_robin_counter + 1) % child_worker_count;
  } else {
    uv_close((uv_handle_t*) client, NULL);
  }
}
Esempio n. 16
0
static void _listen_cb(uv_stream_t* server, int status) {
    TRACE("got client connection...\n");
    luv_object_t* self = container_of(server, luv_object_t, h);
    if (luvL_object_is_waiting(self)) {
        ngx_queue_t* q = ngx_queue_head(&self->rouse);
        luv_state_t* s = ngx_queue_data(q, luv_state_t, cond);
        lua_State* L = s->L;

        TRACE("is waiting..., lua_State*: %p\n", L);
        luaL_checktype(L, 2, LUA_TUSERDATA);
        luv_object_t* conn = (luv_object_t*)lua_touserdata(L, 2);
        TRACE("got client conn: %p\n", conn);
        luvL_object_init(s, conn);
        int rv = uv_accept(&self->h.stream, &conn->h.stream);
        TRACE("accept returned ok\n");
        if (rv) {
            uv_err_t err = uv_last_error(self->h.stream.loop);
            TRACE("ERROR: %s\n", uv_strerror(err));
            lua_settop(L, 0);
            lua_pushnil(L);
            lua_pushstring(L, uv_strerror(err));
        }
        self->flags &= ~LUV_OWAITING;
        luvL_cond_signal(&self->rouse);
    }
    else {
        TRACE("increment backlog count\n");
        self->count++;
    }
}
Esempio n. 17
0
int TcpServer::Accept() {
  uv_tcp_t* handle = new uv_tcp_t;

  int result = uv_tcp_init( m_loop, handle );
  assert( result == 0 );

  result = uv_accept( reinterpret_cast<uv_stream_t*>( &m_server ), reinterpret_cast<uv_stream_t*>( handle ) );
  if ( result == -1 ) {
    uv_close( reinterpret_cast<uv_handle_t*>( handle ), TcpServer::OnCloseClient );
    return -1;
  }

  result = uv_read_start( reinterpret_cast<uv_stream_t*>( handle ), TcpServer::OnAllocBuffer, TcpServer::OnRead );
  if ( result == -1 ) {
    uv_close( reinterpret_cast<uv_handle_t*>( handle ), TcpServer::OnCloseClient );
    return -1;
  }

  Client* p_client = new Client { this, handle };
  handle->data = p_client;

  int id = s_next_client_id++;
  p_client->id = id;
  p_client->b_connected = true;

  m_clients.push_back( std::move( p_client ) );
  m_client_id_map.insert( std::move( std::make_pair( id, p_client ) ) );

  return id;
}
Esempio n. 18
0
static void ipc_on_connection_tcp_conn(uv_stream_t* server, int status) {
  int r;
  uv_buf_t buf;
  uv_tcp_t* conn;

  ASSERT(status == 0);
  ASSERT((uv_stream_t*)&tcp_server == server);

  conn = malloc(sizeof(*conn));
  ASSERT(conn);

  r = uv_tcp_init(server->loop, conn);
  ASSERT(r == 0);

  r = uv_accept(server, (uv_stream_t*)conn);
  ASSERT(r == 0);

  /* Send the accepted connection to the other process */
  buf = uv_buf_init("hello\n", 6);
  r = uv_write2(&conn_notify_req, (uv_stream_t*)&channel, &buf, 1,
    (uv_stream_t*)conn, NULL);
  ASSERT(r == 0);

  r = uv_read_start((uv_stream_t*) conn,
                    on_read_alloc,
                    on_tcp_child_process_read);
  ASSERT(r == 0);

  uv_close((uv_handle_t*)conn, close_cb);
}
Esempio n. 19
0
static MVMObject * socket_accept(MVMThreadContext *tc, MVMOSHandle *h) {
    MVMIOSyncSocketData *data = (MVMIOSyncSocketData *)h->body.data;

    while (!data->accept_server) {
        uv_ref((uv_handle_t *)data->ss.handle);
        uv_run(tc->loop, UV_RUN_DEFAULT);
    }

    /* Check the accept worked out. */
    if (data->accept_status < 0) {
        MVM_exception_throw_adhoc(tc, "Failed to listen: unknown error");
    }
    else {
        uv_tcp_t *client    = malloc(sizeof(uv_tcp_t));
        uv_stream_t *server = data->accept_server;
        int r;
        uv_tcp_init(tc->loop, client);
        data->accept_server = NULL;
        if ((r = uv_accept(server, (uv_stream_t *)client)) == 0) {
            MVMOSHandle         * const result = (MVMOSHandle *)MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTIO);
            MVMIOSyncSocketData * const data   = calloc(1, sizeof(MVMIOSyncSocketData));
            data->ss.handle   = (uv_stream_t *)client;
            data->ss.encoding = MVM_encoding_type_utf8;
            data->ss.sep      = '\n';
            result->body.ops  = &op_table;
            result->body.data = data;
            return (MVMObject *)result;
        }
        else {
            uv_close((uv_handle_t*)client, NULL);
            free(client);
            MVM_exception_throw_adhoc(tc, "Failed to accept: %s", uv_strerror(r));
        }
    }
}
Esempio n. 20
0
static PyObject *
Pipe_func_accept(Pipe *self, PyObject *args)
{
    int r;
    PyObject *client;

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "O:accept", &client)) {
        return NULL;
    }

    if (PyObject_IsSubclass((PyObject *)client->ob_type, (PyObject *)&StreamType)) {
        if (UV_HANDLE(client)->type != UV_TCP && UV_HANDLE(client)->type != UV_NAMED_PIPE) {
            PyErr_SetString(PyExc_TypeError, "Only TCP and Pipe objects are supported for accept");
            return NULL;
        }
    } else if (PyObject_IsSubclass((PyObject *)client->ob_type, (PyObject *)&UDPType)) {
        /* empty */
    } else {
        PyErr_SetString(PyExc_TypeError, "Only Stream and UDP objects are supported for accept");
        return NULL;
    }

    r = uv_accept((uv_stream_t *)UV_HANDLE(self), (uv_stream_t *)UV_HANDLE(client));
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_PipeError);
        return NULL;
    }

    Py_RETURN_NONE;
}
Esempio n. 21
0
File: tcp.c Progetto: eagles125/pyuv
static PyObject *
TCP_func_accept(TCP *self, PyObject *args)
{
    int r;
    PyObject *client;

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "O:accept", &client)) {
        return NULL;
    }

    if (!PyObject_IsSubclass((PyObject *)client->ob_type, (PyObject *)&StreamType)) {
        PyErr_SetString(PyExc_TypeError, "Only stream objects are supported for accept");
        return NULL;
    }

    r = uv_accept((uv_stream_t *)&self->tcp_h, (uv_stream_t *)UV_HANDLE(client));
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TCPError);
        return NULL;
    }

    Py_RETURN_NONE;
}
Esempio n. 22
0
void
source_accept_cb(uv_stream_t *server, int status) {
    struct source_context *source = new_source();
    struct target_context *target = new_target();

    source->target = target;
    target->source = source;

    uv_tcp_init(server->loop, &source->handle.tcp);
    uv_tcp_init(server->loop, &target->handle.tcp);

    uv_tcp_nodelay(&source->handle.tcp, 0);
    uv_tcp_nodelay(&target->handle.tcp, 0);
    uv_tcp_keepalive(&source->handle.tcp, 1, 60);
    uv_tcp_keepalive(&target->handle.tcp, 1, 60);

    int rc = uv_accept(server, &source->handle.stream);
    if (rc == 0) {
        connect_to_target(target);
    } else {
        logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc));
        close_source(source);
        close_target(target);
    }
}
Esempio n. 23
0
static void do_accept(uv_timer_t* timer_handle, int status) {
  uv_tcp_t* server;
  uv_tcp_t* accepted_handle = (uv_tcp_t*)malloc(sizeof *accepted_handle);
  int r;

  ASSERT(timer_handle != NULL);
  ASSERT(status == 0);
  ASSERT(accepted_handle != NULL);

  r = uv_tcp_init(uv_default_loop(), accepted_handle);
  ASSERT(r == 0);

  server = (uv_tcp_t*)timer_handle->data;
  r = uv_accept((uv_stream_t*)server, (uv_stream_t*)accepted_handle);
  ASSERT(r == 0);

  do_accept_called++;

  /* Immediately close the accepted handle. */
  uv_close((uv_handle_t*)accepted_handle, close_cb);

  /* After accepting the two clients close the server handle */
  if (do_accept_called == 2) {
    uv_close((uv_handle_t*)server, close_cb);
  }

  /* Dispose the timer. */
  uv_close((uv_handle_t*)timer_handle, close_cb);
}
Esempio n. 24
0
static void read2_cb(uv_pipe_t* handle,
                     ssize_t nread,
                     const uv_buf_t* rdbuf,
                     uv_handle_type pending) {
  uv_buf_t wrbuf;
  int r;

  ASSERT(pending == UV_NAMED_PIPE || pending == UV_TCP);
  ASSERT(handle == &ctx.channel);
  ASSERT(nread >= 0);

  wrbuf = uv_buf_init(".", 1);

  if (pending == UV_NAMED_PIPE)
    r = uv_pipe_init(ctx.channel.loop, &ctx.recv.pipe, 0);
  else if (pending == UV_TCP)
    r = uv_tcp_init(ctx.channel.loop, &ctx.recv.tcp);
  else
    abort();
  ASSERT(r == 0);

  r = uv_accept((uv_stream_t*)handle, &ctx.recv.stream);
  ASSERT(r == 0);

  r = uv_write2(&ctx.write_req,
                (uv_stream_t*)&ctx.channel,
                &wrbuf,
                1,
                &ctx.recv.stream,
                write2_cb);
  ASSERT(r == 0);
}
Esempio n. 25
0
File: tcp.c Progetto: dvv/libuv-test
static void server_on_connection(uv_stream_t *handle, int status)
{
  server_t *server = handle->data;

  // allocate client
  client_t *client = client_alloc(server->sizeof_client);

  // setup client
  client->server = server;

  // accept client
  // TODO: report errors
  uv_tcp_init(server->handle.loop, &client->handle);
  client->handle.data = client;
  // TODO: EMFILE trick!
  // https://github.com/joyent/libuv/blob/master/src/unix/ev/ev.3#L1812-1816
  if (uv_accept((uv_stream_t *)&server->handle, (uv_stream_t *)&client->handle)) {
    // accept failed? report error
    client->server->on_event(server, EVT_ERROR, uv_last_error(uv_default_loop()).code, NULL);
    exit(-2);
  }

  // fire 'open' event
  client->server->on_event(client, EVT_CLI_OPEN, 0, NULL);

  // start reading client
  uv_read_start((uv_stream_t *)&client->handle, buf_alloc, client_on_read);
}
Esempio n. 26
0
static void recv_cb(uv_pipe_t* handle,
                    ssize_t nread,
                    const uv_buf_t* buf,
                    uv_handle_type pending) {
  int r;

  ASSERT(pending == ctx.expected_type);
  ASSERT(handle == &ctx.channel);
  ASSERT(nread >= 0);

  if (pending == UV_NAMED_PIPE)
    r = uv_pipe_init(ctx.channel.loop, &ctx.recv.pipe, 0);
  else if (pending == UV_TCP)
    r = uv_tcp_init(ctx.channel.loop, &ctx.recv.tcp);
  else
    abort();
  ASSERT(r == 0);

  r = uv_accept((uv_stream_t*)&ctx.channel, &ctx.recv.stream);
  ASSERT(r == 0);

  uv_close((uv_handle_t*)&ctx.channel, NULL);
  uv_close(&ctx.send.handle, NULL);
  uv_close(&ctx.recv.handle, NULL);
  num_recv_handles++;
}
Esempio n. 27
0
void
client_accept_cb(uv_stream_t *server, int status) {
    struct client_context *client = new_client();
    struct remote_context *remote = new_remote(idle_timeout);

    client->remote = remote;
    remote->client = client;

    uv_timer_init(server->loop, remote->timer);

    uv_tcp_init(server->loop, &client->handle.tcp);
    uv_tcp_init(server->loop, &remote->handle.tcp);


    int rc = uv_accept(server, &client->handle.stream);
    if (rc == 0) {
        int namelen = sizeof client->addr;
        uv_tcp_getpeername(&client->handle.tcp, &client->addr, &namelen);
        reset_timer(remote); // start timer
        connect_to_remote(remote);
    } else {
        logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc));
        close_client(client);
        close_remote(remote);
    }
}
Esempio n. 28
0
static void connect_cb(uv_stream_t* listener, int status)
{
	int n;

	if (status) {
		SHOW_UV_ERROR(listener->loop);
		return;
	}

	server_ctx *ctx = calloc(1, sizeof(server_ctx));
	ctx->client.data = ctx;
	ctx->remote.data = ctx;

	ctx->handshake_buffer = calloc(1, HANDSHAKE_BUFFER_SIZE);
	if (!ctx || !ctx->handshake_buffer)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_tcp_init(listener->loop, &ctx->client);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_accept(listener, (uv_stream_t *)(void *)&ctx->client);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_tcp_nodelay(&ctx->client, 1);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_read_start((uv_stream_t *)(void *)&ctx->client, client_handshake_alloc_cb, client_handshake_read_cb);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);
}
Esempio n. 29
0
static void ipc_on_connection(uv_stream_t* server, int status) {
  int r;
  uv_buf_t buf;
  uv_tcp_t* conn;

  if (!connection_accepted) {
    /* 
     * Accept the connection and close it.  Also let the other
     * side know.
     */
    ASSERT(status == 0);
    ASSERT((uv_stream_t*)&tcp_server == server);

    conn = malloc(sizeof(*conn));
    ASSERT(conn);

    r = uv_tcp_init(server->loop, conn);
    ASSERT(r == 0);

    r = uv_accept(server, (uv_stream_t*)conn);
    ASSERT(r == 0);

    uv_close((uv_handle_t*)conn, close_conn_cb);

    buf = uv_buf_init("accepted_connection\n", 20);
    r = uv_write2(&conn_notify_req, (uv_stream_t*)&channel, &buf, 1,
      NULL, conn_notify_write_cb);
    ASSERT(r == 0);

    connection_accepted = 1;
  }
}
Esempio n. 30
0
static void cb_connection(uv_stream_t *stream, int status)
{
	ws_server_t *ptr = (ws_server_t*)stream->data;

	ws_connect_t *pConnect = NULL;

    ptr->cb.cb_malloc(pConnect, ptr->cb.obj_malloc);
    
    if (NULL == pConnect)
    {
        printf("->strerror:malloc connect error,(%s,%d)\n", __FILE__, __LINE__);
        return;
    }
    
	if(0 == uv_accept((uv_stream_t *)(&ptr->server), (uv_stream_t*)(&pConnect->connect)))
	{
        pConnect->state = WS_CONNECTING;
		on_connect(pConnect);
	}
    else
    {
        if (NULL != ptr->cb.cb_connection)
        {
            ptr->cb.cb_connection(pConnect, ptr->cb.obj_connection, WS_ERROR_BAD);
        }
        else
        {
            printf("->strerror:accept error,(%s,%d)\n", __FILE__, __LINE__);
        }
	}
}