From e697cfb6fc020c08afa2c794ee5802e5a4d2b97b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 17 Jun 2011 14:27:02 +0200 Subject: [PATCH] net_uv: shim up more methods --- lib/net_uv.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/lib/net_uv.js b/lib/net_uv.js index 19f2d74af2db33..d637f0c6eb1b29 100644 --- a/lib/net_uv.js +++ b/lib/net_uv.js @@ -14,13 +14,25 @@ if (process.env.NODE_DEBUG && /net/.test(process.env.NODE_DEBUG)) { } +exports.createServer = function() { + return new Server(arguments[0], arguments[1]); +}; + + +exports.connect = exports.createConnection = function(port, host) { + var s = new Socket(); + s.connect(port, host); + return s; +}; + + function Socket(options) { if (!(this instanceof Socket)) return new Socket(options); stream.Stream.call(this); // private - if (options.handle) { + if (options && options.handle) { this._handle = options.handle; } else { this._handle = new TCP(); @@ -46,6 +58,39 @@ Socket.prototype.setTimeout = function(msecs, callback) { }; +Socket.prototype.setNoDelay = function() { + /* TODO implement me */ +}; + + +Object.defineProperty(Socket.prototype, 'readyState', { + get: function() { + if (this._connecting) { + return 'opening'; + } else if (this.readable && this.writable) { + assert(typeof this.fd === 'number'); + return 'open'; + } else if (this.readable && !this.writable) { + assert(typeof this.fd === 'number'); + return 'readOnly'; + } else if (!this.readable && this.writable) { + assert(typeof this.fd === 'number'); + return 'writeOnly'; + } else { + assert(typeof this.fd !== 'number'); + return 'closed'; + } + } +}); + + +Object.defineProperty(Socket.prototype, 'bufferSize', { + get: function() { + return this._handle.writeQueueSize; + } +}); + + Socket.prototype.pause = function() { this._handle.readStop(); }; @@ -204,6 +249,8 @@ Socket.prototype.connect = function(port, host) { throw new Error("ipv6 addresses not yet supported by libuv"); } + ip = ip || '127.0.0.1'; + self.remoteAddress = ip; self.remotePort = port; @@ -214,11 +261,11 @@ Socket.prototype.connect = function(port, host) { self._connectReq = self._handle.connect(ip, port); - if (!self._connectReq) { + if (self._connectReq) { + self._connectReq.oncomplete = afterConnect; + } else { self.destroy(errnoException(errno, 'connect')); } - - self._connectReq.oncomplete = afterConnect; } }); }; @@ -330,8 +377,12 @@ function onconnection(clientHandle) { var self = handle.socket; var socket = new Socket({ handle: clientHandle }); + socket.readable = socket.writable = true; socket.resume(); + self.connections++; + socket.server = self; + DTRACE_NET_SERVER_CONNECTION(socket); self.emit('connection', socket); }