2017-06-27 21:23:09 +02:00
|
|
|
|
|
|
|
module.exports = class RpcMessage {
|
|
|
|
static serialize(obj) {
|
|
|
|
const serializedJson = JSON.stringify(obj);
|
|
|
|
const msgLen = 4 + serializedJson.length;
|
|
|
|
let buff = Buffer.alloc(msgLen);
|
|
|
|
buff.writeInt32LE(msgLen, 0);
|
|
|
|
buff.write(serializedJson, 4, serializedJson.length, 'utf-8');
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static deserialize(buff) {
|
|
|
|
const msgLen = buff.readInt32LE(0);
|
|
|
|
if (buff.length < msgLen) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const msg = buff.toString('utf-8', 4, msgLen);
|
|
|
|
try {
|
|
|
|
return JSON.parse(msg);
|
|
|
|
} catch(e) {
|
2017-06-29 17:58:03 +02:00
|
|
|
console.log(`failed to parse "${msg}"`);
|
|
|
|
console.error(e);
|
2017-06-27 22:19:36 +02:00
|
|
|
return {};
|
2017-06-27 21:23:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|