net: expose more of the tcp interface.

This commit is contained in:
Christopher Jeffrey 2017-02-03 09:46:05 -08:00
parent f20aae676a
commit 91f36b3d1c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 102 additions and 1 deletions

View File

@ -213,6 +213,10 @@ WSProxy.prototype._handleConnect = function _handleConnect(ws, port, host, nonce
}); });
}); });
socket.on('timeout', function() {
ws.emit('tcp timeout');
});
socket.on('close', function() { socket.on('close', function() {
self.log('Closing %s (%s).', state.remoteHost, state.host); self.log('Closing %s (%s).', state.remoteHost, state.host);
ws.emit('tcp close'); ws.emit('tcp close');
@ -225,6 +229,26 @@ WSProxy.prototype._handleConnect = function _handleConnect(ws, port, host, nonce
socket.write(new Buffer(data, 'hex')); socket.write(new Buffer(data, 'hex'));
}); });
ws.on('tcp keep alive', function(enable, delay) {
socket.setKeepAlive(enable, delay);
});
ws.on('tcp no delay', function(enable) {
socket.setNoDelay(enable);
});
ws.on('tcp set timeout', function(timeout) {
socket.setTimeout(timeout);
});
ws.on('tcp pause', function() {
socket.pause();
});
ws.on('tcp resume', function() {
socket.resume();
});
ws.on('disconnect', function() { ws.on('disconnect', function() {
socket.destroy(); socket.destroy();
}); });

View File

@ -387,6 +387,8 @@ Peer.prototype.bind = function bind(socket) {
self.lastRecv = util.ms(); self.lastRecv = util.ms();
self.feedParser(chunk); self.feedParser(chunk);
}); });
this.socket.setNoDelay(true);
}; };
/** /**

View File

@ -395,6 +395,8 @@ Pool.prototype.listen = co(function* listen() {
if (!this.options.listen) if (!this.options.listen)
return; return;
this.server.maxConnections = this.options.maxInbound;
yield this.server.listen(this.options.port, this.options.host); yield this.server.listen(this.options.port, this.options.host);
}); });

View File

@ -84,6 +84,10 @@ ProxySocket.prototype._init = function _init() {
self.emit('error', err); self.emit('error', err);
}); });
this.socket.on('tcp timeout', function() {
self.emit('timeout');
});
this.socket.on('disconnect', function() { this.socket.on('disconnect', function() {
if (self.closed) if (self.closed)
return; return;
@ -151,6 +155,20 @@ ProxySocket.prototype.connect = function connect(port, host) {
this.sendBuffer.length = 0; this.sendBuffer.length = 0;
}; };
ProxySocket.prototype.setKeepAlive = function setKeepAlive(enable, delay) {
this.socket.emit('tcp keep alive', enable, delay);
};
ProxySocket.prototype.setNoDelay = function setNoDelay(enable) {
this.socket.emit('tcp no delay', enable);
};
ProxySocket.prototype.setTimeout = function setTimeout(timeout, callback) {
this.socket.emit('tcp set timeout', timeout);
if (callback)
this.on('timeout', callback);
};
ProxySocket.prototype.write = function write(data, callback) { ProxySocket.prototype.write = function write(data, callback) {
if (!this.info) { if (!this.info) {
this.sendBuffer.push(data); this.sendBuffer.push(data);

View File

@ -553,13 +553,14 @@ function Proxy(host, port, user, pass) {
this.bytesRead = 0; this.bytesRead = 0;
this.remoteAddress = null; this.remoteAddress = null;
this.remotePort = 0; this.remotePort = 0;
this.ops = [];
} }
util.inherits(Proxy, EventEmitter); util.inherits(Proxy, EventEmitter);
Proxy.prototype.connect = co(function* connect(port, host) { Proxy.prototype.connect = co(function* connect(port, host) {
var self = this; var self = this;
var options, socket; var i, options, socket;
assert(!this.socket, 'Already connected.'); assert(!this.socket, 'Already connected.');
@ -600,9 +601,48 @@ Proxy.prototype.connect = co(function* connect(port, host) {
self.emit('drain'); self.emit('drain');
}); });
this.socket.on('timeout', function() {
self.emit('timeout');
});
for (i = 0; i < this.ops.length; i++)
this.ops[i].call(this);
this.ops.length = 0;
this.emit('connect'); this.emit('connect');
}); });
Proxy.prototype.setKeepAlive = function setKeepAlive(enable, delay) {
if (!this.socket) {
this.ops.push(function() {
this.socket.setKeepAlive(enable, delay);
});
return;
}
this.socket.setKeepAlive(enable, delay);
};
Proxy.prototype.setNoDelay = function setNoDelay(enable) {
if (!this.socket) {
this.ops.push(function() {
this.socket.setNoDelay(enable);
});
return;
}
this.socket.setNoDelay(enable);
};
Proxy.prototype.setTimeout = function setTimeout(timeout, callback) {
if (!this.socket) {
this.ops.push(function() {
this.socket.setTimeout(timeout, callback);
});
return;
}
this.socket.setTimeout(timeout, callback);
};
Proxy.prototype.write = function write(data, callback) { Proxy.prototype.write = function write(data, callback) {
assert(this.socket, 'Not connected.'); assert(this.socket, 'Not connected.');
this.bytesWritten += data.length; this.bytesWritten += data.length;

View File

@ -16,18 +16,24 @@ tcp.createSocket = function createSocket(port, host, proxy) {
tcp.createServer = function createServer() { tcp.createServer = function createServer() {
var server = new EventEmitter(); var server = new EventEmitter();
server.listen = function listen(port, host) { server.listen = function listen(port, host) {
server.emit('listening'); server.emit('listening');
return Promise.resolve(); return Promise.resolve();
}; };
server.close = function close() { server.close = function close() {
return Promise.resolve(); return Promise.resolve();
}; };
server.address = function address() { server.address = function address() {
return { return {
address: '127.0.0.1', address: '127.0.0.1',
port: 0 port: 0
}; };
}; };
server.maxConnections = undefined;
return server; return server;
}; };

View File

@ -37,6 +37,15 @@ tcp.createServer = function createServer() {
return server.address(); return server.address();
}; };
ee.__defineGetter__('maxConnections', function() {
return server.maxConnections;
});
ee.__defineSetter__('maxConnections', function(value) {
server.maxConnections = value;
return server.maxConnections;
});
server.on('listening', function() { server.on('listening', function() {
ee.emit('listening'); ee.emit('listening');
}); });