Skip to content

Commit 50e3318

Browse files
committed
dgram: add dgram send queue info
1 parent 90c758c commit 50e3318

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

doc/api/dgram.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ added: v8.7.0
461461

462462
This method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
463463

464+
### `socket.getSendQueueSize()`
465+
466+
<!-- YAML
467+
added: REPLACEME
468+
-->
469+
470+
* Returns: {number} Number of bytes queued for sending.
471+
472+
### `socket.getSendQueueCount()`
473+
474+
<!-- YAML
475+
added: REPLACEME
476+
-->
477+
478+
* Returns: {number} Number of send requests currently in the queue awaiting
479+
to be processed.
480+
464481
### `socket.ref()`
465482

466483
<!-- YAML

lib/dgram.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,13 @@ Socket.prototype.getSendBufferSize = function() {
976976
return bufferSize(this, 0, SEND_BUFFER);
977977
};
978978

979+
Socket.prototype.getSendQueueSize = function() {
980+
return this[kStateSymbol].handle.getSendQueueSize();
981+
};
982+
983+
Socket.prototype.getSendQueueCount = function() {
984+
return this[kStateSymbol].handle.getSendQueueCount();
985+
};
979986

980987
// Deprecated private APIs.
981988
ObjectDefineProperty(Socket.prototype, '_handle', {

src/udp_wrap.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ void UDPWrap::Initialize(Local<Object> target,
181181
SetProtoMethod(isolate, t, "setMulticastLoopback", SetMulticastLoopback);
182182
SetProtoMethod(isolate, t, "setBroadcast", SetBroadcast);
183183
SetProtoMethod(isolate, t, "setTTL", SetTTL);
184-
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
184+
SetProtoMethod(isolate, t, "getSendQueueSize", GetSendQueueSize);
185+
SetProtoMethod(isolate, t, "getSendQueueCount", GetSendQueueCount);
185186

186187
t->Inherit(HandleWrap::GetConstructorTemplate(env));
187188

@@ -783,6 +784,25 @@ MaybeLocal<Object> UDPWrap::Instantiate(Environment* env,
783784
return env->udp_constructor_function()->NewInstance(env->context());
784785
}
785786

787+
void UDPWrap::GetSendQueueSize(const FunctionCallbackInfo<Value>& args) {
788+
UDPWrap* wrap;
789+
ASSIGN_OR_RETURN_UNWRAP(&wrap,
790+
args.Holder(),
791+
args.GetReturnValue().Set(UV_EBADF));
792+
793+
size_t size = uv_udp_get_send_queue_size(&wrap->handle_);
794+
args.GetReturnValue().Set(static_cast<double>(size));
795+
}
796+
797+
void UDPWrap::GetSendQueueCount(const FunctionCallbackInfo<Value>& args) {
798+
UDPWrap* wrap;
799+
ASSIGN_OR_RETURN_UNWRAP(&wrap,
800+
args.Holder(),
801+
args.GetReturnValue().Set(UV_EBADF));
802+
803+
size_t count = uv_udp_get_send_queue_count(&wrap->handle_);
804+
args.GetReturnValue().Set(static_cast<double>(count));
805+
}
786806

787807
} // namespace node
788808

src/udp_wrap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ class UDPWrap final : public HandleWrap,
150150
static void SetBroadcast(const v8::FunctionCallbackInfo<v8::Value>& args);
151151
static void SetTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
152152
static void BufferSize(const v8::FunctionCallbackInfo<v8::Value>& args);
153+
static void GetSendQueueSize(
154+
const v8::FunctionCallbackInfo<v8::Value>& args);
155+
static void GetSendQueueCount(
156+
const v8::FunctionCallbackInfo<v8::Value>& args);
153157

154158
// UDPListener implementation
155159
uv_buf_t OnAlloc(size_t suggested_size) override;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const dgram = require('dgram');
5+
6+
const sock = dgram.createSocket('udp4');
7+
assert.strictEqual(sock.getSendQueueSize(), 0);
8+
assert.strictEqual(sock.getSendQueueCount(), 0);
9+
sock.close();

0 commit comments

Comments
 (0)