|
@ -12,6 +12,7 @@ var recvfrom = binding.recvfrom; |
|
|
var sendto = binding.sendto; |
|
|
var sendto = binding.sendto; |
|
|
var close = binding.close; |
|
|
var close = binding.close; |
|
|
var ENOENT = binding.ENOENT; |
|
|
var ENOENT = binding.ENOENT; |
|
|
|
|
|
var setBroadcast = binding.setBroadcast; |
|
|
|
|
|
|
|
|
function isPort (x) { return parseInt(x) >= 0; } |
|
|
function isPort (x) { return parseInt(x) >= 0; } |
|
|
var pool = null; |
|
|
var pool = null; |
|
@ -30,10 +31,16 @@ function getPool() { |
|
|
return pool; |
|
|
return pool; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function Socket (listener) { |
|
|
function Socket (broadcast, listener) { |
|
|
events.EventEmitter.call(this); |
|
|
events.EventEmitter.call(this); |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
|
|
|
if (typeof(broadcast) != 'boolean') { |
|
|
|
|
|
listener = broadcast; |
|
|
|
|
|
broadcast = false; |
|
|
|
|
|
} |
|
|
|
|
|
self.broadcast = broadcast; |
|
|
|
|
|
|
|
|
if (listener) { |
|
|
if (listener) { |
|
|
self.addListener('message', listener); |
|
|
self.addListener('message', listener); |
|
|
} |
|
|
} |
|
@ -57,8 +64,8 @@ function Socket (listener) { |
|
|
sys.inherits(Socket, events.EventEmitter); |
|
|
sys.inherits(Socket, events.EventEmitter); |
|
|
exports.Socket = Socket; |
|
|
exports.Socket = Socket; |
|
|
|
|
|
|
|
|
exports.createSocket = function (listener) { |
|
|
exports.createSocket = function (broadcast, listener) { |
|
|
return new Socket(listener); |
|
|
return new Socket(broadcast, listener); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Socket.prototype.bind = function () { |
|
|
Socket.prototype.bind = function () { |
|
@ -145,8 +152,7 @@ Socket.prototype.send = function(port, addr, buffer, offset, length) { |
|
|
self.fd = socket(self.type); |
|
|
self.fd = socket(self.type); |
|
|
} |
|
|
} |
|
|
sendto(self.fd, buffer, offset, length, 0, port, addr); |
|
|
sendto(self.fd, buffer, offset, length, 0, port, addr); |
|
|
} |
|
|
} else { |
|
|
else { |
|
|
|
|
|
dns.lookup(arguments[1], function (err, ip, addressType) { |
|
|
dns.lookup(arguments[1], function (err, ip, addressType) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
self.emit('error', err); |
|
|
self.emit('error', err); |
|
@ -154,6 +160,7 @@ Socket.prototype.send = function(port, addr, buffer, offset, length) { |
|
|
if (!self.fd) { |
|
|
if (!self.fd) { |
|
|
self.type = addressType == 4 ? 'udp4' : 'udp6'; |
|
|
self.type = addressType == 4 ? 'udp4' : 'udp6'; |
|
|
self.fd = socket(self.type); |
|
|
self.fd = socket(self.type); |
|
|
|
|
|
setBroadcast(self.fd, self.broadcast); |
|
|
process.nextTick(function() { |
|
|
process.nextTick(function() { |
|
|
self._startWatcher(); |
|
|
self._startWatcher(); |
|
|
}); |
|
|
}); |
|
@ -182,3 +189,4 @@ Socket.prototype.close = function () { |
|
|
self.emit("close"); |
|
|
self.emit("close"); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|