mirror of https://github.com/nodejs/node.git
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.pull/5010/head
parent
34b0a36120
commit
6bd922fce8
|
@ -48,8 +48,13 @@ should be created via `dgram.createSocket(type, [callback])`.
|
||||||
* `msg` Buffer object. The message
|
* `msg` Buffer object. The message
|
||||||
* `rinfo` Object. Remote address information
|
* `rinfo` Object. Remote address information
|
||||||
|
|
||||||
Emitted when a new datagram is available on a socket. `msg` is a `Buffer` and `rinfo` is
|
Emitted when a new datagram is available on a socket. `msg` is a `Buffer` and
|
||||||
an object with the sender's address information and the number of bytes in the datagram.
|
`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'
|
### Event: 'listening'
|
||||||
|
|
||||||
|
@ -93,7 +98,7 @@ Example of sending a UDP packet to a random port on `localhost`;
|
||||||
var dgram = require('dgram');
|
var dgram = require('dgram');
|
||||||
var message = new Buffer("Some bytes");
|
var message = new Buffer("Some bytes");
|
||||||
var client = dgram.createSocket("udp4");
|
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();
|
client.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -302,11 +302,9 @@ Socket.prototype.send = function(buffer,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function afterSend(status, handle, req, buffer) {
|
function afterSend(err) {
|
||||||
var self = handle.owner;
|
if (this.cb)
|
||||||
|
this.cb(err ? errnoException(err, 'send') : null);
|
||||||
if (req.cb)
|
|
||||||
req.cb(null, buffer.length); // compatibility with dgram_legacy.js
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -341,14 +341,8 @@ void UDPWrap::OnSend(uv_udp_send_t* req, int status) {
|
||||||
assert(wrap->persistent().IsEmpty() == false);
|
assert(wrap->persistent().IsEmpty() == false);
|
||||||
|
|
||||||
Local<Object> req_wrap_obj = req_wrap->object();
|
Local<Object> req_wrap_obj = req_wrap->object();
|
||||||
Local<Value> argv[] = {
|
Local<Value> arg = Integer::New(status, node_isolate);
|
||||||
Integer::New(status, node_isolate),
|
MakeCallback(req_wrap_obj, oncomplete_sym, 1, &arg);
|
||||||
wrap->object(),
|
|
||||||
req_wrap_obj,
|
|
||||||
req_wrap_obj->GetHiddenValue(buffer_sym),
|
|
||||||
};
|
|
||||||
|
|
||||||
MakeCallback(req_wrap_obj, oncomplete_sym, ARRAY_SIZE(argv), argv);
|
|
||||||
delete req_wrap;
|
delete req_wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
|
@ -54,13 +54,11 @@ server.on('listening', function() {
|
||||||
server.close();
|
server.close();
|
||||||
});
|
});
|
||||||
client.send(message_to_send, 0, message_to_send.length,
|
client.send(message_to_send, 0, message_to_send.length,
|
||||||
server_port, 'localhost', function(err, bytes) {
|
server_port, 'localhost', function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('Caught error in client send.');
|
console.log('Caught error in client send.');
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
console.log('client wrote ' + bytes + ' bytes.');
|
|
||||||
assert.strictEqual(bytes, message_to_send.length);
|
|
||||||
});
|
});
|
||||||
client.on('close',
|
client.on('close',
|
||||||
function() {
|
function() {
|
||||||
|
|
Loading…
Reference in New Issue