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