Skip to content

Commit 60bfa95

Browse files
committed
WIP compressed write
1 parent 4758b15 commit 60bfa95

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

lib/protocol/PacketWriter.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ var COMPRESSED_PACKET_HEADER_LENGTH = 7;
77
var IEEE_754_BINARY_64_PRECISION = Math.pow(2, 53);
88
var PACKET_HEADER_LENGTH = 4;
99
var MAX_PACKET_LENGTH = Math.pow(2, 24) - 1;
10-
var Buffer = require('safe-buffer').Buffer;
10+
11+
var Buffer = require('safe-buffer').Buffer;
12+
var BufferList = require('./BufferList');
13+
var EventEmitter = require('events').EventEmitter;
14+
var Util = require('util');
15+
var Zlib = require('zlib');
1116

1217
module.exports = PacketWriter;
1318
function PacketWriter() {
1419
this._buffer = null;
1520
this._offset = 0;
21+
this._sync = false;
1622
}
23+
Util.inherits(PacketWriter, EventEmitter);
1724

18-
PacketWriter.prototype.toBuffer = function toBuffer(parser) {
25+
PacketWriter.prototype.finalize = function finalize(parser) {
1926
if (!this._buffer) {
2027
this._buffer = Buffer.alloc(0);
2128
this._offset = 0;
@@ -36,6 +43,7 @@ PacketWriter.prototype.toBuffer = function toBuffer(parser) {
3643
: MAX_PACKET_LENGTH;
3744

3845
var packetNumber = parser.incrementPacketNumber();
46+
var packetOffset = this._offset;
3947

4048
this.writeUnsignedNumber(3, packetLength);
4149
this.writeUnsignedNumber(1, packetNumber);
@@ -44,6 +52,10 @@ PacketWriter.prototype.toBuffer = function toBuffer(parser) {
4452
var end = start + packetLength;
4553

4654
this.writeBuffer(buffer.slice(start, end));
55+
56+
if (!parser._compressed) {
57+
this.emit('data', this._buffer.slice(packetOffset, this._offset));
58+
}
4759
}
4860

4961
if (parser._compressed) {
@@ -52,9 +64,6 @@ PacketWriter.prototype.toBuffer = function toBuffer(parser) {
5264
packets = Math.floor(length / MAX_PACKET_LENGTH) + 1;
5365
size = length + (packets * COMPRESSED_PACKET_HEADER_LENGTH);
5466

55-
this._buffer = Buffer.allocUnsafe(size);
56-
this._offset = 0;
57-
5867
for (packet = 0; packet < packets; packet++) {
5968
isLast = (packet + 1 === packets);
6069
packetLength = (isLast)
@@ -63,6 +72,9 @@ PacketWriter.prototype.toBuffer = function toBuffer(parser) {
6372

6473
packetNumber = parser.incrementCompressedPacketNumber();
6574

75+
this._buffer = Buffer.allocUnsafe(packetLength + COMPRESSED_PACKET_HEADER_LENGTH);
76+
this._offset = 0;
77+
6678
this.writeUnsignedNumber(3, packetLength);
6779
this.writeUnsignedNumber(1, packetNumber);
6880
this.writeUnsignedNumber(3, 0);
@@ -71,8 +83,28 @@ PacketWriter.prototype.toBuffer = function toBuffer(parser) {
7183
var end = start + packetLength;
7284

7385
this.writeBuffer(buffer.slice(start, end));
86+
87+
this.emit('data', this._buffer);
7488
}
7589
}
90+
};
91+
92+
PacketWriter.prototype.toBuffer = function toBuffer(parser) {
93+
var bufs = new BufferList();
94+
95+
this.on('data', function (data) {
96+
bufs.push(data);
97+
});
98+
99+
this._sync = true;
100+
this.finalize(parser);
101+
102+
this._buffer = Buffer.allocUnsafe(bufs.size);
103+
this._offset = 0;
104+
105+
while (bufs.size > 0) {
106+
this._offset += bufs.shift().copy(this._buffer, this._offset);
107+
}
76108

77109
return this._buffer;
78110
};

lib/protocol/Protocol.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,14 @@ Protocol.prototype._parsePacketDebug = function _parsePacketDebug(packet) {
306306

307307
Protocol.prototype._emitPacket = function(packet) {
308308
var packetWriter = new PacketWriter();
309+
var protocol = this;
310+
311+
packetWriter.on('data', function (data) {
312+
protocol.emit('data', data);
313+
});
314+
309315
packet.write(packetWriter);
310-
this.emit('data', packetWriter.toBuffer(this._parser));
316+
packetWriter.toBuffer(this._parser);
311317

312318
if (this._config.debug) {
313319
this._debugPacket(false, packet);

0 commit comments

Comments
 (0)