|
@ -248,7 +248,7 @@ function allocRecvBuffer () { |
|
|
|
|
|
|
|
|
function _doFlush () { |
|
|
function _doFlush () { |
|
|
var socket = this.socket; |
|
|
var socket = this.socket; |
|
|
// Socket becomes writeable on connect() but don't flush if there's
|
|
|
// Stream becomes writeable on connect() but don't flush if there's
|
|
|
// nothing actually to write
|
|
|
// nothing actually to write
|
|
|
if ((socket._writeQueueSize == 0) && (socket._writeMessageQueueSize == 0)) { |
|
|
if ((socket._writeQueueSize == 0) && (socket._writeMessageQueueSize == 0)) { |
|
|
return; |
|
|
return; |
|
@ -262,7 +262,7 @@ function _doFlush () { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function initSocket (self) { |
|
|
function initStream (self) { |
|
|
self._readWatcher = ioWatchers.alloc(); |
|
|
self._readWatcher = ioWatchers.alloc(); |
|
|
self._readWatcher.callback = function () { |
|
|
self._readWatcher.callback = function () { |
|
|
// If this is the first recv (recvBuffer doesn't exist) or we've used up
|
|
|
// If this is the first recv (recvBuffer doesn't exist) or we've used up
|
|
@ -361,7 +361,7 @@ function initSocket (self) { |
|
|
self.writable = false; |
|
|
self.writable = false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function Socket (fd) { |
|
|
function Stream (fd) { |
|
|
events.EventEmitter.call(this); |
|
|
events.EventEmitter.call(this); |
|
|
|
|
|
|
|
|
this.fd = null; |
|
|
this.fd = null; |
|
@ -370,12 +370,12 @@ function Socket (fd) { |
|
|
this.open(fd); |
|
|
this.open(fd); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
sys.inherits(Socket, events.EventEmitter); |
|
|
sys.inherits(Stream, events.EventEmitter); |
|
|
exports.Socket = Socket; |
|
|
exports.Stream = Stream; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.open = function (fd) { |
|
|
Stream.prototype.open = function (fd) { |
|
|
initSocket(this); |
|
|
initStream(this); |
|
|
|
|
|
|
|
|
this.fd = fd; |
|
|
this.fd = fd; |
|
|
|
|
|
|
|
@ -387,14 +387,14 @@ Socket.prototype.open = function (fd) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.createConnection = function (port, host) { |
|
|
exports.createConnection = function (port, host) { |
|
|
var s = new Socket(); |
|
|
var s = new Stream(); |
|
|
s.connect(port, host); |
|
|
s.connect(port, host); |
|
|
return s; |
|
|
return s; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var readyStateMessage; |
|
|
var readyStateMessage; |
|
|
Object.defineProperty(Socket.prototype, 'readyState', { |
|
|
Object.defineProperty(Stream.prototype, 'readyState', { |
|
|
get: function () { |
|
|
get: function () { |
|
|
if (!readyStateMessage) { |
|
|
if (!readyStateMessage) { |
|
|
readyStateMessage = 'readyState is depricated. Use stream.readable or stream.writable'; |
|
|
readyStateMessage = 'readyState is depricated. Use stream.readable or stream.writable'; |
|
@ -413,7 +413,7 @@ Object.defineProperty(Socket.prototype, 'readyState', { |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype._allocateSendBuffer = function () { |
|
|
Stream.prototype._allocateSendBuffer = function () { |
|
|
var b = buffers.alloc(1024); |
|
|
var b = buffers.alloc(1024); |
|
|
b.used = 0; |
|
|
b.used = 0; |
|
|
b.sent = 0; |
|
|
b.sent = 0; |
|
@ -423,9 +423,9 @@ Socket.prototype._allocateSendBuffer = function () { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype._writeString = function (data, encoding) { |
|
|
Stream.prototype._writeString = function (data, encoding) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
if (!self.writable) throw new Error('Socket is not writable'); |
|
|
if (!self.writable) throw new Error('Stream is not writable'); |
|
|
var buffer; |
|
|
var buffer; |
|
|
|
|
|
|
|
|
if (self._writeQueue.length == 0) { |
|
|
if (self._writeQueue.length == 0) { |
|
@ -478,17 +478,17 @@ Socket.prototype._writeString = function (data, encoding) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.__writeQueueLast = function () { |
|
|
Stream.prototype.__writeQueueLast = function () { |
|
|
return this._writeQueue.length > 0 ? this._writeQueue[this._writeQueue.length-1] |
|
|
return this._writeQueue.length > 0 ? this._writeQueue[this._writeQueue.length-1] |
|
|
: null; |
|
|
: null; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.send = function () { |
|
|
Stream.prototype.send = function () { |
|
|
throw new Error('send renamed to write'); |
|
|
throw new Error('send renamed to write'); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Socket.prototype.setEncoding = function (enc) { |
|
|
Stream.prototype.setEncoding = function (enc) { |
|
|
// TODO check values, error out on bad, and deprecation message?
|
|
|
// TODO check values, error out on bad, and deprecation message?
|
|
|
this._encoding = enc.toLowerCase(); |
|
|
this._encoding = enc.toLowerCase(); |
|
|
}; |
|
|
}; |
|
@ -496,10 +496,10 @@ Socket.prototype.setEncoding = function (enc) { |
|
|
// Returns true if all the data was flushed to socket. Returns false if
|
|
|
// Returns true if all the data was flushed to socket. Returns false if
|
|
|
// something was queued. If data was queued, then the "drain" event will
|
|
|
// something was queued. If data was queued, then the "drain" event will
|
|
|
// signal when it has been finally flushed to socket.
|
|
|
// signal when it has been finally flushed to socket.
|
|
|
Socket.prototype.write = function (data, encoding) { |
|
|
Stream.prototype.write = function (data, encoding) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
if (!self.writable) throw new Error('Socket is not writable'); |
|
|
if (!self.writable) throw new Error('Stream is not writable'); |
|
|
|
|
|
|
|
|
if (self.__writeQueueLast() == END_OF_FILE) { |
|
|
if (self.__writeQueueLast() == END_OF_FILE) { |
|
|
throw new Error('socket.close() called already; cannot write.'); |
|
|
throw new Error('socket.close() called already; cannot write.'); |
|
@ -520,10 +520,10 @@ Socket.prototype.write = function (data, encoding) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// Sends a file descriptor over a unix socket
|
|
|
// Sends a file descriptor over a unix socket
|
|
|
Socket.prototype.sendFD = function(socketToPass) { |
|
|
Stream.prototype.sendFD = function(socketToPass) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
if (!self.writable) throw new Error('Socket is not writable'); |
|
|
if (!self.writable) throw new Error('Stream is not writable'); |
|
|
|
|
|
|
|
|
if (self.__writeQueueLast() == END_OF_FILE) { |
|
|
if (self.__writeQueueLast() == END_OF_FILE) { |
|
|
throw new Error('socket.close() called already; cannot write.'); |
|
|
throw new Error('socket.close() called already; cannot write.'); |
|
@ -533,7 +533,7 @@ Socket.prototype.sendFD = function(socketToPass) { |
|
|
throw new Error('FD passing only available on unix sockets'); |
|
|
throw new Error('FD passing only available on unix sockets'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (! socketToPass instanceof Socket) { |
|
|
if (! socketToPass instanceof Stream) { |
|
|
throw new Error('Provided arg is not a socket'); |
|
|
throw new Error('Provided arg is not a socket'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -543,12 +543,12 @@ Socket.prototype.sendFD = function(socketToPass) { |
|
|
|
|
|
|
|
|
// Flushes the write buffer out.
|
|
|
// Flushes the write buffer out.
|
|
|
// Returns true if the entire buffer was flushed.
|
|
|
// Returns true if the entire buffer was flushed.
|
|
|
Socket.prototype.flush = function () { |
|
|
Stream.prototype.flush = function () { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var bytesWritten; |
|
|
var bytesWritten; |
|
|
while (self._writeQueue.length) { |
|
|
while (self._writeQueue.length) { |
|
|
if (!self.writable) throw new Error('Socket is not writable'); |
|
|
if (!self.writable) throw new Error('Stream is not writable'); |
|
|
|
|
|
|
|
|
var b = self._writeQueue[0]; |
|
|
var b = self._writeQueue[0]; |
|
|
|
|
|
|
|
@ -638,14 +638,14 @@ function doConnect (socket, port, host) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// var stream = new Socket();
|
|
|
// var stream = new Stream();
|
|
|
// stream.connect(80) - TCP connect to port 80 on the localhost
|
|
|
// stream.connect(80) - TCP connect to port 80 on the localhost
|
|
|
// stream.connect(80, 'nodejs.org') - TCP connect to port 80 on nodejs.org
|
|
|
// stream.connect(80, 'nodejs.org') - TCP connect to port 80 on nodejs.org
|
|
|
// stream.connect('/tmp/socket') - UNIX connect to socket specified by path
|
|
|
// stream.connect('/tmp/socket') - UNIX connect to socket specified by path
|
|
|
Socket.prototype.connect = function () { |
|
|
Stream.prototype.connect = function () { |
|
|
var self = this; |
|
|
var self = this; |
|
|
initSocket(self); |
|
|
initStream(self); |
|
|
if (self.fd) throw new Error('Socket already opened'); |
|
|
if (self.fd) throw new Error('Stream already opened'); |
|
|
if (!self._readWatcher) throw new Error('No readWatcher'); |
|
|
if (!self._readWatcher) throw new Error('No readWatcher'); |
|
|
|
|
|
|
|
|
timeout.active(socket); |
|
|
timeout.active(socket); |
|
@ -670,34 +670,34 @@ Socket.prototype.connect = function () { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.address = function () { |
|
|
Stream.prototype.address = function () { |
|
|
return getsockname(this.fd); |
|
|
return getsockname(this.fd); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setNoDelay = function (v) { |
|
|
Stream.prototype.setNoDelay = function (v) { |
|
|
if (this.type == 'tcp') setNoDelay(this.fd, v); |
|
|
if (this.type == 'tcp') setNoDelay(this.fd, v); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setTimeout = function (msecs) { |
|
|
Stream.prototype.setTimeout = function (msecs) { |
|
|
timeout.enroll(this, msecs); |
|
|
timeout.enroll(this, msecs); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.pause = function () { |
|
|
Stream.prototype.pause = function () { |
|
|
this._readWatcher.stop(); |
|
|
this._readWatcher.stop(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.resume = function () { |
|
|
Stream.prototype.resume = function () { |
|
|
if (this.fd === null) throw new Error('Cannot resume() closed Socket.'); |
|
|
if (this.fd === null) throw new Error('Cannot resume() closed Stream.'); |
|
|
this._readWatcher.set(this.fd, true, false); |
|
|
this._readWatcher.set(this.fd, true, false); |
|
|
this._readWatcher.start(); |
|
|
this._readWatcher.start(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.forceClose = function (exception) { |
|
|
Stream.prototype.forceClose = function (exception) { |
|
|
// recvBuffer is shared between sockets, so don't need to free it here.
|
|
|
// recvBuffer is shared between sockets, so don't need to free it here.
|
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
@ -734,7 +734,7 @@ Socket.prototype.forceClose = function (exception) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype._shutdown = function () { |
|
|
Stream.prototype._shutdown = function () { |
|
|
if (this.writable) { |
|
|
if (this.writable) { |
|
|
this.writable = false; |
|
|
this.writable = false; |
|
|
|
|
|
|
|
@ -750,7 +750,7 @@ Socket.prototype._shutdown = function () { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.close = function () { |
|
|
Stream.prototype.close = function () { |
|
|
if (this.writable) { |
|
|
if (this.writable) { |
|
|
if (this.__writeQueueLast() != END_OF_FILE) { |
|
|
if (this.__writeQueueLast() != END_OF_FILE) { |
|
|
this._writeQueue.push(END_OF_FILE); |
|
|
this._writeQueue.push(END_OF_FILE); |
|
@ -775,7 +775,7 @@ function Server (listener) { |
|
|
var peerInfo = accept(self.fd); |
|
|
var peerInfo = accept(self.fd); |
|
|
if (!peerInfo) return; |
|
|
if (!peerInfo) return; |
|
|
|
|
|
|
|
|
var s = new Socket(peerInfo.fd); |
|
|
var s = new Stream(peerInfo.fd); |
|
|
s.remoteAddress = peerInfo.remoteAddress; |
|
|
s.remoteAddress = peerInfo.remoteAddress; |
|
|
s.remotePort = peerInfo.remotePort; |
|
|
s.remotePort = peerInfo.remotePort; |
|
|
s.type = self.type; |
|
|
s.type = self.type; |
|
|