From 29ec6b5916da9568da1ca3c9ff720efd360cf4ce Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 20 Sep 2017 00:55:53 -0400 Subject: [PATCH] dgram: refactor SO_RCVBUF and SO_SNDBUF methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/13623 PR-URL: https://github.com/nodejs/node/pull/15483 Reviewed-By: Tobias Nießen Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Luigi Pinca --- lib/dgram.js | 20 ++++++++++---------- src/udp_wrap.cc | 37 ++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 7dd9dd8847..85e5f909c0 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -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; @@ -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'); } @@ -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); } @@ -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); }; diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 8a4b6ea966..5fb8daed74 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -233,31 +233,26 @@ void UDPWrap::BufferSize(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(UV_EBADF)); CHECK(args[0]->IsUint32()); - CHECK(args[1]->IsUint32()); + CHECK(args[1]->IsBoolean()); + bool is_recv = args[1].As()->Value(); + const char* uv_func_name = is_recv ? "uv_recv_buffer_size" : + "uv_send_buffer_size"; - if (!args[0]->IsInt32()) { - if (args[1].As()->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(&wrap->handle_); int size = static_cast(args[0].As()->Value()); - if (args[1].As()->Value() == 0) { - err = uv_recv_buffer_size(reinterpret_cast(&wrap->handle_), - &size); - } else { - err = uv_send_buffer_size(reinterpret_cast(&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()->Value() == 0) - return env->ThrowUVException(err, "uv_recv_buffer_size"); - else - return env->ThrowUVException(err, "uv_send_buffer_size"); - } args.GetReturnValue().Set(size); }