Allow for net reconnects

This commit is contained in:
Ryan Dahl 2010-02-26 12:13:49 -08:00
parent 71d237e6a0
commit 16e32c8fd9

View File

@ -71,11 +71,7 @@ function allocRecvBuffer () {
recvBuffer.used = 0; recvBuffer.used = 0;
} }
function Socket (peerInfo) { function initSocket (self) {
process.EventEmitter.call(this);
var self = this;
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
@ -163,18 +159,24 @@ function Socket (peerInfo) {
self._writeWatcher = ioWatchers.alloc(); self._writeWatcher = ioWatchers.alloc();
self._writeWatcher.callback = self._doFlush; self._writeWatcher.callback = self._doFlush;
self.writable = false; self.writable = false;
}
function Socket (peerInfo) {
process.EventEmitter.call(this);
if (peerInfo) { if (peerInfo) {
self.fd = peerInfo.fd; initSocket(this);
self.remoteAddress = peerInfo.remoteAddress;
self.remotePort = peerInfo.remotePort;
self._readWatcher.set(self.fd, true, false); this.fd = peerInfo.fd;
self._readWatcher.start(); this.remoteAddress = peerInfo.remoteAddress;
self.readable = true; this.remotePort = peerInfo.remotePort;
self._writeWatcher.set(self.fd, false, true); this._readWatcher.set(this.fd, true, false);
self.writable = true; this._readWatcher.start();
this.readable = true;
this._writeWatcher.set(this.fd, false, true);
this.writable = true;
} }
}; };
process.inherits(Socket, process.EventEmitter); process.inherits(Socket, process.EventEmitter);
@ -376,6 +378,8 @@ Socket.prototype.flush = function () {
// 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 () { Socket.prototype.connect = function () {
initSocket(this);
var self = this; var self = this;
if (self.fd) throw new Error('Socket already opened'); if (self.fd) throw new Error('Socket already opened');