Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic compression & batch header #544

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Client extends Connection {
this.compressionAlgorithm = this.versionGreaterThanOrEqualTo('1.19.30') ? 'none' : 'deflate'
this.compressionThreshold = 512
this.compressionLevel = this.options.compressionLevel
this.batchHeader = 0xfe

if (isDebug) {
this.inLog = (...args) => debug('C ->', ...args)
Expand Down
4 changes: 2 additions & 2 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Connection extends EventEmitter {

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

Expand All @@ -165,7 +165,7 @@ class Connection extends EventEmitter {
}

handle (buffer) { // handle encapsulated
if (buffer[0] === 0xfe) { // wrapper
if (!this.batchHeader || buffer[0] === this.batchHeader) { // wrapper
if (this.encryptionEnabled) {
this.decrypt(buffer.slice(1))
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Server extends EventEmitter {
this.clients = {}
this.clientCount = 0
this.conLog = debug
this.batchHeader = 0xfe

this.setCompressor(this.options.compressionAlgorithm, this.options.compressionLevel, this.options.compressionThreshold)
}
Expand All @@ -44,16 +45,19 @@ class Server extends EventEmitter {
case 'none':
this.compressionAlgorithm = 'none'
this.compressionLevel = 0
this.compressionHeader = 255
break
case 'deflate':
this.compressionAlgorithm = 'deflate'
this.compressionLevel = level
this.compressionThreshold = threshold
this.compressionHeader = 0
break
case 'snappy':
this.compressionAlgorithm = 'snappy'
this.compressionLevel = level
this.compressionThreshold = threshold
this.compressionHeader = 1
break
default:
throw new Error(`Unknown compression algorithm: ${algorithm}`)
Expand Down
2 changes: 2 additions & 0 deletions src/serverPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class Player extends Connection {
this.outLog = (...args) => debug('<- S', ...args)
}

this.batchHeader = this.server.batchHeader
// Compression is server-wide
this.compressionAlgorithm = this.server.compressionAlgorithm
this.compressionLevel = this.server.compressionLevel
this.compressionThreshold = this.server.compressionThreshold
this.compressionHeader = this.server.compressionHeader

this._sentNetworkSettings = false // 1.19.30+
}
Expand Down
11 changes: 7 additions & 4 deletions src/transforms/framer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ class Framer {
constructor (client) {
// Encoding
this.packets = []
this.batchHeader = client.batchHeader
this.compressor = client.compressionAlgorithm || 'none'
this.compressionLevel = client.compressionLevel
this.compressionThreshold = client.compressionThreshold
this.compressionHeader = client.compressionHeader || 0
this.writeCompressor = client.features.compressorInHeader && client.compressionReady
}

Expand Down Expand Up @@ -38,7 +40,7 @@ class Framer {

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

encode () {
const buf = Buffer.concat(this.packets)
const compressed = (buf.length > this.compressionThreshold) ? this.compress(buf) : buf
const header = this.writeCompressor ? [0xfe, 0] : [0xfe]
return Buffer.concat([Buffer.from(header), compressed])
const shouldCompress = buf.length > this.compressionThreshold
const header = this.batchHeader ? [this.batchHeader] : []
if (this.writeCompressor) header.push(shouldCompress ? this.compressionHeader : 255)
return Buffer.concat([Buffer.from(header), shouldCompress ? this.compress(buf) : buf])
}

addEncodedPacket (chunk) {
Expand Down
Loading