Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #22 from albertosantini/fix-issue20
Browse files Browse the repository at this point in the history
fix coalescing tcp packets
  • Loading branch information
nkcmr authored Dec 11, 2016
2 parents 8ed447d + 119d03c commit 3c03e5c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
53 changes: 32 additions & 21 deletions lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,43 @@ Bridge.prototype._handle_connection = function (socket) {
}

Bridge.prototype._handle_data = function (socket, rawData) {
debug('node --> bridge: %s', rawData)
var self = this

try {
var message = JSON.parse(rawData)
var events = rawData.split('\0')

events.forEach(function (event) {
if (!event) {
return
}

debug('node --> bridge: %s', event)
var message = JSON.parse(event)

switch (_.get(message, 'method')) {
case IDENT:
self._handle_ident_message(socket, message)
break
case TELL:
self._handle_tell_message(socket, message)
break
case SHOUT:
self._handle_shout_message(socket, message)
break
case ACK:
self._handle_ack_message(socket, message)
break
case LEAVE:
self._handle_leave_message(socket, message)
break
default:
debug('received unrecognized command: %s', _.get(message, 'method'))
}
})
} catch (e) {
debug('bridge received invalid data, ignoring...')
return
}
switch (_.get(message, 'method')) {
case IDENT:
this._handle_ident_message(socket, message)
break
case TELL:
this._handle_tell_message(socket, message)
break
case SHOUT:
this._handle_shout_message(socket, message)
break
case ACK:
this._handle_ack_message(socket, message)
break
case LEAVE:
this._handle_leave_message(socket, message)
break
default:
debug('received unrecognized command: %s', _.get(message, 'method'))
}
}

Bridge.prototype._handle_ident_message = function (socket, message) {
Expand Down
2 changes: 1 addition & 1 deletion lib/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Message.prototype.toString = function () {
out.nodeName = this.nodeName
}

return JSON.stringify(out)
return JSON.stringify(out) + '\0'
}

module.exports = Message
43 changes: 26 additions & 17 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,38 @@ Node.prototype._handle_socket_error = function (e) {
}

Node.prototype._handle_data = function (rawData) {
debug('node <-- bridge: %s', rawData)
var self = this

try {
var message = JSON.parse(rawData)
var events = rawData.toString().split('\0')

events.forEach(function (event) {
if (!event) {
return
}

debug('node <-- bridge: %s', event)
var message = JSON.parse(event)

switch (_.get(message, 'method')) {
case ACK:
self._handle_ack_message(message)
break
case TELL:
self._handle_tell_message(message)
break
case SHOUT:
self._handle_shout_message(message)
break
case CLOSE:
self._handle_close_message(message)
break
}
})
} catch (e) {
debug('node received invalid data, ignoring...')
return
}

switch (_.get(message, 'method')) {
case ACK:
this._handle_ack_message(message)
break
case TELL:
this._handle_tell_message(message)
break
case SHOUT:
this._handle_shout_message(message)
break
case CLOSE:
this._handle_close_message(message)
break
}
}

Node.prototype._handle_ack_message = function (message) {
Expand Down

0 comments on commit 3c03e5c

Please sign in to comment.