Skip to content

Commit

Permalink
Merge pull request #29 from evs-broadcast/bugfix/remove_falsepositive…
Browse files Browse the repository at this point in the history
…_timeouts

Bugfix/remove falsepositive timeouts
  • Loading branch information
dufourgilles authored Jan 18, 2019
2 parents 67592ad + 5f6a8e7 commit d3f84fc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
45 changes: 27 additions & 18 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ S101Socket.prototype.connect = function (timeout = 2) {
self.emit('connecting');

self.codec = new S101Codec();

const connectTimeoutListener = () => {
self.socket.destroy();
self.emit("error", new Error(`Could not connect to ${self.address}:${self.port} after a timeout of ${timeout} seconds`));
};

self.socket = net.createConnection({
port: self.port,
host: self.address,
Expand All @@ -173,11 +179,14 @@ S101Socket.prototype.connect = function (timeout = 2) {
() => {
winston.debug('socket connected');

// Disable connect timeout to hand-over to keepalive mechanism
self.socket.removeListener("timeout", connectTimeoutListener);
self.socket.setTimeout(0);

self.keepaliveIntervalTimer = setInterval(() => {
try {
self.sendKeepaliveRequest();
}
catch(e) {
} catch (e) {
self.emit("error", e);
}
}, 1000 * self.keepaliveInterval);
Expand All @@ -201,22 +210,22 @@ S101Socket.prototype.connect = function (timeout = 2) {
});

self.emit('connected');
}
).on('error', (e) => {
self.emit("error", e);
}).on("timeout", () => {
self.socket.destroy();
self.emit("error", new Error(`Could not connect to ${self.address}:${self.port} after a timeout of ${timeout} seconds`));
}).on('data', (data) => {
if (self.isConnected()) {
self.codec.dataIn(data);
}
}).on('close', () => {
clearInterval(self.keepaliveIntervalTimer);
self.emit('disconnected');
self.status = "disconnected";
self.socket = null;
});
})
.on('error', (e) => {
self.emit("error", e);
})
.once("timeout", connectTimeoutListener)
.on('data', (data) => {
if (self.isConnected()) {
self.codec.dataIn(data);
}
})
.on('close', () => {
clearInterval(self.keepaliveIntervalTimer);
self.emit('disconnected');
self.status = "disconnected";
self.socket = null;
});
}

S101Socket.prototype.isConnected = function () {
Expand Down
16 changes: 16 additions & 0 deletions test/DeviceTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ describe("DeviceTree", () => {
.then(() => tree.disconnect())
})
});

it("should not disconnect after 5 seconds of inactivity", () => {
return Promise.resolve()
.then(() => {
let tree = new DeviceTree(LOCALHOST, PORT);

tree.on("error", error => {
throw error;
});

return Promise.resolve()
.then(() => tree.connect())
.then(() => new Promise(resolve => setTimeout(resolve, 5000)))
.then(() => tree.disconnect())
})
}, 7000);
});

it("timeout should be taken into account when connecting to unknown host", () => {
Expand Down

0 comments on commit d3f84fc

Please sign in to comment.