From 4840ffe7b14cc0640683024421ed88dec58aaafa Mon Sep 17 00:00:00 2001 From: deathcap Date: Sat, 30 Jan 2016 15:20:53 -0800 Subject: [PATCH] Splitter prepends length header to legacy ping for deserializer support --- src/transforms/framing.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/transforms/framing.js b/src/transforms/framing.js index a447137..38d7eb4 100644 --- a/src/transforms/framing.js +++ b/src/transforms/framing.js @@ -23,6 +23,8 @@ class Framer extends Transform { } } +const LEGACY_PING_PACKET_ID = 0xfe; + class Splitter extends Transform { constructor() { super(); @@ -31,9 +33,15 @@ class Splitter extends Transform { _transform(chunk, enc, cb) { this.buffer = Buffer.concat([this.buffer, chunk]); - if (this.buffer[0] === 0xfe) { - // legacy_server_list_ping packet follows a different protocol format, no varint length - this.push(this.buffer); + // TODO: only decode if in handshake state! important since 254 is a valid varint (encodes as 0xfe 0x01), packet length + if (this.buffer[0] === LEGACY_PING_PACKET_ID) { + // legacy_server_list_ping packet follows a different protocol format + // prefix the encoded varint packet id for the deserializer + var header = new Buffer(sizeOfVarInt(LEGACY_PING_PACKET_ID)); + writeVarInt(LEGACY_PING_PACKET_ID, header, 0); + var payload = this.buffer.slice(1); // remove 0xfe packet id + if (payload.length === 0) payload = new Buffer('\0'); // TODO: update minecraft-data to recognize a lone 0xfe, https://github.com/PrismarineJS/minecraft-data/issues/95 + this.push(Buffer.concat([header, payload])); return cb(); }