mirror of https://github.com/lukechilds/node.git
Browse Source
Fixes: https://github.com/nodejs/node/issues/4351 Fixes: https://github.com/nodejs/node/issues/6763 Refs: https://github.com/nodejs/node/pull/8280 PR-URL: https://github.com/nodejs/node/pull/9267 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>v7.x
cjihrig
8 years ago
committed by
Evan Lucas
104 changed files with 2511 additions and 897 deletions
@ -0,0 +1,70 @@ |
|||
# Supported platforms |
|||
|
|||
| System | Support type | Supported versions | Notes | |
|||
|---|---|---|---| |
|||
| GNU/Linux | Tier 1 | Linux >= 2.6.18 with glibc >= 2.5 | | |
|||
| macOS | Tier 1 | macOS >= 10.7 | | |
|||
| Windows | Tier 1 | Windows >= XP SP1 | MSVC 2008 and later are supported | |
|||
| FreeBSD | Tier 1 | >= 9 (see note) | | |
|||
| AIX | Tier 2 | >= 6 | Maintainers: @libuv/aix | |
|||
| z/OS | Tier 2 | >= V2R2 | Maintainers: @libuv/zos | |
|||
| Linux with musl | Tier 2 | musl >= 1.0 | | |
|||
| SunOS | Tier 2 | Solaris 121 and later | Maintainers: @libuv/sunos | |
|||
| MinGW | Tier 3 | MinGW32 and MinGW-w64 | | |
|||
| Other | Tier 3 | N/A | | |
|||
|
|||
#### Note on FreeBSD 9 |
|||
|
|||
While FreeBSD is supported as Tier 1, FreeBSD 9 will get Tier 2 support until |
|||
it reaches end of life, in December 2016. |
|||
|
|||
## Support types |
|||
|
|||
* **Tier 1**: Officially supported and tested with CI. Any contributed patch |
|||
MUST NOT break such systems. These are supported by @libuv/collaborators. |
|||
|
|||
* **Tier 2**: Officially supported, but not necessarily tested with CI. These |
|||
systems are maintained to the best of @libuv/collaborators ability, |
|||
without being a top priority. |
|||
|
|||
* **Tier 3**: Community maintained. These systems may inadvertently break and the |
|||
community and interested parties are expected to help with the maintenance. |
|||
|
|||
## Adding support for a new platform |
|||
|
|||
**IMPORTANT**: Before attempting to add support for a new platform please open |
|||
an issue about it for discussion. |
|||
|
|||
### Unix |
|||
|
|||
I/O handling is abstracted by an internal `uv__io_t` handle. The new platform |
|||
will need to implement some of the functions, the prototypes are in |
|||
``src/unix/internal.h``. |
|||
|
|||
If the new platform requires extra fields for any handle structure, create a |
|||
new include file in ``include/`` with the name ``uv-theplatform.h`` and add |
|||
the appropriate defines there. |
|||
|
|||
All functionality related to the new platform must be implemented in its own |
|||
file inside ``src/unix/`` unless it's already done in a common file, in which |
|||
case adding an `ifdef` is fine. |
|||
|
|||
Two build systems are supported: autotools and GYP. Ideally both need to be |
|||
supported, but if GYP does not support the new platform it can be left out. |
|||
|
|||
### Windows |
|||
|
|||
Windows is treated as a single platform, so adding support for a new platform |
|||
would mean adding support for a new version. |
|||
|
|||
Compilation and runtime must succeed for the minimum supported version. If a |
|||
new API is to be used, it must be done optionally, only in supported versions. |
|||
|
|||
### Common |
|||
|
|||
Some common notes when adding support for new platforms: |
|||
|
|||
* Generally libuv tries to avoid compile time checks. Do not add any to the |
|||
autotools based build system or use version checking macros. |
|||
Dynamically load functions and symbols if they are not supported by the |
|||
minimum supported version. |
@ -1,72 +0,0 @@ |
|||
/* Copyright (c) 2013, Sony Mobile Communications AB
|
|||
* Copyright (c) 2012, Google Inc. |
|||
All rights reserved. |
|||
|
|||
Redistribution and use in source and binary forms, with or without |
|||
modification, are permitted provided that the following conditions are |
|||
met: |
|||
|
|||
* Redistributions of source code must retain the above copyright |
|||
notice, this list of conditions and the following disclaimer. |
|||
* Redistributions in binary form must reproduce the above |
|||
copyright notice, this list of conditions and the following disclaimer |
|||
in the documentation and/or other materials provided with the |
|||
distribution. |
|||
* Neither the name of Google Inc. nor the names of its |
|||
contributors may be used to endorse or promote products derived from |
|||
this software without specific prior written permission. |
|||
|
|||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|||
*/ |
|||
|
|||
#ifndef GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_PTHREAD_FIXES_H |
|||
#define GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_PTHREAD_FIXES_H |
|||
|
|||
#include <pthread.h> |
|||
|
|||
|
|||
/*Android doesn't provide pthread_barrier_t for now.*/ |
|||
#ifndef PTHREAD_BARRIER_SERIAL_THREAD |
|||
|
|||
/* Anything except 0 will do here.*/ |
|||
#define PTHREAD_BARRIER_SERIAL_THREAD 0x12345 |
|||
|
|||
typedef struct { |
|||
pthread_mutex_t mutex; |
|||
pthread_cond_t cond; |
|||
unsigned count; |
|||
} pthread_barrier_t; |
|||
|
|||
int pthread_barrier_init(pthread_barrier_t* barrier, |
|||
const void* barrier_attr, |
|||
unsigned count); |
|||
|
|||
int pthread_barrier_wait(pthread_barrier_t* barrier); |
|||
int pthread_barrier_destroy(pthread_barrier_t *barrier); |
|||
#endif /* defined(PTHREAD_BARRIER_SERIAL_THREAD) */ |
|||
|
|||
int pthread_yield(void); |
|||
|
|||
/* Workaround pthread_sigmask() returning EINVAL on versions < 4.1 by
|
|||
* replacing all calls to pthread_sigmask with sigprocmask. See: |
|||
* https://android.googlesource.com/platform/bionic/+/9bf330b5
|
|||
* https://code.google.com/p/android/issues/detail?id=15337
|
|||
*/ |
|||
int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset); |
|||
|
|||
#ifdef pthread_sigmask |
|||
#undef pthread_sigmask |
|||
#endif |
|||
#define pthread_sigmask(how, set, oldset) uv__pthread_sigmask(how, set, oldset) |
|||
|
|||
#endif /* GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_PTHREAD_FIXES_H */ |
@ -0,0 +1,27 @@ |
|||
/* Copyright libuv project 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_MVS_H |
|||
#define UV_MVS_H |
|||
|
|||
#define UV_PLATFORM_SEM_T int |
|||
|
|||
#endif /* UV_MVS_H */ |
@ -0,0 +1,42 @@ |
|||
/* Copyright libuv project 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 "internal.h" |
|||
|
|||
int uv__io_check_fd(uv_loop_t* loop, int fd) { |
|||
struct pollfd p[1]; |
|||
int rv; |
|||
|
|||
p[0].fd = fd; |
|||
p[0].events = POLLIN; |
|||
|
|||
do |
|||
rv = poll(p, 1, 0); |
|||
while (rv == -1 && errno == EINTR); |
|||
|
|||
if (rv == -1) |
|||
abort(); |
|||
|
|||
if (p[0].revents & POLLNVAL) |
|||
return -1; |
|||
|
|||
return 0; |
|||
} |
@ -0,0 +1,35 @@ |
|||
#include "uv.h" |
|||
#include "internal.h" |
|||
#include "winapi.h" |
|||
|
|||
static void uv__register_system_resume_callback(); |
|||
|
|||
void uv__init_detect_system_wakeup() { |
|||
/* Try registering system power event callback. This is the cleanest
|
|||
* method, but it will only work on Win8 and above. |
|||
*/ |
|||
uv__register_system_resume_callback(); |
|||
} |
|||
|
|||
static ULONG CALLBACK uv__system_resume_callback(PVOID Context, |
|||
ULONG Type, |
|||
PVOID Setting) { |
|||
if (Type == PBT_APMRESUMESUSPEND || Type == PBT_APMRESUMEAUTOMATIC) |
|||
uv__wake_all_loops(); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static void uv__register_system_resume_callback() { |
|||
_DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS recipient; |
|||
_HPOWERNOTIFY registration_handle; |
|||
|
|||
if (pPowerRegisterSuspendResumeNotification == NULL) |
|||
return; |
|||
|
|||
recipient.Callback = uv__system_resume_callback; |
|||
recipient.Context = NULL; |
|||
(*pPowerRegisterSuspendResumeNotification)(DEVICE_NOTIFY_CALLBACK, |
|||
&recipient, |
|||
®istration_handle); |
|||
} |
@ -0,0 +1,123 @@ |
|||
/* Copyright libuv project and 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" |
|||
|
|||
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 uv_write_t write_req; |
|||
|
|||
static char hello[] = "HELLO!"; |
|||
|
|||
|
|||
static void close_cb(uv_handle_t* handle) { |
|||
close_cb_called++; |
|||
} |
|||
|
|||
static void write_cb(uv_write_t* req, int status) { |
|||
ASSERT(status == 0); |
|||
} |
|||
|
|||
static void conn_alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) { |
|||
/* Do nothing, read_cb should be called with UV_ENOBUFS. */ |
|||
} |
|||
|
|||
static void conn_read_cb(uv_stream_t* stream, |
|||
ssize_t nread, |
|||
const uv_buf_t* buf) { |
|||
ASSERT(nread == UV_ENOBUFS); |
|||
ASSERT(buf->base == NULL); |
|||
ASSERT(buf->len == 0); |
|||
|
|||
uv_close((uv_handle_t*) &incoming, close_cb); |
|||
uv_close((uv_handle_t*) &client, close_cb); |
|||
uv_close((uv_handle_t*) &server, close_cb); |
|||
} |
|||
|
|||
static void connect_cb(uv_connect_t* req, int status) { |
|||
int r; |
|||
uv_buf_t buf; |
|||
|
|||
ASSERT(status == 0); |
|||
connect_cb_called++; |
|||
|
|||
buf = uv_buf_init(hello, sizeof(hello)); |
|||
r = uv_write(&write_req, 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)); |
|||
ASSERT(0 == uv_read_start((uv_stream_t*) &incoming, |
|||
conn_alloc_cb, |
|||
conn_read_cb)); |
|||
|
|||
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_alloc_cb_fail) { |
|||
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_run(uv_default_loop(), UV_RUN_DEFAULT)); |
|||
|
|||
ASSERT(connect_cb_called == 1); |
|||
ASSERT(connection_cb_called == 1); |
|||
ASSERT(close_cb_called == 3); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
@ -1,119 +0,0 @@ |
|||
/* Copyright (c) 2015, Santiago Gimeno <santiago.gimeno@gmail.com>
|
|||
* |
|||
* 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> |
|||
|
|||
|
|||
static uv_tcp_t tcp_server; |
|||
static uv_tcp_t tcp_client; |
|||
static uv_tcp_t tcp_server_client; |
|||
static uv_connect_t connect_req; |
|||
static uv_write_t write_req; |
|||
|
|||
static void alloc_cb(uv_handle_t* handle, |
|||
size_t size, |
|||
uv_buf_t* buf) { |
|||
buf->base = malloc(size); |
|||
buf->len = size; |
|||
} |
|||
|
|||
static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) { |
|||
free(buf->base); |
|||
ASSERT(nread == UV_EOF); |
|||
} |
|||
|
|||
static void on_connect(uv_connect_t* req, int status) { |
|||
int r; |
|||
uv_buf_t outbuf; |
|||
|
|||
ASSERT(req != NULL); |
|||
ASSERT(status == 0); |
|||
|
|||
outbuf = uv_buf_init("ping", 4); |
|||
r = uv_write(&write_req, (uv_stream_t*) req->handle, &outbuf, 1, NULL); |
|||
ASSERT(r == 0); |
|||
|
|||
r = uv_read_start((uv_stream_t*) req->handle, alloc_cb, read_cb); |
|||
ASSERT(r == 0); |
|||
} |
|||
|
|||
static void on_connection(uv_stream_t* server, int status) { |
|||
int r; |
|||
|
|||
ASSERT(status == 0); |
|||
|
|||
r = uv_tcp_init(uv_default_loop(), &tcp_server_client); |
|||
ASSERT(r == 0); |
|||
|
|||
r = uv_accept(server, (uv_stream_t*) &tcp_server_client); |
|||
ASSERT(r == 0); |
|||
|
|||
uv_close((uv_handle_t*) &tcp_server_client, NULL); |
|||
uv_close((uv_handle_t*) &tcp_server, NULL); |
|||
} |
|||
|
|||
static void start_server(void) { |
|||
struct sockaddr_in addr; |
|||
int r; |
|||
|
|||
ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr)); |
|||
|
|||
r = uv_tcp_init(uv_default_loop(), &tcp_server); |
|||
ASSERT(r == 0); |
|||
|
|||
r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0); |
|||
ASSERT(r == 0); |
|||
|
|||
r = uv_listen((uv_stream_t*) &tcp_server, SOMAXCONN, on_connection); |
|||
ASSERT(r == 0); |
|||
} |
|||
|
|||
static void start_client(void) { |
|||
struct sockaddr_in addr; |
|||
int r; |
|||
|
|||
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); |
|||
|
|||
r = uv_tcp_init(uv_default_loop(), &tcp_client); |
|||
ASSERT(r == 0); |
|||
|
|||
r = uv_tcp_connect(&connect_req, |
|||
&tcp_client, |
|||
(const struct sockaddr*) &addr, |
|||
on_connect); |
|||
ASSERT(r == 0); |
|||
} |
|||
|
|||
|
|||
TEST_IMPL(tcp_squelch_connreset) { |
|||
|
|||
start_server(); |
|||
|
|||
start_client(); |
|||
|
|||
uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
@ -0,0 +1,197 @@ |
|||
/* Copyright libuv project 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 cl_recv_cb_called; |
|||
|
|||
static int sv_send_cb_called; |
|||
static int sv_recv_cb_called; |
|||
|
|||
static int close_cb_called; |
|||
|
|||
|
|||
static void sv_alloc_cb(uv_handle_t* handle, |
|||
size_t suggested_size, |
|||
uv_buf_t* buf) { |
|||
static char slab[65536]; |
|||
CHECK_HANDLE(handle); |
|||
buf->base = slab; |
|||
buf->len = sizeof(slab); |
|||
} |
|||
|
|||
|
|||
static void cl_alloc_cb(uv_handle_t* handle, |
|||
size_t suggested_size, |
|||
uv_buf_t* buf) { |
|||
/* Do nothing, recv_cb should be called with UV_ENOBUFS. */ |
|||
} |
|||
|
|||
|
|||
static void close_cb(uv_handle_t* handle) { |
|||
CHECK_HANDLE(handle); |
|||
ASSERT(1 == uv_is_closing(handle)); |
|||
close_cb_called++; |
|||
} |
|||
|
|||
|
|||
static void cl_recv_cb(uv_udp_t* handle, |
|||
ssize_t nread, |
|||
const uv_buf_t* buf, |
|||
const struct sockaddr* addr, |
|||
unsigned flags) { |
|||
CHECK_HANDLE(handle); |
|||
ASSERT(flags == 0); |
|||
ASSERT(nread == UV_ENOBUFS); |
|||
|
|||
cl_recv_cb_called++; |
|||
|
|||
uv_close((uv_handle_t*) handle, close_cb); |
|||
} |
|||
|
|||
|
|||
static void cl_send_cb(uv_udp_send_t* req, int status) { |
|||
int r; |
|||
|
|||
ASSERT(req != NULL); |
|||
ASSERT(status == 0); |
|||
CHECK_HANDLE(req->handle); |
|||
|
|||
r = uv_udp_recv_start(req->handle, cl_alloc_cb, cl_recv_cb); |
|||
ASSERT(r == 0); |
|||
|
|||
cl_send_cb_called++; |
|||
} |
|||
|
|||
|
|||
static void sv_send_cb(uv_udp_send_t* req, int status) { |
|||
ASSERT(req != NULL); |
|||
ASSERT(status == 0); |
|||
CHECK_HANDLE(req->handle); |
|||
|
|||
uv_close((uv_handle_t*) req->handle, close_cb); |
|||
free(req); |
|||
|
|||
sv_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) { |
|||
uv_udp_send_t* req; |
|||
uv_buf_t sndbuf; |
|||
int r; |
|||
|
|||
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)); |
|||
|
|||
r = uv_udp_recv_stop(handle); |
|||
ASSERT(r == 0); |
|||
|
|||
req = malloc(sizeof *req); |
|||
ASSERT(req != NULL); |
|||
|
|||
sndbuf = uv_buf_init("PONG", 4); |
|||
r = uv_udp_send(req, handle, &sndbuf, 1, addr, sv_send_cb); |
|||
ASSERT(r == 0); |
|||
|
|||
sv_recv_cb_called++; |
|||
} |
|||
|
|||
|
|||
TEST_IMPL(udp_alloc_cb_fail) { |
|||
struct sockaddr_in addr; |
|||
uv_udp_send_t req; |
|||
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, sv_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("PING", 4); |
|||
r = uv_udp_send(&req, |
|||
&client, |
|||
&buf, |
|||
1, |
|||
(const struct sockaddr*) &addr, |
|||
cl_send_cb); |
|||
ASSERT(r == 0); |
|||
|
|||
ASSERT(close_cb_called == 0); |
|||
ASSERT(cl_send_cb_called == 0); |
|||
ASSERT(cl_recv_cb_called == 0); |
|||
ASSERT(sv_send_cb_called == 0); |
|||
ASSERT(sv_recv_cb_called == 0); |
|||
|
|||
uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
|||
|
|||
ASSERT(cl_send_cb_called == 1); |
|||
ASSERT(cl_recv_cb_called == 1); |
|||
ASSERT(sv_send_cb_called == 1); |
|||
ASSERT(sv_recv_cb_called == 1); |
|||
ASSERT(close_cb_called == 2); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue