listen for error on udp socket

This commit is contained in:
Feross Aboukhadijeh 2014-07-13 15:28:23 -07:00
parent 3ca0563e84
commit d1ac7d930e

View File

@ -50,18 +50,22 @@ function Server (opts) {
if (opts.http !== false) { if (opts.http !== false) {
self._httpServer = http.createServer() self._httpServer = http.createServer()
self._httpServer.on('request', self._onHttpRequest.bind(self)) self._httpServer.on('request', self._onHttpRequest.bind(self))
self._httpServer.on('error', function (err) { self._httpServer.on('error', self._onError.bind(self))
self.emit('error', err)
})
} }
// default to starting a udp server unless the user explicitly says no // default to starting a udp server unless the user explicitly says no
if (opts.udp !== false) { if (opts.udp !== false) {
self._udpServer = dgram.createSocket('udp4') self._udpServer = dgram.createSocket('udp4')
self._udpServer.on('message', self._onUdpRequest.bind(self)) self._udpServer.on('message', self._onUdpRequest.bind(self))
self._udpServer.on('error', self._onError.bind(self))
} }
} }
Server.prototype._onError = function (err) {
var self = this
self.emit('error', err)
}
Server.prototype.listen = function (port, onlistening) { Server.prototype.listen = function (port, onlistening) {
var self = this var self = this
var tasks = [] var tasks = []
@ -114,7 +118,6 @@ Server.prototype._getSwarm = function (binaryInfoHash) {
peers: {} peers: {}
} }
} }
return swarm return swarm
} }
@ -439,7 +442,9 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
function send (buf) { function send (buf) {
socket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () { socket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () {
try { socket.close() } catch (err) {} try {
socket.close()
} catch (err) {}
}) })
} }