Skip to content

Commit ce72df3

Browse files
committed
add comments
1 parent e017860 commit ce72df3

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

lib/connection.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ Connection.prototype.connect = function(port, host) {
8383
}
8484
};
8585

86+
//attach read listeners for incoming packet parsing
8687
Connection.prototype.attachListeners = function(stream) {
8788
var self = this;
89+
//wrap stream for older node version
8890
var reader = new Readable();
8991
reader = reader.wrap(stream);
9092
reader.on('readable', function() {
@@ -296,20 +298,6 @@ Connection.prototype.sendCopyFail = function (msg) {
296298
this._send(0x66);
297299
};
298300

299-
//parsing methods
300-
Connection.prototype.setBuffer = function(buffer) {
301-
if(this.lastBuffer) { //we have unfinished biznaz
302-
//need to combine last two buffers
303-
var remaining = this.lastBuffer.length - this.lastOffset;
304-
var combinedBuffer = new Buffer(buffer.length + remaining);
305-
this.lastBuffer.copy(combinedBuffer, 0, this.lastOffset);
306-
buffer.copy(combinedBuffer, remaining, 0);
307-
buffer = combinedBuffer;
308-
}
309-
this.buffer = buffer;
310-
this.offset = 0;
311-
};
312-
313301
Connection.prototype.readSslResponse = function() {
314302
var remaining = this.buffer.length - (this.offset);
315303
if(remaining < 1) {
@@ -324,11 +312,11 @@ Connection.prototype.readSslResponse = function() {
324312
};
325313

326314
Connection.prototype.parseMessage = function(reader) {
327-
//I want to get rid of this bit here...
328315
if(!this.header) {
329316
//try to read the 5 byte header
330317
this.header = reader.read(5);
331318
if(!this.header) {
319+
//try again once the stream becomes readable
332320
return false;
333321
}
334322
}
@@ -338,17 +326,25 @@ Connection.prototype.parseMessage = function(reader) {
338326
//read message length
339327
var length = this.header.readInt32BE(1, true);
340328

329+
//packet length includes the 4 bytes of the length 32 bit int
341330
var bodyLength = length - 4;
331+
342332
//some packets have an empty body
343333
if(bodyLength) {
334+
//try to read the entire body packet
344335
var body = reader.read(bodyLength);
336+
//could not read
337+
//try again when more data comes in
345338
if(!body) {
346339
return false;
347340
}
348-
//blow away header
341+
//set buffer for further packet-specific parsing
349342
this.buffer = body;
343+
//reset parse offset to 0 for this packet
350344
this.offset = 0;
351345
}
346+
//if we have a full body packet
347+
//we can remove the saved header
352348
this.header = false;
353349

354350
var msg = {

0 commit comments

Comments
 (0)