-
Notifications
You must be signed in to change notification settings - Fork 63
refactor TCP Server to close open connections, refactor TCP Client to handle multiple messages and close event #105
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
base: v4.0.1
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,27 +30,33 @@ TcpClient.prototype.connect = function(callback) { | |
| self.responseBuffer += data.toString(); | ||
| if (self.responseBuffer.substring(self.responseBuffer.length - 2, self.responseBuffer.length) == FS + CR) { | ||
| var ack = self.parser.parse(self.responseBuffer.substring(1, self.responseBuffer.length - 2)); | ||
| self.callback(null, ack); | ||
| self.responseBuffer = ""; | ||
| self.awaitingResponse = false; | ||
| if (!self.keepalive) { | ||
| self.close(); | ||
| } | ||
| self.callback(null, ack); | ||
| } | ||
| }); | ||
| callback(); | ||
| }); | ||
| self.client.on('close', function() { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On server side close, it would not clean up |
||
| self.responseBuffer = ""; | ||
| self.awaitingResponse = false; | ||
| delete self.client; | ||
| }) | ||
| self.client.on('error', function(err) { | ||
| callback(err); | ||
| }) | ||
| } | ||
|
|
||
| TcpClient.prototype.send = function(msg, callback) { | ||
| var self = this; | ||
| self.callback = callback || self.callback; | ||
| if (self.awaitingResponse) { | ||
| self.callback(new Error("Can't send while awaiting response")); | ||
| callback(new Error("Can't send while awaiting response")); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replaced the callback, so that the first message callback didn't receive the ack, but the second one did |
||
| return; | ||
| } | ||
| self.callback = callback || self.callback; | ||
| self.awaitingResponse = true; | ||
| try { | ||
| if (self.client) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ function TcpServer(options, handler) { | |
|
|
||
| this.handler = handler; | ||
| this.server = null; | ||
| this.socket = null; | ||
| this.sockets = []; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added socket tracking |
||
| this.parser = options.parser || new Parser(); | ||
| } | ||
|
|
||
|
|
@@ -43,17 +43,24 @@ function Req(msg, raw) { | |
| function Res(socket, ack) { | ||
| this.ack = ack; | ||
|
|
||
| this.socket = socket; | ||
| this.end = function() { | ||
| socket.write(VT + (this.ack).toString() + FS + CR); | ||
| } | ||
| } | ||
|
|
||
| TcpServer.prototype.start = function(port, encoding, options) { | ||
| TcpServer.prototype.start = function(port, encoding) { | ||
| var self = this; | ||
| options = options || {} | ||
| this.server = net.createServer(function(socket) { | ||
| var message = ""; | ||
|
|
||
| self.sockets.push(socket); | ||
| socket.on('close', function () { | ||
| const index = self.sockets.indexOf(socket); | ||
| if (index >= 0) { | ||
| self.sockets.splice(index, 1); | ||
| } | ||
| }); | ||
| socket.on('data', function(data) { | ||
| try { | ||
| message += data.toString(); | ||
|
|
@@ -79,7 +86,25 @@ TcpServer.prototype.start = function(port, encoding, options) { | |
| this.server.listen(port); | ||
| } | ||
|
|
||
| TcpServer.prototype.stop = function() { | ||
| TcpServer.prototype.stop = function (closeConnectionsCallback) { | ||
| var self = this; | ||
| if (closeConnectionsCallback) { | ||
| var promises = []; | ||
| this.sockets.forEach(function (socket) { | ||
| promises.push( | ||
| new Promise(function (resolve) { | ||
| socket.end(resolve); | ||
| }), | ||
| ); | ||
| }); | ||
| Promise.all(promises).then(function () { | ||
| self.server.close(function () { | ||
| self.sockets = []; | ||
| closeConnectionsCallback(); | ||
| }); | ||
| }); | ||
| return; | ||
| } | ||
| this.server.close(); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't allow another message to be queued up right after the first one completed