|
|
@ -248,7 +248,7 @@ function allocRecvBuffer () { |
|
|
|
|
|
|
|
function _doFlush () { |
|
|
|
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
|
|
|
|
if ((socket._writeQueueSize == 0) && (socket._writeMessageQueueSize == 0)) { |
|
|
|
return; |
|
|
@ -262,7 +262,7 @@ function _doFlush () { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function initSocket (self) { |
|
|
|
function initStream (self) { |
|
|
|
self._readWatcher = ioWatchers.alloc(); |
|
|
|
self._readWatcher.callback = function () { |
|
|
|
// 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; |
|
|
|
} |
|
|
|
|
|
|
|
function Socket (fd) { |
|
|
|
function Stream (fd) { |
|
|
|
events.EventEmitter.call(this); |
|
|
|
|
|
|
|
this.fd = null; |
|
|
@ -370,12 +370,12 @@ function Socket (fd) { |
|
|
|
this.open(fd); |
|
|
|
} |
|
|
|
}; |
|
|
|
sys.inherits(Socket, events.EventEmitter); |
|
|
|
exports.Socket = Socket; |
|
|
|
sys.inherits(Stream, events.EventEmitter); |
|
|
|
exports.Stream = Stream; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.open = function (fd) { |
|
|
|
initSocket(this); |
|
|
|
Stream.prototype.open = function (fd) { |
|
|
|
initStream(this); |
|
|
|
|
|
|
|
this.fd = fd; |
|
|
|
|
|
|
@ -387,14 +387,14 @@ Socket.prototype.open = function (fd) { |
|
|
|
|
|
|
|
|
|
|
|
exports.createConnection = function (port, host) { |
|
|
|
var s = new Socket(); |
|
|
|
var s = new Stream(); |
|
|
|
s.connect(port, host); |
|
|
|
return s; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var readyStateMessage; |
|
|
|
Object.defineProperty(Socket.prototype, 'readyState', { |
|
|
|
Object.defineProperty(Stream.prototype, 'readyState', { |
|
|
|
get: function () { |
|
|
|
if (!readyStateMessage) { |
|
|
|
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); |
|
|
|
b.used = 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; |
|
|
|
if (!self.writable) throw new Error('Socket is not writable'); |
|
|
|
if (!self.writable) throw new Error('Stream is not writable'); |
|
|
|
var buffer; |
|
|
|
|
|
|
|
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] |
|
|
|
: null; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.send = function () { |
|
|
|
Stream.prototype.send = function () { |
|
|
|
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?
|
|
|
|
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
|
|
|
|
// something was queued. If data was queued, then the "drain" event will
|
|
|
|
// signal when it has been finally flushed to socket.
|
|
|
|
Socket.prototype.write = function (data, encoding) { |
|
|
|
Stream.prototype.write = function (data, encoding) { |
|
|
|
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) { |
|
|
|
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
|
|
|
|
Socket.prototype.sendFD = function(socketToPass) { |
|
|
|
Stream.prototype.sendFD = function(socketToPass) { |
|
|
|
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) { |
|
|
|
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'); |
|
|
|
} |
|
|
|
|
|
|
|
if (! socketToPass instanceof Socket) { |
|
|
|
if (! socketToPass instanceof Stream) { |
|
|
|
throw new Error('Provided arg is not a socket'); |
|
|
|
} |
|
|
|
|
|
|
@ -543,12 +543,12 @@ Socket.prototype.sendFD = function(socketToPass) { |
|
|
|
|
|
|
|
// Flushes the write buffer out.
|
|
|
|
// Returns true if the entire buffer was flushed.
|
|
|
|
Socket.prototype.flush = function () { |
|
|
|
Stream.prototype.flush = function () { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
var bytesWritten; |
|
|
|
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]; |
|
|
|
|
|
|
@ -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, 'nodejs.org') - TCP connect to port 80 on nodejs.org
|
|
|
|
// stream.connect('/tmp/socket') - UNIX connect to socket specified by path
|
|
|
|
Socket.prototype.connect = function () { |
|
|
|
Stream.prototype.connect = function () { |
|
|
|
var self = this; |
|
|
|
initSocket(self); |
|
|
|
if (self.fd) throw new Error('Socket already opened'); |
|
|
|
initStream(self); |
|
|
|
if (self.fd) throw new Error('Stream already opened'); |
|
|
|
if (!self._readWatcher) throw new Error('No readWatcher'); |
|
|
|
|
|
|
|
timeout.active(socket); |
|
|
@ -670,34 +670,34 @@ Socket.prototype.connect = function () { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.address = function () { |
|
|
|
Stream.prototype.address = function () { |
|
|
|
return getsockname(this.fd); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setNoDelay = function (v) { |
|
|
|
Stream.prototype.setNoDelay = function (v) { |
|
|
|
if (this.type == 'tcp') setNoDelay(this.fd, v); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setTimeout = function (msecs) { |
|
|
|
Stream.prototype.setTimeout = function (msecs) { |
|
|
|
timeout.enroll(this, msecs); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.pause = function () { |
|
|
|
Stream.prototype.pause = function () { |
|
|
|
this._readWatcher.stop(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.resume = function () { |
|
|
|
if (this.fd === null) throw new Error('Cannot resume() closed Socket.'); |
|
|
|
Stream.prototype.resume = function () { |
|
|
|
if (this.fd === null) throw new Error('Cannot resume() closed Stream.'); |
|
|
|
this._readWatcher.set(this.fd, true, false); |
|
|
|
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.
|
|
|
|
var self = this; |
|
|
|
|
|
|
@ -734,7 +734,7 @@ Socket.prototype.forceClose = function (exception) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype._shutdown = function () { |
|
|
|
Stream.prototype._shutdown = function () { |
|
|
|
if (this.writable) { |
|
|
|
this.writable = false; |
|
|
|
|
|
|
@ -750,7 +750,7 @@ Socket.prototype._shutdown = function () { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.close = function () { |
|
|
|
Stream.prototype.close = function () { |
|
|
|
if (this.writable) { |
|
|
|
if (this.__writeQueueLast() != END_OF_FILE) { |
|
|
|
this._writeQueue.push(END_OF_FILE); |
|
|
@ -775,7 +775,7 @@ function Server (listener) { |
|
|
|
var peerInfo = accept(self.fd); |
|
|
|
if (!peerInfo) return; |
|
|
|
|
|
|
|
var s = new Socket(peerInfo.fd); |
|
|
|
var s = new Stream(peerInfo.fd); |
|
|
|
s.remoteAddress = peerInfo.remoteAddress; |
|
|
|
s.remotePort = peerInfo.remotePort; |
|
|
|
s.type = self.type; |
|
|
|