Added decoding of messages

Also moved things out to their own files and setup require()s to bring it all together.
This commit is contained in:
Parnic
2018-03-30 16:07:56 -05:00
parent af02f60791
commit b6ee816fb3
9 changed files with 259 additions and 86 deletions

57
messages/SLMessage.js Executable file
View File

@ -0,0 +1,57 @@
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();
}
}