Fixes #10, socket timeout now refreshes on data

This commit is contained in:
Andy Prock 2016-12-02 11:33:01 -08:00
parent f641b5e6db
commit a88d5bea7f
2 changed files with 44 additions and 20 deletions

View File

@ -112,6 +112,12 @@ TcpSocket.prototype.connect = function(options, callback) : TcpSocket {
} }
} }
if (options.timeout) {
this.setTimeout(options.timeout);
} else if (this._timeout) {
this._activeTimer(this._timeout.msecs);
}
this._state = STATE.CONNECTING; this._state = STATE.CONNECTING;
this._debug('connecting, host:', host, 'port:', port); this._debug('connecting, host:', host, 'port:', port);
@ -155,27 +161,48 @@ TcpSocket.prototype._read = function(n) {
} }
}; };
TcpSocket.prototype._activeTimer = function(msecs, wrapper) {
TcpSocket.prototype.setTimeout = function(msecs: number, callback: () => void) { if (this._timeout && this._timeout.handle) {
var self = this; clearTimeout(this._timeout.handle);
if (this._timeout) {
clearTimeout(this._timeout);
this._timeout = null;
} }
if (msecs > 0) { if (!wrapper) {
var self = this;
wrapper = function() {
self._timeout = null;
self.emit('timeout');
};
}
this._timeout = {
handle: setTimeout(wrapper, msecs),
wrapper: wrapper,
msecs: msecs
};
};
TcpSocket.prototype._clearTimeout = function() {
if (this._timeout) {
clearTimeout(this._timeout.handle);
this._timeout = null;
}
};
TcpSocket.prototype.setTimeout = function(msecs: number, callback: () => void) {
if (msecs === 0) {
this._clearTimeout();
if (callback) {
this.removeListener('timeout', callback);
}
} else {
if (callback) { if (callback) {
this.once('timeout', callback); this.once('timeout', callback);
} }
var self = this; this._activeTimer(msecs);
this._timeout = setTimeout(function() {
self.emit('timeout');
self._timeout = null;
self.destroy();
}, msecs);
} }
return this;
}; };
TcpSocket.prototype.address = function() : { port: number, address: string, family: string } { TcpSocket.prototype.address = function() : { port: number, address: string, family: string } {
@ -209,6 +236,7 @@ TcpSocket.prototype.destroy = function() {
if (!this._destroyed) { if (!this._destroyed) {
this._destroyed = true; this._destroyed = true;
this._debug('destroying'); this._debug('destroying');
this._clearTimeout();
Sockets.destroy(this._id); Sockets.destroy(this._id);
} }
@ -268,8 +296,7 @@ TcpSocket.prototype._onData = function(data: string): void {
this._debug('received', 'data'); this._debug('received', 'data');
if (this._timeout) { if (this._timeout) {
clearTimeout(this._timeout); this._activeTimer(this._timeout.msecs);
this._timeout = null;
} }
if (data && data.length > 0) { if (data && data.length > 0) {
@ -335,8 +362,7 @@ TcpSocket.prototype._write = function(buffer: any, encoding: ?String, callback:
Sockets.write(this._id, str, function(err) { Sockets.write(this._id, str, function(err) {
if (self._timeout) { if (self._timeout) {
clearTimeout(self._timeout); self._activeTimer(this._timeout.msecs);
self._timeout = null;
} }
err = normalizeError(err); err = normalizeError(err);

View File

@ -14,8 +14,6 @@ import {
View View
} from 'react-native'; } from 'react-native';
global.Buffer = global.Buffer || require('buffer').Buffer;
var net = require('net'); var net = require('net');
function randomPort() { function randomPort() {