Splitter prepends length header to legacy ping for deserializer support

This commit is contained in:
deathcap 2016-01-30 15:20:53 -08:00
parent c24718f64c
commit 4840ffe7b1

View File

@ -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();
}