Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix, windows: add uv_recv_buffer_size and uv_send_buffer_size
Browse files Browse the repository at this point in the history
  • Loading branch information
txdv authored and saghul committed Aug 9, 2014
1 parent 4dc0d81 commit 0ecee21
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
24 changes: 24 additions & 0 deletions include/uv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions src/unix/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/uv-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/uv-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
28 changes: 28 additions & 0 deletions src/win/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
77 changes: 77 additions & 0 deletions test/test-socket-buffer-size.c
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions uv.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0ecee21

Please sign in to comment.