Files
node-screenlogic/messages/SLMessage.js
Parnic b6ee816fb3 Added decoding of messages
Also moved things out to their own files and setup require()s to bring it all together.
2018-03-30 16:07:56 -05:00

58 lines
1.2 KiB
JavaScript
Executable File

const SmartBuffer = require('smart-buffer').SmartBuffer;
exports.SLMessage = class SLMessage extends SmartBuffer {
constructor(senderId, messageId) {
super();
this.writeUInt16LE(senderId);
this.writeUInt16LE(messageId);
this._wroteSize = false;
}
toBuffer() {
if (this._wroteSize === false) {
this.insertInt32LE(this.length - 4, 4);
this._wroteSize = true;
} else {
this.writeInt32LE(this.length - 8, 4);
}
return super.toBuffer();
}
writeSLString(str) {
this.writeInt32LE(str.length);
this.writeString(str);
if (str.length % 4 != 0) {
this.skipWrite(4 - (str.length % 4));
}
}
readSLString() {
var len = this.readInt32LE();
var str = this.readString(len);
if (len % 4 != 0) {
this.readOffset += 4 - (len % 4);
}
return str;
}
writeSLBuffer(buf) {
this.writeInt32LE(buf.length);
this.writeBuffer(buf);
}
skipWrite(num) {
if (num > 0) {
this.writeBuffer(Buffer.alloc(num));
}
}
decode() {
this.readOffset = 0;
this.senderId = this.readUInt16LE();
this.messageId = this.readUInt16LE();
this.dataLength = this.readInt32LE();
}
}