-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Dgram buffers #13623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dgram buffers #13623
Changes from 1 commit
9d6cd42
b84bf95
8aed7aa
f7c1aa6
a99ec11
2581c09
b0ac4ff
f6bf7f2
d5789c0
cd79a58
de458d1
f196be9
f4d3564
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,8 +135,7 @@ void UDPWrap::Initialize(Local<Object> target, | |
| env->SetProtoMethod(t, "setMulticastLoopback", SetMulticastLoopback); | ||
| env->SetProtoMethod(t, "setBroadcast", SetBroadcast); | ||
| env->SetProtoMethod(t, "setTTL", SetTTL); | ||
| env->SetProtoMethod(t, "setRecvBufferSize", SetRecvBufferSize); | ||
| env->SetProtoMethod(t, "setSendBufferSize", SetSendBufferSize); | ||
| env->SetProtoMethod(t, "bufferSize", BufferSize); | ||
|
|
||
| env->SetProtoMethod(t, "ref", HandleWrap::Ref); | ||
| env->SetProtoMethod(t, "unref", HandleWrap::Unref); | ||
|
|
@@ -225,35 +224,35 @@ void UDPWrap::Bind6(const FunctionCallbackInfo<Value>& args) { | |
| } | ||
|
|
||
|
|
||
| void UDPWrap::SetRecvBufferSize(const FunctionCallbackInfo<Value>& args) { | ||
| void UDPWrap::BufferSize(const FunctionCallbackInfo<Value>& args) { | ||
| Environment* env = Environment::GetCurrent(args); | ||
| UDPWrap* wrap; | ||
| ASSIGN_OR_RETURN_UNWRAP(&wrap, | ||
| args.Holder(), | ||
| args.GetReturnValue().Set(UV_EBADF)); | ||
|
|
||
| CHECK(args[0]->IsUint32()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably don't want to crash when the user does
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TimothyGu you mean on the JS side, right?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted, do you recommend:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend doing the type check in JS. Just something simple like: const errors = require('internal/errors');
// then within the the functions
if (typeof size !== 'number')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'size', 'number');
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your input. Do you think I need to do any safety checking in udp_wrap.cc SetRecvBufferSize() or we assume only dgram.js will interact with udp_wrap ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, once the type checking is added to the js side, I would keep the CHECK in place on the native side. That should only be hit if we do something wrong in core or if the user is doing something they really shouldn't. |
||
| int size = (int)args[0].As<Uint32>()->Value(); | ||
| int err = uv_recv_buffer_size(reinterpret_cast<uv_handle_t*>(&wrap->handle_), | ||
| CHECK(args[1]->IsUint32()); | ||
| int size = (int)args[0].As<Uint32>()->Value(); | ||
|
|
||
| int err = 0; | ||
| if (args[1].As<Uint32>()->Value() == 0) | ||
| err = uv_recv_buffer_size(reinterpret_cast<uv_handle_t*>(&wrap->handle_), | ||
| &size); | ||
|
|
||
| args.GetReturnValue().Set(err); | ||
| } | ||
|
|
||
|
|
||
| void UDPWrap::SetSendBufferSize(const FunctionCallbackInfo<Value>& args) { | ||
| Environment* env = Environment::GetCurrent(args); | ||
| UDPWrap* wrap; | ||
| ASSIGN_OR_RETURN_UNWRAP(&wrap, | ||
| args.Holder(), | ||
| args.GetReturnValue().Set(UV_EBADF)); | ||
|
|
||
| CHECK(args[0]->IsUint32()); | ||
| int size = (int)args[0].As<Uint32>()->Value(); | ||
| int err = uv_send_buffer_size(reinterpret_cast<uv_handle_t*>(&wrap->handle_), | ||
| else | ||
| err = uv_send_buffer_size(reinterpret_cast<uv_handle_t*>(&wrap->handle_), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about alignment. |
||
| &size); | ||
|
|
||
| args.GetReturnValue().Set(err); | ||
| if (err != 0) { | ||
| std::string syscall; | ||
| if (args[1].As<Uint32>()->Value() == 0) | ||
| syscall = "uv_recv_buffer_size"; | ||
| else | ||
| syscall = "uv_send_buffer_size"; | ||
|
|
||
| return env->ThrowUVException(err, syscall.c_str()); | ||
| } | ||
| args.GetReturnValue().Set(size); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,3 +39,22 @@ validTypes.forEach((validType) => { | |
| socket.close(); | ||
| }); | ||
| }); | ||
|
|
||
| // Ensure buffer sizes can be set | ||
| { | ||
| const socket = dgram.createSocket({ type: 'udp4', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little surprised the linter doesn't complain about this. Can you move |
||
| recvBufferSize: 1300, | ||
| sendBufferSize: 1800, | ||
| }); | ||
|
|
||
| socket.bind(common.mustCall(() => { | ||
| // note: linux will double the buffer size | ||
| assert.ok(socket.getRecvBufferSize() === 1300 || | ||
| socket.getRecvBufferSize() === 2600, | ||
| "SO_RCVBUF not 1300 or 2600"); | ||
| assert.ok(socket.getSendBufferSize() === 1800 || | ||
| socket.getSendBufferSize() === 3600, | ||
| "SO_SNDVBUF not 1800 or 3600"); | ||
| socket.close(); | ||
| })); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,28 +11,41 @@ const dgram = require('dgram'); | |
| assert.throws(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The duplication here could probably be reduced. |
||
| socket.setRecvBufferSize(8192); | ||
| }, common.expectsError({ | ||
| code: 'ERR_SOCKET_SET_BUFFER_SIZE', | ||
| code: 'ERR_SOCKET_BUFFER_SIZE', | ||
| type: Error, | ||
| message: /^Coud not set buffer size: E[A-Z]+$/ | ||
| message: /^Could not get or set buffer size:.*$/ | ||
| })); | ||
|
|
||
| assert.throws(() => { | ||
| socket.setSendBufferSize(8192); | ||
| }, common.expectsError({ | ||
| code: 'ERR_SOCKET_SET_BUFFER_SIZE', | ||
| code: 'ERR_SOCKET_BUFFER_SIZE', | ||
| type: Error, | ||
| message: /^Coud not set buffer size: E[A-Z]+$/ | ||
| message: /^Could not get or set buffer size:.*$/ | ||
| })); | ||
|
|
||
| assert.throws(() => { | ||
| socket.getRecvBufferSize(); | ||
| }, common.expectsError({ | ||
| code: 'ERR_SOCKET_BUFFER_SIZE', | ||
| type: Error, | ||
| message: /^Could not get or set buffer size:.*$/ | ||
| })); | ||
|
|
||
| assert.throws(() => { | ||
| socket.getSendBufferSize(); | ||
| }, common.expectsError({ | ||
| code: 'ERR_SOCKET_BUFFER_SIZE', | ||
| type: Error, | ||
| message: /^Could not get or set buffer size:.*$/ | ||
| })); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| { | ||
| // Should throw error if invalid buffer size is specified | ||
| const socket = dgram.createSocket('udp4'); | ||
|
|
||
| socket.bind(0, common.mustCall(() => { | ||
|
|
||
| socket.bind(common.mustCall(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test doesn't exercise
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, good spot., thanks. |
||
| assert.throws(() => { | ||
| socket.setRecvBufferSize(-1); | ||
| }, common.expectsError({ | ||
|
|
@@ -61,13 +74,21 @@ const dgram = require('dgram'); | |
| })); | ||
| } | ||
|
|
||
| // Can set and get buffer sizes after binding the socket. | ||
| { | ||
| // Can call setRecvBufferSize() and setRecvBufferSize() after binding the socket. | ||
| const socket = dgram.createSocket('udp4'); | ||
|
|
||
| socket.bind(0, common.mustCall(() => { | ||
| socket.setRecvBufferSize(8192); | ||
| socket.setSendBufferSize(8192); | ||
| socket.bind(common.mustCall(() => { | ||
| socket.setRecvBufferSize(1200); | ||
| socket.setSendBufferSize(1500); | ||
|
|
||
| // note: linux will double the buffer size | ||
| assert.ok(socket.getRecvBufferSize() === 1200 || | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to add an
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ternary condition nicer, maybe..
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not my cup of tea. const expectedBufferSize = common.isLinux ? 2400 : 1200;
assert.strictEqual(socket.getRecvBufferSize(), expectedBufferSize); |
||
| socket.getRecvBufferSize() === 2400, | ||
| "SO_RCVBUF not 1200 or 2400"); | ||
| assert.ok(socket.getSendBufferSize() === 1500 || | ||
| socket.getSendBufferSize() === 3000, | ||
| "SO_SNDVBUF not 1500 or 3000"); | ||
| socket.close(); | ||
| })); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought, but we might not need to mention
SO_RCVBUFandSO_SNDBUFat all. They aren't really needed to work with the API.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are not needed, but I though it was no harm having them mentioned in the docs. I'd like to get a few people's opinion on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that googling
SO_RCVBUFgives very good results (linux man and MSDN) so I'm +0.5There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can always add something like
(see socket(7))if you like, the doctool will expand that into a link.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for the record, I'm not opposed to including them. I just thought we might be able to get by without them. I'm open to whatever everyone thinks is best here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I know, the only reason I included them was that I was familiar with setting the buffer sizes with the JDK, and the javadocs mentioned these property names. When I started working on a small app to get more familiar with javascript, I was goggle'in around "node.js SO_RCVBUF" to see if anyone had already added or was in the process of adding buffer size setting support in node.js.