unix, windows: add uv_recv_buffer_size and uv_send_buffer_size

This commit is contained in:
Andrius Bentkus 2014-07-07 14:56:11 +02:00 committed by Saúl Ibarra Corretgé
parent 4dc0d81ccb
commit 0ecee213ea
9 changed files with 170 additions and 0 deletions

View File

@ -177,6 +177,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \
test/test-shutdown-twice.c \
test/test-signal-multiple-loops.c \
test/test-signal.c \
test/test-socket-buffer-size.c \
test/test-spawn.c \
test/test-stdio-over-pipes.c \
test/test-tcp-bind-error.c \

View File

@ -602,6 +602,30 @@ UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
*/
UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
/*
* Returns or sets the size of the receive buffer that the operating
* system uses for the socket.
*
* If *value == 0, it will return the current receive buffer size,
* otherwise it will use *value to set the new receive buffer size.
*
* NOTE: linux will set double the size and return double the size
* of the original set value.
*/
UV_EXTERN int uv_recv_buffer_size(uv_handle_t* handle, int* value);
/*
* Returns or sets the size of the send buffer that the operating
* system uses for the socket.
*
* If *value == 0, it will return the current send buffer size,
* otherwise it will use *value to set the new send buffer size.
*
* NOTE: linux will set double the size and return double the size
* of the original set value.
*/
UV_EXTERN int uv_send_buffer_size(uv_handle_t* handle, int* value);
/*
* Constructor for uv_buf_t.

View File

@ -163,6 +163,33 @@ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
uv__make_close_pending(handle);
}
int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value) {
int r;
int fd;
socklen_t len;
if (handle == NULL || value == NULL)
return -EINVAL;
if (handle->type == UV_TCP || handle->type == UV_NAMED_PIPE)
fd = uv__stream_fd((uv_stream_t*) handle);
else if (handle->type == UV_UDP)
fd = ((uv_udp_t *) handle)->io_watcher.fd;
else
return -ENOTSUP;
len = sizeof(*value);
if (*value == 0)
r = getsockopt(fd, SOL_SOCKET, optname, value, &len);
else
r = setsockopt(fd, SOL_SOCKET, optname, (const void*) value, len);
if (r < 0)
return -errno;
return 0;
}
void uv__make_close_pending(uv_handle_t* handle) {
assert(handle->flags & UV_CLOSING);

View File

@ -422,6 +422,13 @@ size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs) {
return bytes;
}
int uv_recv_buffer_size(uv_handle_t* handle, int* value) {
return uv__socket_sockopt(handle, SO_RCVBUF, value);
}
int uv_send_buffer_size(uv_handle_t* handle, int *value) {
return uv__socket_sockopt(handle, SO_SNDBUF, value);
}
int uv_fs_event_getpath(uv_fs_event_t* handle, char* buf, size_t* len) {
size_t required_len;

View File

@ -107,6 +107,8 @@ void uv__work_done(uv_async_t* handle);
size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs);
int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value);
#define uv__has_active_reqs(loop) \
(QUEUE_EMPTY(&(loop)->active_reqs) == 0)

View File

@ -428,3 +428,31 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) {
return r;
}
int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value) {
int r;
int len;
SOCKET socket;
if (handle == NULL || value == NULL)
return UV_EINVAL;
if (handle->type == UV_TCP)
socket = ((uv_tcp_t*) handle)->socket;
else if (handle->type == UV_UDP)
socket = ((uv_udp_t*) handle)->socket;
else
return UV_ENOTSUP;
len = sizeof(*value);
if (*value == 0)
r = getsockopt(socket, SOL_SOCKET, optname, (char*) value, &len);
else
r = setsockopt(socket, SOL_SOCKET, optname, (const char*) value, len);
if (r == SOCKET_ERROR)
return uv_translate_sys_error(WSAGetLastError());
return 0;
}

View File

@ -176,6 +176,7 @@ TEST_DECLARE (getsockname_tcp)
TEST_DECLARE (getsockname_udp)
TEST_DECLARE (fail_always)
TEST_DECLARE (pass_always)
TEST_DECLARE (socket_buffer_size)
TEST_DECLARE (spawn_fails)
TEST_DECLARE (spawn_exit_code)
TEST_DECLARE (spawn_stdout)
@ -506,6 +507,8 @@ TASK_LIST_START
TEST_ENTRY (poll_unidirectional)
TEST_ENTRY (poll_close)
TEST_ENTRY (socket_buffer_size)
TEST_ENTRY (spawn_fails)
TEST_ENTRY (spawn_exit_code)
TEST_ENTRY (spawn_stdout)

View File

@ -0,0 +1,77 @@
/* 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>
static uv_udp_t udp;
static uv_tcp_t tcp;
static int close_cb_called;
static void close_cb(uv_handle_t* handle) {
close_cb_called++;
}
static void check_buffer_size(uv_handle_t* handle) {
int value;
value = 0;
ASSERT(0 == uv_recv_buffer_size(handle, &value));
ASSERT(value > 0);
value = 10000;
ASSERT(0 == uv_recv_buffer_size(handle, &value));
value = 0;
ASSERT(0 == uv_recv_buffer_size(handle, &value));
/* linux sets double the value */
ASSERT(value == 10000 || value == 20000);
}
TEST_IMPL(socket_buffer_size) {
struct sockaddr_in addr;
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
ASSERT(0 == uv_tcp_init(uv_default_loop(), &tcp));
ASSERT(0 == uv_tcp_bind(&tcp, (struct sockaddr*) &addr, 0));
check_buffer_size((uv_handle_t*) &tcp);
uv_close((uv_handle_t*) &tcp, close_cb);
ASSERT(0 == uv_udp_init(uv_default_loop(), &udp));
ASSERT(0 == uv_udp_bind(&udp, (struct sockaddr*) &addr, 0));
check_buffer_size((uv_handle_t*) &udp);
uv_close((uv_handle_t*) &udp, close_cb);
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
ASSERT(close_cb_called == 2);
MAKE_VALGRIND_HAPPY();
return 0;
}

1
uv.gyp
View File

@ -360,6 +360,7 @@
'test/test-shutdown-twice.c',
'test/test-signal.c',
'test/test-signal-multiple-loops.c',
'test/test-socket-buffer-size.c',
'test/test-spawn.c',
'test/test-fs-poll.c',
'test/test-stdio-over-pipes.c',