Skip to content

Commit

Permalink
net_uv: shim up more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 17, 2011
1 parent 52b517c commit e697cfb
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions lib/net_uv.js
Expand Up @@ -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();
Expand All @@ -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();
};
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
});
};
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit e697cfb

Please sign in to comment.