void TcpConnection::Setup(Listener* listener, struct sockaddr_storage* localAddr, const std::string &localIP, uint16_t localPort) { MS_TRACE(); int err; // Set the UV handle. err = uv_tcp_init(DepLibUV::GetLoop(), this->uvHandle); if (err) { delete this->uvHandle; this->uvHandle = nullptr; MS_THROW_ERROR("uv_tcp_init() failed: %s", uv_strerror(err)); } // Set the listener. this->listener = listener; // Set the local address. this->localAddr = localAddr; this->localIP = localIP; this->localPort = localPort; }
/* same ping-pong test, but using IPv6 connection */ static void tcp_pinger_v6_new() { int r; struct sockaddr_in6 server_addr = uv_ip6_addr("::1", TEST_PORT); pinger_t *pinger; pinger = (pinger_t*)malloc(sizeof(*pinger)); pinger->state = 0; pinger->pongs = 0; /* Try to connec to the server and do NUM_PINGS ping-pongs. */ r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp); pinger->stream.tcp.data = pinger; ASSERT(!r); /* We are never doing multiple reads/connects at a time anyway. */ /* so these handles can be pre-initialized. */ r = uv_tcp_connect6(&pinger->connect_req, &pinger->stream.tcp, server_addr, pinger_on_connect); ASSERT(!r); /* Synchronous connect callbacks are not allowed. */ ASSERT(pinger_on_connect_count == 0); }
void on_new_connection (uv_stream_t *server, int status) { struct clt *clt = server->data; if (status == -1) { // error! return; } /* Each new connection: Start up a * peer object for this connection. */ peer_t *peer = malloc (sizeof (peer_t)); uv_tcp_init (loop, &peer->tcpsock); if (uv_accept (server, (uv_stream_t*) &peer->tcpsock) == 0) { struct sockaddr_in sin; uv_ip4_addr (remaddr, remport, &sin); peer_start (peer, (struct sockaddr_in *)&sin, clt->sel, 1); peer_open (peer); } else { uv_close ((uv_handle_t*) &peer->tcpsock, NULL); free (peer); } }
static void pinger_new() { int r; struct sockaddr_in client_addr = uv_ip4_addr("0.0.0.0", 0); struct sockaddr_in server_addr = uv_ip4_addr("127.0.0.1", TEST_PORT); pinger_t *pinger; pinger = (pinger_t*)malloc(sizeof(*pinger)); pinger->state = 0; pinger->pongs = 0; /* Try to connec to the server and do NUM_PINGS ping-pongs. */ r = uv_tcp_init(&pinger->tcp, pinger_close_cb, (void*)pinger); ASSERT(!r); /* We are never doing multiple reads/connects at a time anyway. */ /* so these handles can be pre-initialized. */ uv_req_init(&pinger->connect_req, (uv_handle_t*)&pinger->tcp, pinger_connect_cb); uv_bind(&pinger->tcp, client_addr); r = uv_connect(&pinger->connect_req, server_addr); ASSERT(!r); }
static void on_connection(uv_stream_t* server, int status) { dnshandle* handle; int r; ASSERT(status == 0); handle = (dnshandle*) malloc(sizeof *handle); ASSERT(handle != NULL); /* initialize read buffer state */ handle->state.prevbuf_ptr = 0; handle->state.prevbuf_pos = 0; handle->state.prevbuf_rem = 0; r = uv_tcp_init(loop, (uv_tcp_t*)handle); ASSERT(r == 0); r = uv_accept(server, (uv_stream_t*)handle); ASSERT(r == 0); r = uv_read_start((uv_stream_t*)handle, buf_alloc, after_read); ASSERT(r == 0); }
static void make_many_connections(void) { tcp_conn* conn; struct sockaddr_in addr; int r, i; for (i = 0; i < CONN_COUNT; i++) { conn = malloc(sizeof(*conn)); ASSERT(conn); r = uv_tcp_init(uv_default_loop(), &conn->conn); ASSERT(r == 0); ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); r = uv_tcp_connect(&conn->conn_req, (uv_tcp_t*) &conn->conn, (const struct sockaddr*) &addr, connect_cb); ASSERT(r == 0); conn->conn.data = conn; } }
static void on_connection(uv_handle_t* server, int status) { uv_tcp_t* handle; int r; if (status != 0) { fprintf(stderr, "Connect error %d\n", uv_last_error()); } ASSERT(status == 0); handle = (uv_tcp_t*) malloc(sizeof *handle); ASSERT(handle != NULL); uv_tcp_init(handle); /* associate server with stream */ handle->data = server; r = uv_accept(server, (uv_stream_t*)handle); ASSERT(r == 0); r = uv_read_start((uv_stream_t*)handle, echo_alloc, after_read); ASSERT(r == 0); }
int main() { int r; id = 0; loop = uv_default_loop(); struct sockaddr_in bind_addr; r = uv_ip4_addr("0.0.0.0", PORT, &bind_addr); CHECK(r, "get bind addr"); r = uv_tcp_init(loop, &server); CHECK(r, "init server"); r = uv_tcp_bind(&server, (const struct sockaddr*) &bind_addr); CHECK(r, "bind"); r = uv_listen((uv_stream_t*) &server, BACKLOG, on_connect); CHECK(r, "listen"); log_info("listening on http://localhost:%d", PORT); uv_run(loop, UV_RUN_DEFAULT); return 0; }
int main() { uv_loop_t *loop = uv_default_loop(); int port = 8000, r = 0; evt_ctx_t ctx; struct sockaddr_in bind_local; evt_ctx_init_ex(&ctx, "server-cert.pem", "server-key.pem"); evt_ctx_set_nio(&ctx, NULL, uv_tls_writer); uv_tcp_t listener_local; uv_tcp_init(loop, &listener_local); listener_local.data = &ctx; uv_ip4_addr("127.0.0.1", port, &bind_local); if ((r = uv_tcp_bind(&listener_local, (struct sockaddr*)&bind_local, 0))) fprintf( stderr, "bind: %s\n", uv_strerror(r)); if ((r = uv_listen((uv_stream_t*)&listener_local, 128, on_connect_cb))) fprintf( stderr, "listen: %s\n", uv_strerror(r)); printf("Listening on %d\n", port); uv_run(loop, UV_RUN_DEFAULT); evt_ctx_free(&ctx); return 0; }
void TCPServer::IncomingConnection(uv_stream_t * listener, int status) { LogTrace(LOG_NETWORKING, "Incoming connection"); if (status < 0) { LogError(LOG_NETWORKING, "Incoming connection - failure, reason: %s", uv_err_name(status)); return; } uv_tcp_t * client = (uv_tcp_t *) malloc(sizeof(uv_tcp_t)); uv_tcp_init(uv_default_loop(), client); if (uv_accept(listener, (uv_stream_t *) client) == 0) { LogTrace(LOG_NETWORKING, "Incoming connection - success"); uv_read_start((uv_stream_t *) client, AllocBuffer, IncomingData); } else { LogError(LOG_NETWORKING, "Incoming connection - failure, reason: %s", uv_err_name(status)); uv_close((uv_handle_t *) client, NULL); } }
int main() { int r = 0; uv_loop_t *loop = uv_default_loop(); r = uv_tcp_init(loop, &tcp_server); CHECK(r, "uv_tcp_init"); struct sockaddr_in addr; r = uv_ip4_addr(HOST, PORT, &addr); CHECK(r, "uv_ip4_addr"); r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0); CHECK(r, "uv_tcp_bind"); r = uv_listen((uv_stream_t*) &tcp_server, SOMAXCONN, onconnection); CHECK(r, "uv_listen"); log_info("Listening on %s:%d", HOST, PORT); uv_run(loop, UV_RUN_DEFAULT); MAKE_VALGRIND_HAPPY(); return 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); uint64_t tcpcnt; int r; ASSERT(timer_handle != NULL); ASSERT(status == 0); ASSERT(accepted_handle != NULL); uv_tcp_init(accepted_handle); /* Test to that uv_counters()->tcp_init does not increase across the uv_accept. */ tcpcnt = uv_counters()->tcp_init; server = (uv_tcp_t*)timer_handle->data; r = uv_accept((uv_handle_t*)server, (uv_stream_t*)accepted_handle); ASSERT(r == 0); ASSERT(uv_counters()->tcp_init == tcpcnt); do_accept_called++; /* Immediately close the accepted handle. */ r = uv_close((uv_handle_t*)accepted_handle, close_cb); ASSERT(r == 0); /* After accepting the two clients close the server handle */ if (do_accept_called == 2) { r = uv_close((uv_handle_t*)server, close_cb); ASSERT(r == 0); } /* Dispose the timer. */ r = uv_close((uv_handle_t*)timer_handle, close_cb); ASSERT(r == 0); }
int tcp_client_connect(abstract_tcp_client_t *client, char *ip, int32_t port, tcp_client_connect_cb connect_cb) { int r = 0; char *local_ip; client_connect_req_t *req = (client_connect_req_t*) malloc(sizeof(client_connect_req_t)); struct sockaddr_in addr; r = uv_tcp_init(client->loop, &client->socket); local_ip = smts_get_one_addrs(); if (local_ip == NULL) { r = NET_ADDR_NOT_FOUND; CL_ERROR("get local addr error:%d,%s.\n", r, smts_strerror(r)); return r; } r = uv_ip4_addr(local_ip, 0, &addr); if (r != 0) { CL_ERROR("uv_ip4_addr error\n"); return r; } r = uv_tcp_bind(&client->socket, (struct sockaddr *) &addr, 0); if (r != 0) { CL_ERROR("bind ip:%s error:%d,%s\n", ip, r, smts_strerror(r)); return r; } CL_DEBUG("bind local addr:%s .\n", local_ip); r = uv_ip4_addr(ip, port, &client->addr); if (r != 0) { CL_ERROR("uv_ip4_addr error\n"); return r; } req->client = client; req->req.data = req; req->cb = connect_cb; r = uv_tcp_connect(&req->req, &client->socket, (const struct sockaddr*) &client->addr, on_tcp_client_connect_cb); return r; }
uv_tcp_t* createServer(uv_loop_t* pLoop, const std::string& host, int port, WebApplication* pWebApplication) { // Deletes itself when destroy() is called, which occurs in freeServer() Socket* pSocket = new Socket(); // TODO: Handle error uv_tcp_init(pLoop, &pSocket->handle); pSocket->handle.data = pSocket; pSocket->pWebApplication = pWebApplication; struct sockaddr_in address = uv_ip4_addr(host.c_str(), port); int r = uv_tcp_bind(&pSocket->handle, address); if (r) { pSocket->destroy(); return NULL; } r = uv_listen((uv_stream_t*)&pSocket->handle, 128, &on_request); if (r) { pSocket->destroy(); return NULL; } return &pSocket->handle; }
int spawn_tcp_server_helper(void) { uv_tcp_t tcp; uv_os_sock_t handle; int r; r = uv_tcp_init(uv_default_loop(), &tcp); ASSERT(r == 0); #ifdef _WIN32 handle = _get_osfhandle(3); #else handle = 3; #endif r = uv_tcp_open(&tcp, handle); ASSERT(r == 0); /* Make sure that we can listen on a socket that was * passed down from the parent process */ r = uv_listen((uv_stream_t*)&tcp, SOMAXCONN, NULL); ASSERT(r == 0); return 1; }
void on_connect (uv_stream_t* server_handle, int status) { CHECK (status, "connect"); int r; assert ( (uv_tcp_t*) server_handle == &server); client_t* client = malloc (sizeof (client_t) ); client->request_num = request_num; LOGF ("[ %5d ] new connection", request_num++); uv_tcp_init (uv_loop, &client->handle); http_parser_init (&client->parser, HTTP_REQUEST); client->parser.data = client; client->handle.data = client; r = uv_accept (server_handle, (uv_stream_t*) &client->handle); CHECK (r, "accept"); uv_read_start ( (uv_stream_t*) &client->handle, on_alloc, on_read); }
void on_connected(uv_stream_t *s, int status) { assert(s == (uv_stream_t *) &server); assert(status == 0); client_t *client = malloc(sizeof(client_t)); int r = uv_tcp_init(s->loop, &client->tcp); r = uv_accept((uv_stream_t *) &server, (uv_stream_t *) &client->tcp); if (r != 0) { fprintf(stderr, "accept: %s\n", uv_strerror(r)); return; } client->tcp.data = client; client->parser.data = client; http_parser_init(&client->parser, HTTP_REQUEST); uv_read_start((uv_stream_t *) &client->tcp, on_alloc, on_read); }
bool TCPClient::init() { if (!isclosed_) { return true; } int iret = uv_async_init(&loop_, &async_handle_, AsyncCB); if (iret) { errmsg_ = GetUVError(iret); LOG(ERROR)<<(errmsg_); return false; } async_handle_.data = this; iret = uv_tcp_init(&loop_, &client_handle_->tcphandle); if (iret) { errmsg_ = GetUVError(iret); LOG(ERROR)<<(errmsg_); return false; } client_handle_->tcphandle.data = client_handle_; client_handle_->parent_server = this; client_handle_->packet_->SetPacketCB(GetPacket, client_handle_); client_handle_->packet_->Start(PACKET_HEAD, PACKET_TAIL); iret = uv_timer_init(&loop_, &reconnect_timer_); if (iret) { errmsg_ = GetUVError(iret); LOG(ERROR)<<(errmsg_); return false; } reconnect_timer_.data = this; LOG(INFO) << "client(" << this << ")Init"; isclosed_ = false; return true; }
void on_new_connection(uv_stream_t *stream, int status) { struct tcp_client *client; int r; if (status != 0) { // TODO(lijie3): ignore this error ? fprintf(stderr, "create new client connection failed!\n"); return; } client = (struct tcp_client *)malloc(sizeof(struct tcp_client)); r = uv_tcp_init(&loop, &client->handle); assert(r == 0); uv_tcp_nodelay(&client->handle, 1); r = uv_accept(stream, (uv_stream_t *)&client->handle); assert(r == 0); client->recvbuf = (char *)malloc(kMaxRecvBufferSize); client->rdpos = 0; r = uv_read_start((uv_stream_t *)&client->handle, on_alloc, on_read); assert(r == 0); }
static void connection_cb(uv_handle_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)); uv_tcp_init((uv_tcp_t*)stream); } else { stream = (uv_stream_t*)malloc(sizeof(uv_pipe_t)); uv_pipe_init((uv_pipe_t*)stream); } 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++; }
static int TCP_tp_init(TCP *self, PyObject *args, PyObject *kwargs) { int r; Loop *loop; UNUSED_ARG(kwargs); RAISE_IF_HANDLE_INITIALIZED(self, -1); if (!PyArg_ParseTuple(args, "O!:__init__", &LoopType, &loop)) { return -1; } r = uv_tcp_init(loop->uv_loop, &self->tcp_h); if (r != 0) { RAISE_UV_EXCEPTION(loop->uv_loop, PyExc_TCPError); return -1; } initialize_handle(HANDLE(self), loop); return 0; }
int main(void) { uv_loop_t loop; struct sockaddr_in addr; uv_connect_t connect_req; int fd, r; /* loop init */ uv_loop_init(&loop); #if USE_PIPE uv_pipe_init(&loop, &client, 1); uv_pipe_connect(&connect_req, &client, "/var/tmp/pipe.server1", connect_cb); #else /*tcp socket initial */ uv_ip4_addr("127.0.0.1", 4789, &addr); uv_tcp_init(&loop, &client); uv_tcp_connect(&connect_req, &client, (const struct sockaddr *)&addr, connect_cb); #endif r = uv_tty_init(&loop, &tty, 0, 1); printf("r=%d\n", r); uv_read_start((uv_stream_t *) & tty, alloc_cb, recv_cb); uv_run(&loop, UV_RUN_DEFAULT); printf("end\n"); return 0; }
int uvm_start_client(uvm_read_callback_t cb, char* host, int port, void* data){ struct sockaddr_in sin; uv_tcp_t* sock = malloc(sizeof(uv_tcp_t)); uv_connect_t* connect_req = malloc(sizeof(uv_connect_t)); connection_request_t* req = malloc(sizeof(connection_request_t)); if(!req){ /* FIXME: set error here */ return -1; } connect_req->data = req; req->cb = cb; req->data = data; /* Resolve name */ sin = uv_ip4_addr(host,port); /* Init connection state */ uv_tcp_init(uv_default_loop(), sock); /* Enqueue connection request */ return uv_tcp_connect(connect_req, sock, sin, cb_connect); }
static void on_connection(uv_stream_t* server, int status) { uv_stream_t* stream; int r; if (status != 0) { fprintf(stderr, "Connect error %d\n", uv_last_error().code); } ASSERT(status == 0); switch (serverType) { case TCP: stream = malloc(sizeof(uv_tcp_t)); ASSERT(stream != NULL); uv_tcp_init((uv_tcp_t*)stream); break; case PIPE: stream = malloc(sizeof(uv_pipe_t)); ASSERT(stream != NULL); uv_pipe_init((uv_pipe_t*)stream); break; default: ASSERT(0 && "Bad serverType"); abort(); } /* associate server with stream */ stream->data = server; r = uv_accept(server, stream); ASSERT(r == 0); r = uv_read_start(stream, echo_alloc, after_read); ASSERT(r == 0); }
void connection_fail(uv_connect_cb connect_cb) { struct sockaddr_in client_addr, server_addr; int r; client_addr = uv_ip4_addr("0.0.0.0", 0); /* There should be no servers listening on this port. */ server_addr = uv_ip4_addr("127.0.0.1", TEST_PORT); /* Try to connec to the server and do NUM_PINGS ping-pongs. */ r = uv_tcp_init(uv_default_loop(), &tcp); ASSERT(!r); /* We are never doing multiple reads/connects at a time anyway. */ /* so these handles can be pre-initialized. */ uv_tcp_bind(&tcp, client_addr); r = uv_tcp_connect(&req, &tcp, server_addr, connect_cb); ASSERT(!r); uv_run(uv_default_loop()); ASSERT(connect_cb_calls == 1); ASSERT(close_cb_calls == 1); }
int main(int argc, char *argv[]){ sockaddr_in addr; int r; resbuf.base = RESPONSE; resbuf.len = sizeof(RESPONSE); uv_loop = uv_default_loop(); addr = uv_ip4_addr("127.0.0.1", 8089); r = uv_tcp_init(uv_loop, &tcp_server); assert(r == 0); r = uv_tcp_bind(&tcp_server, addr); assert(r == 0); r = uv_listen((uv_stream_t*)&tcp_server, 128, connection_cb); assert(r == 0); printf("start\n"); r = uv_run(uv_loop); assert(0 && "Blackhole server dropped out of event loop."); }
static void onconnection(uv_stream_t *server, int status) { CHECK(status, "onconnection"); int r = 0; uv_shutdown_t *shutdown_req; log_info("Accepting Connection"); uv_tcp_t *client = malloc(sizeof(uv_tcp_t)); r = uv_tcp_init(server->loop, client); CHECK(r, "uv_tcp_init"); 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"); } r = uv_read_start((uv_stream_t*) client, alloc_cb, read_cb); CHECK(r, "uv_read_start"); }
static void accept_cb(uv_stream_t *stream, int status) { struct tundev_context *ctx = stream->data; struct client_context *client = new_client(ctx->tun->mtu); uv_tcp_init(stream->loop, &client->handle.tcp); int rc = uv_accept(stream, &client->handle.stream); if (rc == 0) { int len = sizeof(struct sockaddr); uv_tcp_getpeername(&client->handle.tcp, &client->addr, &len); client->handle.stream.data = ctx; uv_tcp_nodelay(&client->handle.tcp, 1); uv_tcp_keepalive(&client->handle.tcp, 1, 60); client->packet.size = 512; ctx->connect = AUTHING; uv_read_start(&client->handle.stream, alloc_cb, recv_cb); } else { logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc)); close_client(client); } }
static void tcp_make_connect(conn_rec* p) { struct sockaddr_in addr; int r; r = uv_tcp_init(loop, (uv_tcp_t*)&p->stream); ASSERT(r == 0); addr = uv_ip4_addr("127.0.0.1", TEST_PORT); r = uv_tcp_connect(&((tcp_conn_rec*)p)->conn_req, (uv_tcp_t*)&p->stream, addr, connect_cb); if (r) { fprintf(stderr, "uv_tcp_connect error %s\n", uv_err_name(uv_last_error(loop))); ASSERT(0); } #if DEBUG printf("make connect %d\n", p->i); #endif p->conn_req.data = p; p->write_req.data = p; p->stream.data = p; }
static void connection_cb(uv_stream_t* stream, int status) { client_t* client; int r; assert(status == 0); assert(stream == (uv_stream_t*)&tcp_server); printf("connected\n"); client = (client_t*)malloc(sizeof *client); assert(client != NULL); client->request_num = 1; r = uv_tcp_init(stream->loop, &client->handle); assert(r == 0); client->handle.data = client; r = uv_accept(stream, (uv_stream_t*)&client->handle); assert(r == 0); r = uv_read_start((uv_stream_t*)&client->handle, alloc_cb, read_cb); assert(r == 0); }