diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2010-06-16 11:00:43 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-06-16 11:03:34 -0700 |
commit | cbf2a2233eb2ef17b7a341b7e88c4309f80457e0 (patch) | |
tree | 17586e7cb9adc8d8bc706aa473ce7cb85cee4b86 /lib/dgram.js | |
parent | 81ad8106dc3713e435eba7cb114c72b8e879112b (diff) | |
download | node-new-cbf2a2233eb2ef17b7a341b7e88c4309f80457e0.tar.gz |
Add callback to dgramSocket.send()
Diffstat (limited to 'lib/dgram.js')
-rw-r--r-- | lib/dgram.js | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/lib/dgram.js b/lib/dgram.js index 9a9c3483c2..ed9eec716b 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -146,17 +146,33 @@ Socket.prototype.address = function () { Socket.prototype.send = function(port, addr, buffer, offset, length) { var self = this; + var lastArg = arguments[arguments.length - 1]; + var callback = typeof lastArg === 'function' ? lastArg : null; + if (!isPort(arguments[0])) { - if (!self.fd) { - self.type = 'unix_dgram'; - self.fd = socket(self.type); + try { + if (!self.fd) { + self.type = 'unix_dgram'; + self.fd = socket(self.type); + } + var bytes = sendto(self.fd, buffer, offset, length, 0, port, addr); + } catch (e) { + if (callback) callback(e); + return; } - sendto(self.fd, buffer, offset, length, 0, port, addr); + + if (callback) callback(null, bytes); + } else { dns.lookup(arguments[1], function (err, ip, addressType) { + // DNS error if (err) { + if (callback) callback(err); self.emit('error', err); - } else { + return; + } + + try { if (!self.fd) { self.type = addressType == 4 ? 'udp4' : 'udp6'; self.fd = socket(self.type); @@ -165,8 +181,14 @@ Socket.prototype.send = function(port, addr, buffer, offset, length) { self._startWatcher(); }); } - sendto(self.fd, buffer, offset, length, 0, port, ip); + var bytes = sendto(self.fd, buffer, offset, length, 0, port, ip); + } catch (err) { + // socket creation, or sendto error. + if (callback) callback(err); + return; } + + if (callback) callback(null, bytes); }); } }; |