8000 fixed a bunch of issues I found when trying to use nssocket by AlmirKadric · Pull Request #41 · foreversd/nssocket · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fixed a bunch of issues I found when trying to use nssocket #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 64 additions & 52 deletions lib/nssocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ NsSocket.prototype.setIdle = function setIdle(time) {
// #### forcibly destroys this nsSocket, unregister socket, remove all callbacks
//
NsSocket.prototype.destroy = function destroy() {
this._reconnect = false;
this.connected = false;

// Prevent reconnect on socket close
if (this.retry.timer) {
clearTimeout(this.retry.timer);
}

// Attempt to disconnect the socket
if (this.socket) {
try {
this.socket.end(); // send FIN
Expand All @@ -228,8 +237,15 @@ NsSocket.prototype.destroy = function destroy() {
// #### closes the underlying socket, recommend you call destroy after
//
NsSocket.prototype.end = function end() {
this._reconnect = false;
this.connected = false;

// Prevent reconnect on socket close
if (this.retry.timer) {
clearTimeout(this.retry.timer);
}

// Attempt to disconnect the socket
if (this.socket) {
try {
this.socket.end();
Expand All @@ -241,63 +257,65 @@ NsSocket.prototype.end = function end() {

this.socket = null;
}

return;
};

//
// ### function connect (port[, host, callback])
// A passthrough to the underlying socket's connect function
//
NsSocket.prototype.connect = function connect(/*port, host, callback*/) {
var args = Array.prototype.slice.call(arguments),
self = this,
callback,
host,
port;

args.forEach(function handle(arg) {
var type = typeof arg;
switch (type) {
case 'number':
port = arg;
break;
case 'string':
host = arg;
break;
case 'function':
callback = arg;
break;
default:
self.emit('error', new Error('bad argument to connect'));
break;
NsSocket.prototype.connect = function () {
var options = {};
var cb;

// Parse out arguments into options object
if (typeof arguments[0] === 'object') {
options = arguments[0];

if (typeof arguments[1] === 'function') {
cb = arguments[1];
}
} else if (typeof arguments[0] === 'number' || !isNaN(parseInt(arguments[0]))) {
options.port = arguments[0];

if (typeof arguments[1] === 'string') {
options.host = arguments[1];
} else if (typeof arguments[1] === 'function') {
cb = arguments[1];
}
});

this.port = port || this.port;
this.host = host || this.host;
this.host = this.host || '127.0.0.1';
args = this.port ? [this.port, this.host] : [this.host];
if (typeof arguments[2] === 'function') {
cb = arguments[2];
}
} else if (typeof arguments[0] === 'string') {
options.path = arguments[0];

if (typeof arguments[1] === 'function') {
cb = arguments[1];
}
}

if (callback) {
args.push(callback);
// Check if there were any connect options, if not try and load it from cache
if (Object.keys(options).length === 0 && this.connectOptions) {
options = this.connectOptions;
}

// Cache connection options for reconnect
this.connectOptions = options;

// Ensure socket type is valid
if (['tcp4', 'tls'].indexOf(this._type) === -1) {
return this.emit('error', new Error('Unknown Socket Type'));
}

var errHandlers = self.listeners('error');

// Copy the last error from nssocket onto the error event if it exists
var errHandlers = this.listeners('error');
if (errHandlers.length > 0) {
//
// copy the last error from nssocker onto the error event.
//
self.socket._events.error = errHandlers[errHandlers.length-1];
this.socket._events.error = errHandlers[errHandlers.length-1];
}

// Initiate connection
this.connected = true;
this.socket.connect.apply(this.socket, args);
this.socket.connect(options, cb);
};

//
Expand All @@ -306,51 +324,45 @@ NsSocket.prototype.connect = function connect(/*port, host, callback*/) {
// This instance will attempt to reconnect until `this.retry.max` is reached,
// with an interval increasing by powers of 10.
//
NsSocket.prototype.reconnect = function reconnect() {
NsSocket.prototype.reconnect = function () {
var self = this;

//
// Helper function containing the core reconnect logic
//
function doReconnect() {
//
// Cleanup and recreate the socket associated
// with this instance.
//
self.retry.waiting = true;
self.socket.removeAllListeners();
self.socket = common.createSocket(self._options);

//
// Cleanup reconnect logic once the socket connects
//
self.socket.once('connect', function () {
self.retry.waiting = false;
self.retry.retries = 0;
});

//
// Attempt to reconnect the socket
//
self._setup();
self.connect();
}

//

// Helper function which attempts to retry if
// it is less than the maximum
//
function tryReconnect() {
self.retry.timer = null;
self.retry.retries++;
if (self.retry.retries >= self.retry.max) {
if (self.retry.max !== true && self.retry.retries >= self.retry.max) {
return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max));
}

doReconnect();
}

this.retry.wait = this.retry.interval * Math.pow(10, this.retry.retries);
setTimeout(tryReconnect, this.retry.wait);

// TODO: IMPLEMENT EXPONENTIAL BACK-OFF
this.retry.wait = this.retry.interval;
this.retry.timer = setTimeout(tryReconnect, this.retry.wait);
};

//
Expand Down
0