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:
57
messages/SLMessage.js
Executable file
57
messages/SLMessage.js
Executable 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user