Performance improvement by using strict equality, fixed heartbeat issue in connection stage

This commit is contained in:
Nicolas Dextraze
2016-10-17 21:58:28 -07:00
parent dd1302f641
commit f951a625f4
22 changed files with 107 additions and 98 deletions

View File

@ -29,7 +29,7 @@ LengthPrefixMessageFramer.prototype._parse = function(bytes) {
{
this._packageLength |= (buffer[i] << (this._headerBytes * 8)); // little-endian order
++this._headerBytes;
if (this._headerBytes == HeaderLength)
if (this._headerBytes === HeaderLength)
{
if (this._packageLength <= 0 || this._packageLength > this._maxPackageSize)
throw new Error(["Package size is out of bounds: ", this._packageLength, "(max: ", this._maxPackageSize, "."].join(''));
@ -44,9 +44,9 @@ LengthPrefixMessageFramer.prototype._parse = function(bytes) {
this._bufferIndex += copyCnt;
i += copyCnt - 1;
if (this._bufferIndex == this._packageLength)
if (this._bufferIndex === this._packageLength)
{
if (this._receivedHandler != null)
if (this._receivedHandler !== null)
this._receivedHandler(createBufferSegment(this._messageBuffer, 0, this._bufferIndex));
this.reset();
}

View File

@ -32,6 +32,7 @@ function TcpConnection(log, connectionId, remoteEndPoint, onConnectionClosed) {
TcpConnection.prototype._initSocket = function(socket) {
this._socket = socket;
this._localEndPoint = {host: socket.localAddress, port: socket.localPort};
this._remoteEndPoint.host = socket.remoteAddress;
this._socket.on('error', this._processError.bind(this));
this._socket.on('data', this._processReceive.bind(this));
@ -120,13 +121,13 @@ TcpConnection.prototype._closeInternal = function(err, reason) {
if (this._closed) return;
this._closed = true;
if (this._socket != null) {
if (this._socket !== null) {
this._socket.end();
this._socket.unref();
this._socket = null;
}
if (this._onConnectionClosed != null)
if (this._onConnectionClosed !== null)
this._onConnectionClosed(this, err);
};

View File

@ -120,26 +120,26 @@ TcpPackageConnection.prototype._incomingMessageArrived = function(data) {
var message = util.format("TcpPackageConnection: [%j, L%j, %s] ERROR for %s. Connection will be closed.",
this.remoteEndPoint, this.localEndPoint, this._connectionId,
valid ? TcpCommand.getName(pkg.command) : "<invalid package>");
if (this._onError != null)
if (this._onError !== null)
this._onError(this, e);
this._log.debug(e, message);
}
};
TcpPackageConnection.prototype.startReceiving = function() {
if (this._connection == null)
if (this._connection === null)
throw new Error("Failed connection.");
this._connection.receive(this._onRawDataReceived.bind(this));
};
TcpPackageConnection.prototype.enqueueSend = function(pkg) {
if (this._connection == null)
if (this._connection === null)
throw new Error("Failed connection.");
this._connection.enqueueSend(this._framer.frameData(pkg.asBufferSegment()));
};
TcpPackageConnection.prototype.close = function(reason) {
if (this._connection == null)
if (this._connection === null)
throw new Error("Failed connection.");
this._connection.close(reason);
};