From aabe56b68019f55574b2ff770eaf687cb5f093a0 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 9 Jun 2011 22:38:41 +0200 Subject: [PATCH] uv_tcp_init() must be called before uv_accept() Windows broken. --- test/benchmark-pump.c | 2 ++ test/echo-server.c | 2 ++ test/test-delayed-accept.c | 6 ++++-- uv-unix.c | 5 +---- uv.h | 6 +++++- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/test/benchmark-pump.c b/test/benchmark-pump.c index a59e46df..8a8f52a7 100644 --- a/test/benchmark-pump.c +++ b/test/benchmark-pump.c @@ -256,6 +256,8 @@ static void connection_cb(uv_tcp_t* s, int status) { tcp = malloc(sizeof(uv_tcp_t)); + uv_tcp_init(tcp); + r = uv_accept(s, tcp); ASSERT(r == 0); diff --git a/test/echo-server.c b/test/echo-server.c index 54f45c0d..a3f8f29e 100644 --- a/test/echo-server.c +++ b/test/echo-server.c @@ -134,6 +134,8 @@ static void on_connection(uv_tcp_t* server, int status) { handle = (uv_tcp_t*) malloc(sizeof *handle); ASSERT(handle != NULL); + uv_tcp_init(handle); + r = uv_accept(server, handle); ASSERT(r == 0); diff --git a/test/test-delayed-accept.c b/test/test-delayed-accept.c index 8e0466b6..57e97fe9 100644 --- a/test/test-delayed-accept.c +++ b/test/test-delayed-accept.c @@ -60,14 +60,16 @@ static void do_accept(uv_handle_t* timer_handle, int status) { ASSERT(status == 0); ASSERT(accepted_handle != NULL); - /* Test to that uv_cnt_tcp_init increases across the uv_accept. */ + uv_tcp_init(accepted_handle); + + /* Test to that uv_cnt_tcp_init does not increase across the uv_accept. */ tcpcnt = uv_cnt_tcp_init; server = (uv_tcp_t*)timer_handle->data; r = uv_accept(server, accepted_handle); ASSERT(r == 0); - ASSERT(uv_cnt_tcp_init == tcpcnt + 1); + ASSERT(uv_cnt_tcp_init == tcpcnt); do_accept_called++; diff --git a/uv-unix.c b/uv-unix.c index 581eb451..7b9ae5ba 100644 --- a/uv-unix.c +++ b/uv-unix.c @@ -367,10 +367,7 @@ void uv__server_io(EV_P_ ev_io* watcher, int revents) { int uv_accept(uv_tcp_t* server, uv_tcp_t* client) { if (server->accepted_fd < 0) { - return -1; - } - - if (uv_tcp_init(client)) { + uv_err_new((uv_handle_t*) server, EAGAIN); return -1; } diff --git a/uv.h b/uv.h index da92899d..dda8b757 100644 --- a/uv.h +++ b/uv.h @@ -204,7 +204,11 @@ int uv_shutdown(uv_req_t* req); int uv_listen(uv_tcp_t* handle, int backlog, uv_connection_cb cb); -/* Call this after connection_cb. client does not need to be initialized. */ +/* This call is used in conjunction with uv_listen() to accept incoming TCP + * connections. Call uv_accept after receiving a uv_connection_cb to accept + * the connection. Before calling uv_accept use uv_tcp_init() must be + * called on the client. Non-zero return value indicates an error. + */ int uv_accept(uv_tcp_t* server, uv_tcp_t* client); /* Read data from an incoming stream. The callback will be made several