mirror of https://github.com/lukechilds/node.git
Fedor Indutny
11 years ago
60 changed files with 3243 additions and 928 deletions
After Width: | Height: | Size: 43 KiB |
After Width: | Height: | Size: 64 KiB |
@ -0,0 +1,32 @@ |
|||||
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
* |
||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
* of this software and associated documentation files (the "Software"), to |
||||
|
* deal in the Software without restriction, including without limitation the |
||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
||||
|
* sell copies of the Software, and to permit persons to whom the Software is |
||||
|
* furnished to do so, subject to the following conditions: |
||||
|
* |
||||
|
* The above copyright notice and this permission notice shall be included in |
||||
|
* all copies or substantial portions of the Software. |
||||
|
* |
||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||||
|
* IN THE SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
#ifndef UV_AIX_H |
||||
|
#define UV_AIX_H |
||||
|
|
||||
|
#define UV_PLATFORM_LOOP_FIELDS \ |
||||
|
int fs_fd; \ |
||||
|
|
||||
|
#define UV_PLATFORM_FS_EVENT_FIELDS \ |
||||
|
uv__io_t event_watcher; \ |
||||
|
char *dir_filename; \ |
||||
|
|
||||
|
#endif /* UV_AIX_H */ |
@ -0,0 +1,37 @@ |
|||||
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
* |
||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
* of this software and associated documentation files (the "Software"), to |
||||
|
* deal in the Software without restriction, including without limitation the |
||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
||||
|
* sell copies of the Software, and to permit persons to whom the Software is |
||||
|
* furnished to do so, subject to the following conditions: |
||||
|
* |
||||
|
* The above copyright notice and this permission notice shall be included in |
||||
|
* all copies or substantial portions of the Software. |
||||
|
* |
||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||||
|
* IN THE SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
/*
|
||||
|
* This file is private to libuv. It provides common functionality to both |
||||
|
* Windows and Unix backends. |
||||
|
*/ |
||||
|
|
||||
|
#ifndef UV_THREADPOOL_H_ |
||||
|
#define UV_THREADPOOL_H_ |
||||
|
|
||||
|
struct uv__work { |
||||
|
void (*work)(struct uv__work *w); |
||||
|
void (*done)(struct uv__work *w, int status); |
||||
|
struct uv_loop_s* loop; |
||||
|
void* wq[2]; |
||||
|
}; |
||||
|
|
||||
|
#endif /* UV_THREADPOOL_H_ */ |
@ -1,81 +0,0 @@ |
|||||
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
||||
* |
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
||||
* of this software and associated documentation files (the "Software"), to |
|
||||
* deal in the Software without restriction, including without limitation the |
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
||||
* sell copies of the Software, and to permit persons to whom the Software is |
|
||||
* furnished to do so, subject to the following conditions: |
|
||||
* |
|
||||
* The above copyright notice and this permission notice shall be included in |
|
||||
* all copies or substantial portions of the Software. |
|
||||
* |
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|
||||
* IN THE SOFTWARE. |
|
||||
*/ |
|
||||
|
|
||||
#include <assert.h> |
|
||||
|
|
||||
#include "uv.h" |
|
||||
#include "internal.h" |
|
||||
#include "req-inl.h" |
|
||||
|
|
||||
|
|
||||
static void uv_work_req_init(uv_loop_t* loop, uv_work_t* req, |
|
||||
uv_work_cb work_cb, uv_after_work_cb after_work_cb) { |
|
||||
uv_req_init(loop, (uv_req_t*) req); |
|
||||
req->type = UV_WORK; |
|
||||
req->loop = loop; |
|
||||
req->work_cb = work_cb; |
|
||||
req->after_work_cb = after_work_cb; |
|
||||
memset(&req->overlapped, 0, sizeof(req->overlapped)); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
static DWORD WINAPI uv_work_thread_proc(void* parameter) { |
|
||||
uv_work_t* req = (uv_work_t*)parameter; |
|
||||
uv_loop_t* loop = req->loop; |
|
||||
|
|
||||
assert(req != NULL); |
|
||||
assert(req->type == UV_WORK); |
|
||||
assert(req->work_cb); |
|
||||
|
|
||||
req->work_cb(req); |
|
||||
|
|
||||
POST_COMPLETION_FOR_REQ(loop, req); |
|
||||
|
|
||||
return 0; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb, |
|
||||
uv_after_work_cb after_work_cb) { |
|
||||
if (work_cb == NULL) |
|
||||
return UV_EINVAL; |
|
||||
|
|
||||
uv_work_req_init(loop, req, work_cb, after_work_cb); |
|
||||
|
|
||||
if (!QueueUserWorkItem(&uv_work_thread_proc, req, WT_EXECUTELONGFUNCTION)) { |
|
||||
return uv_translate_sys_error(GetLastError()); |
|
||||
} |
|
||||
|
|
||||
uv__req_register(loop, req); |
|
||||
return 0; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
int uv_cancel(uv_req_t* req) { |
|
||||
return UV_ENOSYS; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
void uv_process_work_req(uv_loop_t* loop, uv_work_t* req) { |
|
||||
uv__req_unregister(loop, req); |
|
||||
if(req->after_work_cb) |
|
||||
req->after_work_cb(req, 0); |
|
||||
} |
|
@ -0,0 +1,137 @@ |
|||||
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
* |
||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
* of this software and associated documentation files (the "Software"), to |
||||
|
* deal in the Software without restriction, including without limitation the |
||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
||||
|
* sell copies of the Software, and to permit persons to whom the Software is |
||||
|
* furnished to do so, subject to the following conditions: |
||||
|
* |
||||
|
* The above copyright notice and this permission notice shall be included in |
||||
|
* all copies or substantial portions of the Software. |
||||
|
* |
||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||||
|
* IN THE SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <string.h> |
||||
|
|
||||
|
#include "uv.h" |
||||
|
#include "task.h" |
||||
|
|
||||
|
#define REQ_COUNT 100000 |
||||
|
|
||||
|
static uv_timer_t timer; |
||||
|
static uv_tcp_t server; |
||||
|
static uv_tcp_t client; |
||||
|
static uv_tcp_t incoming; |
||||
|
static int connect_cb_called; |
||||
|
static int close_cb_called; |
||||
|
static int connection_cb_called; |
||||
|
static int write_callbacks; |
||||
|
static int write_cancelled_callbacks; |
||||
|
static int write_error_callbacks; |
||||
|
|
||||
|
static uv_write_t write_requests[REQ_COUNT]; |
||||
|
|
||||
|
|
||||
|
static void close_cb(uv_handle_t* handle) { |
||||
|
close_cb_called++; |
||||
|
} |
||||
|
|
||||
|
void timer_cb(uv_timer_t* handle) { |
||||
|
uv_close((uv_handle_t*) &client, close_cb); |
||||
|
uv_close((uv_handle_t*) &server, close_cb); |
||||
|
uv_close((uv_handle_t*) &incoming, close_cb); |
||||
|
} |
||||
|
|
||||
|
void write_cb(uv_write_t* req, int status) { |
||||
|
if (status == 0) |
||||
|
write_callbacks++; |
||||
|
else if (status == UV_ECANCELED) |
||||
|
write_cancelled_callbacks++; |
||||
|
else |
||||
|
write_error_callbacks++; |
||||
|
} |
||||
|
|
||||
|
static void connect_cb(uv_connect_t* req, int status) { |
||||
|
static char base[1024]; |
||||
|
int r; |
||||
|
int i; |
||||
|
uv_buf_t buf; |
||||
|
|
||||
|
ASSERT(status == 0); |
||||
|
connect_cb_called++; |
||||
|
|
||||
|
buf = uv_buf_init(base, sizeof(base)); |
||||
|
|
||||
|
for (i = 0; i < REQ_COUNT; i++) { |
||||
|
r = uv_write(&write_requests[i], |
||||
|
req->handle, |
||||
|
&buf, |
||||
|
1, |
||||
|
write_cb); |
||||
|
ASSERT(r == 0); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void connection_cb(uv_stream_t* tcp, int status) { |
||||
|
ASSERT(status == 0); |
||||
|
|
||||
|
ASSERT(0 == uv_tcp_init(tcp->loop, &incoming)); |
||||
|
ASSERT(0 == uv_accept(tcp, (uv_stream_t*) &incoming)); |
||||
|
|
||||
|
connection_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void start_server(void) { |
||||
|
struct sockaddr_in addr; |
||||
|
|
||||
|
ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr)); |
||||
|
|
||||
|
ASSERT(0 == uv_tcp_init(uv_default_loop(), &server)); |
||||
|
ASSERT(0 == uv_tcp_bind(&server, (struct sockaddr*) &addr, 0)); |
||||
|
ASSERT(0 == uv_listen((uv_stream_t*) &server, 128, connection_cb)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
TEST_IMPL(tcp_write_queue_order) { |
||||
|
uv_connect_t connect_req; |
||||
|
struct sockaddr_in addr; |
||||
|
|
||||
|
start_server(); |
||||
|
|
||||
|
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); |
||||
|
|
||||
|
ASSERT(0 == uv_tcp_init(uv_default_loop(), &client)); |
||||
|
ASSERT(0 == uv_tcp_connect(&connect_req, |
||||
|
&client, |
||||
|
(struct sockaddr*) &addr, |
||||
|
connect_cb)); |
||||
|
|
||||
|
ASSERT(0 == uv_timer_init(uv_default_loop(), &timer)); |
||||
|
ASSERT(0 == uv_timer_start(&timer, timer_cb, 100, 0)); |
||||
|
|
||||
|
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); |
||||
|
|
||||
|
ASSERT(connect_cb_called == 1); |
||||
|
ASSERT(connection_cb_called == 1); |
||||
|
ASSERT(write_callbacks > 0); |
||||
|
ASSERT(write_cancelled_callbacks > 0); |
||||
|
ASSERT(write_callbacks + |
||||
|
write_error_callbacks + |
||||
|
write_cancelled_callbacks == REQ_COUNT); |
||||
|
ASSERT(close_cb_called == 3); |
||||
|
|
||||
|
MAKE_VALGRIND_HAPPY(); |
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,148 @@ |
|||||
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
* |
||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
* of this software and associated documentation files (the "Software"), to |
||||
|
* deal in the Software without restriction, including without limitation the |
||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
||||
|
* sell copies of the Software, and to permit persons to whom the Software is |
||||
|
* furnished to do so, subject to the following conditions: |
||||
|
* |
||||
|
* The above copyright notice and this permission notice shall be included in |
||||
|
* all copies or substantial portions of the Software. |
||||
|
* |
||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||||
|
* IN THE SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
#include "uv.h" |
||||
|
#include "task.h" |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <string.h> |
||||
|
|
||||
|
#define CHECK_HANDLE(handle) \ |
||||
|
ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client) |
||||
|
|
||||
|
static uv_udp_t server; |
||||
|
static uv_udp_t client; |
||||
|
|
||||
|
static int cl_send_cb_called; |
||||
|
static int sv_recv_cb_called; |
||||
|
static int close_cb_called; |
||||
|
|
||||
|
|
||||
|
static void alloc_cb(uv_handle_t* handle, |
||||
|
size_t suggested_size, |
||||
|
uv_buf_t* buf) { |
||||
|
static char slab[65536]; |
||||
|
CHECK_HANDLE(handle); |
||||
|
ASSERT(suggested_size <= sizeof(slab)); |
||||
|
buf->base = slab; |
||||
|
buf->len = sizeof(slab); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void close_cb(uv_handle_t* handle) { |
||||
|
CHECK_HANDLE(handle); |
||||
|
ASSERT(1 == uv_is_closing(handle)); |
||||
|
close_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void cl_send_cb(uv_udp_send_t* req, int status) { |
||||
|
ASSERT(req != NULL); |
||||
|
ASSERT(status == 0); |
||||
|
CHECK_HANDLE(req->handle); |
||||
|
|
||||
|
cl_send_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void sv_recv_cb(uv_udp_t* handle, |
||||
|
ssize_t nread, |
||||
|
const uv_buf_t* rcvbuf, |
||||
|
const struct sockaddr* addr, |
||||
|
unsigned flags) { |
||||
|
if (nread < 0) { |
||||
|
ASSERT(0 && "unexpected error"); |
||||
|
} |
||||
|
|
||||
|
if (nread == 0) { |
||||
|
/* Returning unused buffer */ |
||||
|
/* Don't count towards sv_recv_cb_called */ |
||||
|
ASSERT(addr == NULL); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
CHECK_HANDLE(handle); |
||||
|
ASSERT(flags == 0); |
||||
|
|
||||
|
ASSERT(addr != NULL); |
||||
|
ASSERT(nread == 4); |
||||
|
ASSERT(memcmp("PING", rcvbuf->base, nread) == 0 || |
||||
|
memcmp("PANG", rcvbuf->base, nread) == 0); |
||||
|
|
||||
|
if (++sv_recv_cb_called == 2) { |
||||
|
uv_close((uv_handle_t*) &server, close_cb); |
||||
|
uv_close((uv_handle_t*) &client, close_cb); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
TEST_IMPL(udp_send_immediate) { |
||||
|
struct sockaddr_in addr; |
||||
|
uv_udp_send_t req1, req2; |
||||
|
uv_buf_t buf; |
||||
|
int r; |
||||
|
|
||||
|
ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr)); |
||||
|
|
||||
|
r = uv_udp_init(uv_default_loop(), &server); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); |
||||
|
|
||||
|
r = uv_udp_init(uv_default_loop(), &client); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
/* client sends "PING", then "PANG" */ |
||||
|
buf = uv_buf_init("PING", 4); |
||||
|
|
||||
|
r = uv_udp_send(&req1, |
||||
|
&client, |
||||
|
&buf, |
||||
|
1, |
||||
|
(const struct sockaddr*) &addr, |
||||
|
cl_send_cb); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
buf = uv_buf_init("PANG", 4); |
||||
|
|
||||
|
r = uv_udp_send(&req2, |
||||
|
&client, |
||||
|
&buf, |
||||
|
1, |
||||
|
(const struct sockaddr*) &addr, |
||||
|
cl_send_cb); |
||||
|
|
||||
|
uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
||||
|
|
||||
|
ASSERT(cl_send_cb_called == 2); |
||||
|
ASSERT(sv_recv_cb_called == 2); |
||||
|
ASSERT(close_cb_called == 2); |
||||
|
|
||||
|
MAKE_VALGRIND_HAPPY(); |
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,133 @@ |
|||||
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
* |
||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
|
* of this software and associated documentation files (the "Software"), to |
||||
|
* deal in the Software without restriction, including without limitation the |
||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
||||
|
* sell copies of the Software, and to permit persons to whom the Software is |
||||
|
* furnished to do so, subject to the following conditions: |
||||
|
* |
||||
|
* The above copyright notice and this permission notice shall be included in |
||||
|
* all copies or substantial portions of the Software. |
||||
|
* |
||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||||
|
* IN THE SOFTWARE. |
||||
|
*/ |
||||
|
|
||||
|
#include "uv.h" |
||||
|
#include "task.h" |
||||
|
|
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <string.h> |
||||
|
|
||||
|
#ifdef _WIN32 |
||||
|
|
||||
|
TEST_IMPL(udp_try_send) { |
||||
|
|
||||
|
MAKE_VALGRIND_HAPPY(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
#else /* !_WIN32 */ |
||||
|
|
||||
|
#define CHECK_HANDLE(handle) \ |
||||
|
ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client) |
||||
|
|
||||
|
static uv_udp_t server; |
||||
|
static uv_udp_t client; |
||||
|
|
||||
|
static int sv_recv_cb_called; |
||||
|
|
||||
|
static int close_cb_called; |
||||
|
|
||||
|
|
||||
|
static void alloc_cb(uv_handle_t* handle, |
||||
|
size_t suggested_size, |
||||
|
uv_buf_t* buf) { |
||||
|
static char slab[65536]; |
||||
|
CHECK_HANDLE(handle); |
||||
|
ASSERT(suggested_size <= sizeof(slab)); |
||||
|
buf->base = slab; |
||||
|
buf->len = sizeof(slab); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void close_cb(uv_handle_t* handle) { |
||||
|
CHECK_HANDLE(handle); |
||||
|
ASSERT(uv_is_closing(handle)); |
||||
|
close_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
static void sv_recv_cb(uv_udp_t* handle, |
||||
|
ssize_t nread, |
||||
|
const uv_buf_t* rcvbuf, |
||||
|
const struct sockaddr* addr, |
||||
|
unsigned flags) { |
||||
|
ASSERT(nread > 0); |
||||
|
|
||||
|
if (nread == 0) { |
||||
|
ASSERT(addr == NULL); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
ASSERT(nread == 4); |
||||
|
ASSERT(addr != NULL); |
||||
|
|
||||
|
ASSERT(memcmp("EXIT", rcvbuf->base, nread) == 0); |
||||
|
uv_close((uv_handle_t*) handle, close_cb); |
||||
|
uv_close((uv_handle_t*) &client, close_cb); |
||||
|
|
||||
|
sv_recv_cb_called++; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
TEST_IMPL(udp_try_send) { |
||||
|
struct sockaddr_in addr; |
||||
|
static char buffer[64 * 1024]; |
||||
|
uv_buf_t buf; |
||||
|
int r; |
||||
|
|
||||
|
ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr)); |
||||
|
|
||||
|
r = uv_udp_init(uv_default_loop(), &server); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); |
||||
|
|
||||
|
r = uv_udp_init(uv_default_loop(), &client); |
||||
|
ASSERT(r == 0); |
||||
|
|
||||
|
buf = uv_buf_init(buffer, sizeof(buffer)); |
||||
|
r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &addr); |
||||
|
ASSERT(r == UV_EMSGSIZE); |
||||
|
|
||||
|
buf = uv_buf_init("EXIT", 4); |
||||
|
r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &addr); |
||||
|
ASSERT(r == 4); |
||||
|
|
||||
|
uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
||||
|
|
||||
|
ASSERT(close_cb_called == 2); |
||||
|
ASSERT(sv_recv_cb_called == 1); |
||||
|
|
||||
|
ASSERT(client.send_queue_size == 0); |
||||
|
ASSERT(server.send_queue_size == 0); |
||||
|
|
||||
|
MAKE_VALGRIND_HAPPY(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
#endif /* !_WIN32 */ |
Loading…
Reference in new issue