Skip to content

Commit

Permalink
dgram: refactor SO_RCVBUF and SO_SNDBUF methods
Browse files Browse the repository at this point in the history
This commit refactors the get/set send/receive buffer size
methods in the following ways:

- Use booleans instead of strings and numbers to differentiate
between the send and receive buffers.
- Reduce the amount of branching and complexity in the C++ code.

Refs: #13623
PR-URL: #15483
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
cjihrig authored and MylesBorins committed Oct 3, 2017
1 parent d218f60 commit 0870a80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
20 changes: 10 additions & 10 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const BIND_STATE_UNBOUND = 0;
const BIND_STATE_BINDING = 1;
const BIND_STATE_BOUND = 2;

const RECV_BUFFER = true;
const SEND_BUFFER = false;

// Lazily loaded
var cluster = null;

Expand Down Expand Up @@ -146,10 +149,10 @@ function startListening(socket) {
socket.fd = -42; // compatibility hack

if (socket[kOptionSymbol].recvBufferSize)
bufferSize(socket, socket[kOptionSymbol].recvBufferSize, 'recv');
bufferSize(socket, socket[kOptionSymbol].recvBufferSize, RECV_BUFFER);

if (socket[kOptionSymbol].sendBufferSize)
bufferSize(socket, socket[kOptionSymbol].sendBufferSize, 'send');
bufferSize(socket, socket[kOptionSymbol].sendBufferSize, SEND_BUFFER);

socket.emit('listening');
}
Expand All @@ -172,10 +175,7 @@ function bufferSize(self, size, buffer) {
throw new errors.TypeError('ERR_SOCKET_BAD_BUFFER_SIZE');

try {
if (buffer === 'recv')
return self._handle.bufferSize(size, 0);
else
return self._handle.bufferSize(size, 1);
return self._handle.bufferSize(size, buffer);
} catch (e) {
throw new errors.Error('ERR_SOCKET_BUFFER_SIZE', e);
}
Expand Down Expand Up @@ -677,22 +677,22 @@ Socket.prototype.unref = function() {


Socket.prototype.setRecvBufferSize = function(size) {
bufferSize(this, size, 'recv');
bufferSize(this, size, RECV_BUFFER);
};


Socket.prototype.setSendBufferSize = function(size) {
bufferSize(this, size, 'send');
bufferSize(this, size, SEND_BUFFER);
};


Socket.prototype.getRecvBufferSize = function() {
return bufferSize(this, 0, 'recv');
return bufferSize(this, 0, RECV_BUFFER);
};


Socket.prototype.getSendBufferSize = function() {
return bufferSize(this, 0, 'send');
return bufferSize(this, 0, SEND_BUFFER);
};


Expand Down
37 changes: 16 additions & 21 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,31 +233,26 @@ void UDPWrap::BufferSize(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(UV_EBADF));

CHECK(args[0]->IsUint32());
CHECK(args[1]->IsUint32());
CHECK(args[1]->IsBoolean());
bool is_recv = args[1].As<v8::Boolean>()->Value();
const char* uv_func_name = is_recv ? "uv_recv_buffer_size" :
"uv_send_buffer_size";

if (!args[0]->IsInt32()) {
if (args[1].As<Uint32>()->Value() == 0)
return env->ThrowUVException(UV_EINVAL, "uv_recv_buffer_size");
else
return env->ThrowUVException(UV_EINVAL, "uv_send_buffer_size");
}
if (!args[0]->IsInt32())
return env->ThrowUVException(UV_EINVAL, uv_func_name);

int err;
uv_handle_t* handle = reinterpret_cast<uv_handle_t*>(&wrap->handle_);
int size = static_cast<int>(args[0].As<Uint32>()->Value());
if (args[1].As<Uint32>()->Value() == 0) {
err = uv_recv_buffer_size(reinterpret_cast<uv_handle_t*>(&wrap->handle_),
&size);
} else {
err = uv_send_buffer_size(reinterpret_cast<uv_handle_t*>(&wrap->handle_),
&size);
}
int err;

if (is_recv)
err = uv_recv_buffer_size(handle, &size);
else
err = uv_send_buffer_size(handle, &size);

if (err != 0)
return env->ThrowUVException(err, uv_func_name);

if (err != 0) {
if (args[1].As<Uint32>()->Value() == 0)
return env->ThrowUVException(err, "uv_recv_buffer_size");
else
return env->ThrowUVException(err, "uv_send_buffer_size");
}
args.GetReturnValue().Set(size);
}

Expand Down

0 comments on commit 0870a80

Please sign in to comment.