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:
Will Franzen 2014-12-30 02:55:04 -06:00
parent b73884e236
commit 675d358dec
2 changed files with 43 additions and 7 deletions

View file

@ -4,6 +4,7 @@ var net = require('net')
, protocol = require('./protocol')
, dns = require('dns')
, createPacketBuffer = protocol.createPacketBuffer
, compressPacketBuffer = protocol.compressPacketBuffer
, parsePacket = protocol.parsePacket
, states = protocol.states
, debug = protocol.debug
@ -154,18 +155,38 @@ Client.prototype.write = function(packetId, params) {
packetId = packetId[1];
}
var buffer = createPacketBuffer(packetId, this.state, params, this.isServer);
// 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);
if(this.compressionThreshold != -1 && buffer.length > this.compressionThreshold) {
compressPacketBuffer(buffer, finishWriting);
} else {
finishWriting(buffer);
}
};
Client.prototype.writeRaw = function(buffer, shouldEncrypt) {
if (shouldEncrypt === null) {
shouldEncrypt = true;
}
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);
}
};

View file

@ -1,5 +1,6 @@
var assert = require('assert');
var util = require('util');
var zlib = require('zlib');
var STRING_MAX_LENGTH = 240;
var SRV_STRING_MAX_LENGTH = 32767;
@ -1289,6 +1290,19 @@ function createPacketBuffer(packetId, state, params, isServer) {
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) {
if (state == null) state = states.PLAY;
var cursor = 0;
@ -1365,6 +1379,7 @@ module.exports = {
sessionVersion: 13,
parsePacket: parsePacket,
createPacketBuffer: createPacketBuffer,
compressPacketBuffer: compressPacketBuffer,
STRING_MAX_LENGTH: STRING_MAX_LENGTH,
packetIds: packetIds,
packetNames: packetNames,