From 16e32c8fd969847719d0b612439197c470b5eb67 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 26 Feb 2010 12:13:49 -0800 Subject: [PATCH] Allow for net reconnects --- lib/net.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/net.js b/lib/net.js index 21e114f410..74ed72dd3c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -71,11 +71,7 @@ function allocRecvBuffer () { recvBuffer.used = 0; } -function Socket (peerInfo) { - process.EventEmitter.call(this); - - var self = this; - +function initSocket (self) { self._readWatcher = ioWatchers.alloc(); self._readWatcher.callback = function () { // 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.callback = self._doFlush; self.writable = false; +} + +function Socket (peerInfo) { + process.EventEmitter.call(this); if (peerInfo) { - self.fd = peerInfo.fd; - self.remoteAddress = peerInfo.remoteAddress; - self.remotePort = peerInfo.remotePort; + initSocket(this); - self._readWatcher.set(self.fd, true, false); - self._readWatcher.start(); - self.readable = true; + this.fd = peerInfo.fd; + this.remoteAddress = peerInfo.remoteAddress; + this.remotePort = peerInfo.remotePort; - self._writeWatcher.set(self.fd, false, true); - self.writable = true; + this._readWatcher.set(this.fd, true, false); + this._readWatcher.start(); + this.readable = true; + + this._writeWatcher.set(this.fd, false, true); + this.writable = true; } }; 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('/tmp/socket') - UNIX connect to socket specified by path Socket.prototype.connect = function () { + initSocket(this); + var self = this; if (self.fd) throw new Error('Socket already opened');