Skip to content

Commit 911e0e8

Browse files
authored
Dynamic compression & batch header (#544)
1 parent fa73078 commit 911e0e8

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

src/client.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Client extends Connection {
2626
this.compressionAlgorithm = this.versionGreaterThanOrEqualTo('1.19.30') ? 'none' : 'deflate'
2727
this.compressionThreshold = 512
2828
this.compressionLevel = this.options.compressionLevel
29+
this.batchHeader = 0xfe
2930

3031
if (isDebug) {
3132
this.inLog = (...args) => debug('C ->', ...args)

src/connection.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class Connection extends EventEmitter {
153153

154154
// These are callbacks called from encryption.js
155155
onEncryptedPacket = (buf) => {
156-
const packet = Buffer.concat([Buffer.from([0xfe]), buf]) // add header
156+
const packet = this.batchHeader ? Buffer.concat([Buffer.from([this.batchHeader]), buf]) : buf
157157
this.sendMCPE(packet)
158158
}
159159

@@ -165,7 +165,7 @@ class Connection extends EventEmitter {
165165
}
166166

167167
handle (buffer) { // handle encapsulated
168-
if (buffer[0] === 0xfe) { // wrapper
168+
if (!this.batchHeader || buffer[0] === this.batchHeader) { // wrapper
169169
if (this.encryptionEnabled) {
170170
this.decrypt(buffer.slice(1))
171171
} else {

src/server.js

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Server extends EventEmitter {
2424
this.clients = {}
2525
this.clientCount = 0
2626
this.conLog = debug
27+
this.batchHeader = 0xfe
2728

2829
this.setCompressor(this.options.compressionAlgorithm, this.options.compressionLevel, this.options.compressionThreshold)
2930
}
@@ -44,16 +45,19 @@ class Server extends EventEmitter {
4445
case 'none':
4546
this.compressionAlgorithm = 'none'
4647
this.compressionLevel = 0
48+
this.compressionHeader = 255
4749
break
4850
case 'deflate':
4951
this.compressionAlgorithm = 'deflate'
5052
this.compressionLevel = level
5153
this.compressionThreshold = threshold
54+
this.compressionHeader = 0
5255
break
5356
case 'snappy':
5457
this.compressionAlgorithm = 'snappy'
5558
this.compressionLevel = level
5659
this.compressionThreshold = threshold
60+
this.compressionHeader = 1
5761
break
5862
default:
5963
throw new Error(`Unknown compression algorithm: ${algorithm}`)

src/serverPlayer.js

+2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ class Player extends Connection {
2828
this.outLog = (...args) => debug('<- S', ...args)
2929
}
3030

31+
this.batchHeader = this.server.batchHeader
3132
// Compression is server-wide
3233
this.compressionAlgorithm = this.server.compressionAlgorithm
3334
this.compressionLevel = this.server.compressionLevel
3435
this.compressionThreshold = this.server.compressionThreshold
36+
this.compressionHeader = this.server.compressionHeader
3537

3638
this._sentNetworkSettings = false // 1.19.30+
3739
}

src/transforms/framer.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ class Framer {
66
constructor (client) {
77
// Encoding
88
this.packets = []
9+
this.batchHeader = client.batchHeader
910
this.compressor = client.compressionAlgorithm || 'none'
1011
this.compressionLevel = client.compressionLevel
1112
this.compressionThreshold = client.compressionThreshold
13+
this.compressionHeader = client.compressionHeader || 0
1214
this.writeCompressor = client.features.compressorInHeader && client.compressionReady
1315
}
1416

@@ -38,7 +40,7 @@ class Framer {
3840

3941
static decode (client, buf) {
4042
// Read header
41-
if (buf[0] !== 0xfe) throw Error('bad batch packet header ' + buf[0])
43+
if (this.batchHeader && buf[0] !== this.batchHeader) throw Error(`bad batch packet header, received: ${buf[0]}, expected: ${this.batchHeader}`)
4244
const buffer = buf.slice(1)
4345
// Decompress
4446
let decompressed
@@ -58,9 +60,10 @@ class Framer {
5860

5961
encode () {
6062
const buf = Buffer.concat(this.packets)
61-
const compressed = (buf.length > this.compressionThreshold) ? this.compress(buf) : buf
62-
const header = this.writeCompressor ? [0xfe, 0] : [0xfe]
63-
return Buffer.concat([Buffer.from(header), compressed])
63+
const shouldCompress = buf.length > this.compressionThreshold
64+
const header = this.batchHeader ? [this.batchHeader] : []
65+
if (this.writeCompressor) header.push(shouldCompress ? this.compressionHeader : 255)
66+
return Buffer.concat([Buffer.from(header), shouldCompress ? this.compress(buf) : buf])
6467
}
6568

6669
addEncodedPacket (chunk) {

0 commit comments

Comments
 (0)