From 6bd922fce83d505e44ee3706df9a78c0ad449d64 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 27 Jun 2013 00:43:23 +0200 Subject: [PATCH] dgram: report send errors to cb, don't pass bytes Passing the number of sent bytes to the callback is superfluous; datagram sockets operate in atomic mode: either the sendmsg() system call succeeds or it fails but it never does partial writes. Instead, report send errors to the callback. UDP error reporting is fairly haphazard on most platforms. You should not expect reliable delivery of anything besides EMSGSIZE and (possibly) ENETDOWN and ENETUNREACH. Fixes #2608. --- doc/api/dgram.markdown | 11 +++++++--- lib/dgram.js | 8 +++---- src/udp_wrap.cc | 10 ++------- test/simple/test-dgram-msgsize.js | 35 +++++++++++++++++++++++++++++++ test/simple/test-dgram-udp4.js | 4 +--- 5 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 test/simple/test-dgram-msgsize.js diff --git a/doc/api/dgram.markdown b/doc/api/dgram.markdown index bb102b9484b..92c8bc955ec 100644 --- a/doc/api/dgram.markdown +++ b/doc/api/dgram.markdown @@ -48,8 +48,13 @@ should be created via `dgram.createSocket(type, [callback])`. * `msg` Buffer object. The message * `rinfo` Object. Remote address information -Emitted when a new datagram is available on a socket. `msg` is a `Buffer` and `rinfo` is -an object with the sender's address information and the number of bytes in the datagram. +Emitted when a new datagram is available on a socket. `msg` is a `Buffer` and +`rinfo` is an object with the sender's address information: + + socket.on('message', function(msg, rinfo) { + console.log('Received %d bytes from %s:%d\n', + msg.length, rinfo.address, rinfo.port); + }); ### Event: 'listening' @@ -93,7 +98,7 @@ Example of sending a UDP packet to a random port on `localhost`; var dgram = require('dgram'); var message = new Buffer("Some bytes"); var client = dgram.createSocket("udp4"); - client.send(message, 0, message.length, 41234, "localhost", function(err, bytes) { + client.send(message, 0, message.length, 41234, "localhost", function(err) { client.close(); }); diff --git a/lib/dgram.js b/lib/dgram.js index de0675fb007..3ea0665e66c 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -302,11 +302,9 @@ Socket.prototype.send = function(buffer, }; -function afterSend(status, handle, req, buffer) { - var self = handle.owner; - - if (req.cb) - req.cb(null, buffer.length); // compatibility with dgram_legacy.js +function afterSend(err) { + if (this.cb) + this.cb(err ? errnoException(err, 'send') : null); } diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 27799ffe9f6..7b335ec2e01 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -341,14 +341,8 @@ void UDPWrap::OnSend(uv_udp_send_t* req, int status) { assert(wrap->persistent().IsEmpty() == false); Local req_wrap_obj = req_wrap->object(); - Local argv[] = { - Integer::New(status, node_isolate), - wrap->object(), - req_wrap_obj, - req_wrap_obj->GetHiddenValue(buffer_sym), - }; - - MakeCallback(req_wrap_obj, oncomplete_sym, ARRAY_SIZE(argv), argv); + Local arg = Integer::New(status, node_isolate); + MakeCallback(req_wrap_obj, oncomplete_sym, 1, &arg); delete req_wrap; } diff --git a/test/simple/test-dgram-msgsize.js b/test/simple/test-dgram-msgsize.js new file mode 100644 index 00000000000..03f9cb5324c --- /dev/null +++ b/test/simple/test-dgram-msgsize.js @@ -0,0 +1,35 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// 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. + +var common = require('../common'); +var assert = require('assert'); +var dgram = require('dgram'); + +// Send a too big datagram. The destination doesn't matter because it's +// not supposed to get sent out anyway. +var buf = Buffer(256 * 1024); +var sock = dgram.createSocket('udp4'); +sock.send(buf, 0, buf.length, 12345, '127.0.0.1', common.mustCall(cb)); +function cb(err) { + assert(err instanceof Error); + assert.equal(err.code, 'EMSGSIZE'); + sock.close(); +} diff --git a/test/simple/test-dgram-udp4.js b/test/simple/test-dgram-udp4.js index 9a82de5c0d0..9b1fdfd42cc 100644 --- a/test/simple/test-dgram-udp4.js +++ b/test/simple/test-dgram-udp4.js @@ -54,13 +54,11 @@ server.on('listening', function() { server.close(); }); client.send(message_to_send, 0, message_to_send.length, - server_port, 'localhost', function(err, bytes) { + server_port, 'localhost', function(err) { if (err) { console.log('Caught error in client send.'); throw err; } - console.log('client wrote ' + bytes + ' bytes.'); - assert.strictEqual(bytes, message_to_send.length); }); client.on('close', function() {