From 4b50b38676c67d46bad1baafbb375da1d0abcdab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20A=C3=A7acak?= Date: Thu, 20 Feb 2025 14:46:59 +0300 Subject: [PATCH] fixup! unix,win: introduce `uv_accept_reject` call --- src/win/stream.c | 4 +++ test/test-pipe-reject.c | 53 ++++++++++++++++++++++++++++++----- test/test-tcp-reject-accept.c | 32 ++++++++++----------- test/test-tcp-reject.c | 9 +++--- 4 files changed, 70 insertions(+), 28 deletions(-) diff --git a/src/win/stream.c b/src/win/stream.c index 8388e397..89f0ccbb 100644 --- a/src/win/stream.c +++ b/src/win/stream.c @@ -66,6 +66,8 @@ int uv_reject(uv_stream_t* server) { CloseHandle(pipe_req->pipeHandle); pipe_req->pipeHandle = INVALID_HANDLE_VALUE; + ((uv_pipe_t*)server)->pipe.serv.pending_accepts = pipe_req->next_pending; + pipe_req->next_pending = NULL; break; case UV_TCP: tcp_req = ((uv_tcp_t*)server)->tcp.serv.pending_accepts; @@ -76,6 +78,8 @@ int uv_reject(uv_stream_t* server) { closesocket(tcp_req->accept_socket); tcp_req->accept_socket = INVALID_SOCKET; + ((uv_tcp_t*)server)->tcp.serv.pending_accepts = tcp_req->next_pending; + tcp_req->next_pending = NULL; break; default: return UV_EINVAL; diff --git a/test/test-pipe-reject.c b/test/test-pipe-reject.c index 243bfbeb..4f0ad581 100644 --- a/test/test-pipe-reject.c +++ b/test/test-pipe-reject.c @@ -27,8 +27,11 @@ typedef struct { uv_connect_t conn_req; } client_t; static uv_pipe_t server_handle; -static client_t client_handle; static int connect_cb_called; +static int do_accept_called = 0; +static int connection_cb_called = 0; +static uv_pipe_t connections[2]; +static client_t clients[2]; static void connect_cb(uv_connect_t* _, int status) { ASSERT_OK(status); @@ -36,9 +39,36 @@ static void connect_cb(uv_connect_t* _, int status) { } static void connection_pipe_cb(uv_stream_t* server, int status) { - ASSERT_OK(uv_reject(server)); + int r; + uv_pipe_t* conn; + + conn = &connections[connection_cb_called]; ASSERT_OK(status); - uv_close((uv_handle_t*) &server_handle, NULL); + + if (connection_cb_called == 0) { + ASSERT_OK(uv_reject(server)); + + r = uv_pipe_init(server->loop, conn, 0); + ASSERT_OK(r); + + r = uv_accept(server, (uv_stream_t*)conn); + ASSERT(r == UV_EAGAIN); + } else { + r = uv_pipe_init(server->loop, conn, 0); + ASSERT_OK(r); + + r = uv_accept(server, (uv_stream_t*)conn); + ASSERT_OK(r); + + do_accept_called++; + } + + /* After accepting the second client, close the server handle */ + if (do_accept_called == 1) { + uv_close((uv_handle_t*) &server_handle, NULL); + } + + connection_cb_called++; } TEST_IMPL(pipe_reject) { @@ -57,15 +87,24 @@ TEST_IMPL(pipe_reject) { r = uv_listen((uv_stream_t*)&server_handle, 128, connection_pipe_cb); ASSERT_OK(r); - r = uv_pipe_init(loop, (uv_pipe_t*)&client_handle, 0); + r = uv_pipe_init(loop, &clients[0].pipe_handle, 0); ASSERT_OK(r); - uv_pipe_connect(&client_handle.conn_req, - &client_handle.pipe_handle, + uv_pipe_connect(&clients[0].conn_req, + &clients[0].pipe_handle, + TEST_PIPENAME, + connect_cb); + + r = uv_pipe_init(loop, &clients[1].pipe_handle, 0); + ASSERT_OK(r); + uv_pipe_connect(&clients[1].conn_req, + &clients[1].pipe_handle, TEST_PIPENAME, connect_cb); uv_run(loop, UV_RUN_DEFAULT); - ASSERT(connect_cb_called == 1); + ASSERT_EQ(2, connection_cb_called); + ASSERT_EQ(2, connect_cb_called); + ASSERT_EQ(1, do_accept_called); return 0; } diff --git a/test/test-tcp-reject-accept.c b/test/test-tcp-reject-accept.c index b3e14d8c..a58b7ba2 100644 --- a/test/test-tcp-reject-accept.c +++ b/test/test-tcp-reject-accept.c @@ -28,42 +28,42 @@ static int connection_cb_called = 0; static int do_accept_called = 0; static int close_cb_called = 0; static int connect_cb_called = 0; +static uv_tcp_t connections[2]; uv_tcp_t* server; static void close_cb(uv_handle_t* handle) { ASSERT_NOT_NULL(handle); - free(handle); close_cb_called++; } static void connection_cb(uv_stream_t* tcp, int status) { + int r; + uv_tcp_t* conn; + + conn = &connections[connection_cb_called]; ASSERT_OK(status); if (connection_cb_called == 0) { ASSERT_OK(uv_reject(tcp)); -#ifdef _WIN32 - ASSERT(server->tcp.serv.pending_accepts->accept_socket == INVALID_SOCKET); -#else - ASSERT(server->accepted_fd == -1); -#endif - } else { - uv_tcp_t* accepted_handle = (uv_tcp_t*)malloc(sizeof *accepted_handle); - - ASSERT_NOT_NULL(accepted_handle); - - int r = uv_tcp_init(uv_default_loop(), accepted_handle); + r = uv_tcp_init(tcp->loop, conn); ASSERT_OK(r); - r = uv_accept((uv_stream_t*)tcp, (uv_stream_t*)accepted_handle); + r = uv_accept(tcp, (uv_stream_t*)conn); + ASSERT(r == UV_EAGAIN); + } else { + r = uv_tcp_init(uv_default_loop(), conn); + ASSERT_OK(r); + + r = uv_accept((uv_stream_t*)tcp, (uv_stream_t*)conn); ASSERT_OK(r); do_accept_called++; - uv_close((uv_handle_t*)accepted_handle, close_cb); + uv_close((uv_handle_t*)conn, close_cb); } - /* After accepting the two clients close the server handle */ + /* After accepting the second client, close the server handle */ if (do_accept_called == 1) { uv_close((uv_handle_t*)tcp, close_cb); } @@ -73,7 +73,7 @@ static void connection_cb(uv_stream_t* tcp, int status) { static void start_server(void) { struct sockaddr_in addr; - server = (uv_tcp_t*)malloc(sizeof *server); + server = malloc(sizeof *server); int r; ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr)); diff --git a/test/test-tcp-reject.c b/test/test-tcp-reject.c index 59cefb4b..c9f41be2 100644 --- a/test/test-tcp-reject.c +++ b/test/test-tcp-reject.c @@ -24,6 +24,7 @@ static uv_tcp_t server; static uv_tcp_t client; +static uv_tcp_t conn; static int connection_cb_called; static int connect_cb_called; @@ -40,11 +41,9 @@ static void connection_cb(uv_stream_t* tcp, int status) { ASSERT_OK(uv_reject(tcp)); /* The server should not have accepted the connection */ -#ifdef _WIN32 - ASSERT(server.tcp.serv.pending_accepts->accept_socket == INVALID_SOCKET); -#else - ASSERT(server.accepted_fd == -1); -#endif + int r = uv_tcp_init(uv_default_loop(), &conn); + r = uv_accept((uv_stream_t*)tcp, (uv_stream_t*)&conn); + ASSERT(r == UV_EAGAIN); /* Close the server */ uv_close((uv_handle_t*) &server, NULL);