fixup! unix,win: introduce uv_accept_reject call

This commit is contained in:
Hüseyin Açacak 2025-02-20 14:46:59 +03:00 committed by Hüseyin Açacak
parent e7880bf954
commit 4b50b38676
4 changed files with 70 additions and 28 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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));

View File

@ -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);