Minor optimization to set buffer size appropriately
This avoids unnecessary allocations/reallocations while decoding a message by pre-sizing the buffer to the amount we know it will require. I feel like there's probably a better way to handle this, but this works for now.
This commit is contained in:
@ -6,7 +6,11 @@ const MSG_ID = 14;
|
||||
|
||||
exports.SLChallengeMessage = class SLChallengeMessage extends SLMessage {
|
||||
constructor(buf) {
|
||||
super(0, MSG_ID);
|
||||
var size;
|
||||
if (buf) {
|
||||
size = buf.readInt32LE(4) + 8;
|
||||
}
|
||||
super(0, MSG_ID, size);
|
||||
|
||||
if (buf) {
|
||||
this._wroteSize = true;
|
||||
|
@ -6,7 +6,12 @@ const MSG_ID = 12592;
|
||||
|
||||
exports.SLChemDataMessage = class SLChemDataMessage extends SLMessage {
|
||||
constructor(buf) {
|
||||
super(0, MSG_ID);
|
||||
var size;
|
||||
if (buf) {
|
||||
size = buf.readInt32LE(4) + 8;
|
||||
}
|
||||
super(0, MSG_ID, size);
|
||||
|
||||
if (!buf) {
|
||||
this.writeInt32LE(0); // controller index
|
||||
} else {
|
||||
|
@ -6,7 +6,12 @@ const MSG_ID = 12532;
|
||||
|
||||
exports.SLControllerConfigMessage = class SLControllerConfigMessage extends SLMessage {
|
||||
constructor(buf) {
|
||||
super(0, MSG_ID);
|
||||
var size;
|
||||
if (buf) {
|
||||
size = buf.readInt32LE(4) + 8;
|
||||
}
|
||||
super(0, MSG_ID, size);
|
||||
|
||||
if (!buf) {
|
||||
this.writeInt32LE(0);
|
||||
this.writeInt32LE(0);
|
||||
|
@ -6,7 +6,11 @@ const MSG_ID = 18003;
|
||||
|
||||
exports.SLGetGatewayDataMessage = class SLGetGatewayDataMessage extends SLMessage {
|
||||
constructor(buf) {
|
||||
super(0, MSG_ID);
|
||||
var size;
|
||||
if (buf) {
|
||||
size = buf.readInt32LE(4) + 8;
|
||||
}
|
||||
super(0, MSG_ID, size);
|
||||
|
||||
if (typeof buf === 'string') {
|
||||
this.writeSLString(buf);
|
||||
|
@ -3,8 +3,14 @@
|
||||
const SmartBuffer = require('smart-buffer').SmartBuffer;
|
||||
|
||||
exports.SLMessage = class SLMessage extends SmartBuffer {
|
||||
constructor(senderId, messageId) {
|
||||
super();
|
||||
constructor(senderId, messageId, size) {
|
||||
var options;
|
||||
if (size) {
|
||||
options = {
|
||||
size: size,
|
||||
};
|
||||
}
|
||||
super(options);
|
||||
|
||||
if (typeof senderId === 'number' || typeof senderId === 'undefined') {
|
||||
this.writeUInt16LE(senderId || 0);
|
||||
|
@ -9,7 +9,12 @@ const POOL_CIRCUIT_ID = 505;
|
||||
|
||||
exports.SLPoolStatusMessage = class SLPoolStatusMessage extends SLMessage {
|
||||
constructor(buf) {
|
||||
super(0, MSG_ID);
|
||||
var size;
|
||||
if (buf) {
|
||||
size = buf.readInt32LE(4) + 8;
|
||||
}
|
||||
super(0, MSG_ID, size);
|
||||
|
||||
if (!buf) {
|
||||
this.writeInt32LE(0);
|
||||
} else {
|
||||
|
@ -6,7 +6,12 @@ const MSG_ID = 12572;
|
||||
|
||||
exports.SLSaltCellConfigMessage = class SLSaltCellConfigMessage extends SLMessage {
|
||||
constructor(buf) {
|
||||
super(0, MSG_ID);
|
||||
var size;
|
||||
if (buf) {
|
||||
size = buf.readInt32LE(4) + 8;
|
||||
}
|
||||
super(0, MSG_ID, size);
|
||||
|
||||
if (!buf) {
|
||||
this.writeInt32LE(0); // controller index
|
||||
} else {
|
||||
|
@ -6,7 +6,12 @@ const MSG_ID = 8120;
|
||||
|
||||
exports.SLVersionMessage = class SLVersionMessage extends SLMessage {
|
||||
constructor(buf) {
|
||||
super(0, MSG_ID);
|
||||
var size;
|
||||
if (buf) {
|
||||
size = buf.readInt32LE(4) + 8;
|
||||
}
|
||||
super(0, MSG_ID, size);
|
||||
|
||||
if (buf) {
|
||||
this._wroteSize = true;
|
||||
this.writeBuffer(buf, 0);
|
||||
|
Reference in New Issue
Block a user