mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-12-02 03:56:54 -05:00
Finish adding compression to pipeline (see note)
It's 3 AM and I know theres a pretty big chance none of this code makes sense. Everything needs to be tested, as well as I need to figure out if encryption comes after compression or not.
This commit is contained in:
parent
b73884e236
commit
675d358dec
2 changed files with 43 additions and 7 deletions
|
@ -4,6 +4,7 @@ var net = require('net')
|
||||||
, protocol = require('./protocol')
|
, protocol = require('./protocol')
|
||||||
, dns = require('dns')
|
, dns = require('dns')
|
||||||
, createPacketBuffer = protocol.createPacketBuffer
|
, createPacketBuffer = protocol.createPacketBuffer
|
||||||
|
, compressPacketBuffer = protocol.compressPacketBuffer
|
||||||
, parsePacket = protocol.parsePacket
|
, parsePacket = protocol.parsePacket
|
||||||
, states = protocol.states
|
, states = protocol.states
|
||||||
, debug = protocol.debug
|
, debug = protocol.debug
|
||||||
|
@ -154,18 +155,38 @@ Client.prototype.write = function(packetId, params) {
|
||||||
packetId = packetId[1];
|
packetId = packetId[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Which comes first, encryption or compression?
|
||||||
|
|
||||||
|
var finishWriting = function(buffer) {
|
||||||
|
debug("writing packetId " + packetId + " (0x" + packetId.toString(16) + ")");
|
||||||
|
debug(params);
|
||||||
|
var out = this.encryptionEnabled ? new Buffer(this.cipher.update(buffer), 'binary') : buffer;
|
||||||
|
this.socket.write(out);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
var buffer = createPacketBuffer(packetId, this.state, params, this.isServer);
|
var buffer = createPacketBuffer(packetId, this.state, params, this.isServer);
|
||||||
debug("writing packetId " + packetId + " (0x" + packetId.toString(16) + ")");
|
if(this.compressionThreshold != -1 && buffer.length > this.compressionThreshold) {
|
||||||
debug(params);
|
compressPacketBuffer(buffer, finishWriting);
|
||||||
var out = this.encryptionEnabled ? new Buffer(this.cipher.update(buffer), 'binary') : buffer;
|
} else {
|
||||||
this.socket.write(out);
|
finishWriting(buffer);
|
||||||
return true;
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Client.prototype.writeRaw = function(buffer, shouldEncrypt) {
|
Client.prototype.writeRaw = function(buffer, shouldEncrypt) {
|
||||||
if (shouldEncrypt === null) {
|
if (shouldEncrypt === null) {
|
||||||
shouldEncrypt = true;
|
shouldEncrypt = true;
|
||||||
}
|
}
|
||||||
var out = (shouldEncrypt && this.encryptionEnabled) ? new Buffer(this.cipher.update(buffer), 'binary') : buffer;
|
|
||||||
this.socket.write(out);
|
var finishWriting = function(buffer) {
|
||||||
|
var out = (shouldEncrypt && this.encryptionEnabled) ? new Buffer(this.cipher.update(buffer), 'binary') : buffer;
|
||||||
|
this.socket.write(out);
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this.compressionThreshold != -1 && buffer.length > this.compressionThreshold) {
|
||||||
|
compressPacketBuffer(buffer, finishWriting);
|
||||||
|
} else {
|
||||||
|
finishWriting(buffer);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
var zlib = require('zlib');
|
||||||
|
|
||||||
var STRING_MAX_LENGTH = 240;
|
var STRING_MAX_LENGTH = 240;
|
||||||
var SRV_STRING_MAX_LENGTH = 32767;
|
var SRV_STRING_MAX_LENGTH = 32767;
|
||||||
|
@ -1289,6 +1290,19 @@ function createPacketBuffer(packetId, state, params, isServer) {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function compressPacketBuffer(buffer, callback) {
|
||||||
|
var dataLength = buffer.length;
|
||||||
|
var packetLength = dataLength + sizeOfVarInt(dataLength);
|
||||||
|
var data = zlib.deflateRaw(buffer, function(compressedBuffer) {
|
||||||
|
var size = sizeOfVarInt(packetLength) + sizeOfVarInt(packetLength) + compressedBuffer.length;
|
||||||
|
var packetBuffer = new Buffer(size);
|
||||||
|
var offset = writeVarInt(packetLength, packetBuffer, 0);
|
||||||
|
offset = writeVarInt(dataLength, packetBuffer, offset);
|
||||||
|
writeVarInt(compressedBuffer, packetBuffer, offset);
|
||||||
|
callback(packetBuffer);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function parsePacket(buffer, state, isServer, packetsToParse) {
|
function parsePacket(buffer, state, isServer, packetsToParse) {
|
||||||
if (state == null) state = states.PLAY;
|
if (state == null) state = states.PLAY;
|
||||||
var cursor = 0;
|
var cursor = 0;
|
||||||
|
@ -1365,6 +1379,7 @@ module.exports = {
|
||||||
sessionVersion: 13,
|
sessionVersion: 13,
|
||||||
parsePacket: parsePacket,
|
parsePacket: parsePacket,
|
||||||
createPacketBuffer: createPacketBuffer,
|
createPacketBuffer: createPacketBuffer,
|
||||||
|
compressPacketBuffer: compressPacketBuffer,
|
||||||
STRING_MAX_LENGTH: STRING_MAX_LENGTH,
|
STRING_MAX_LENGTH: STRING_MAX_LENGTH,
|
||||||
packetIds: packetIds,
|
packetIds: packetIds,
|
||||||
packetNames: packetNames,
|
packetNames: packetNames,
|
||||||
|
|
Loading…
Reference in a new issue