@@ -83,8 +83,10 @@ Connection.prototype.connect = function(port, host) {
83
83
}
84
84
} ;
85
85
86
+ //attach read listeners for incoming packet parsing
86
87
Connection . prototype . attachListeners = function ( stream ) {
87
88
var self = this ;
89
+ //wrap stream for older node version
88
90
var reader = new Readable ( ) ;
89
91
reader = reader . wrap ( stream ) ;
90
92
reader . on ( 'readable' , function ( ) {
@@ -296,20 +298,6 @@ Connection.prototype.sendCopyFail = function (msg) {
296
298
this . _send ( 0x66 ) ;
297
299
} ;
298
300
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
-
313
301
Connection . prototype . readSslResponse = function ( ) {
314
302
var remaining = this . buffer . length - ( this . offset ) ;
315
303
if ( remaining < 1 ) {
@@ -324,11 +312,11 @@ Connection.prototype.readSslResponse = function() {
324
312
} ;
325
313
326
314
Connection . prototype . parseMessage = function ( reader ) {
327
- //I want to get rid of this bit here...
328
315
if ( ! this . header ) {
329
316
//try to read the 5 byte header
330
317
this . header = reader . read ( 5 ) ;
331
318
if ( ! this . header ) {
319
+ //try again once the stream becomes readable
332
320
return false ;
333
321
}
334
322
}
@@ -338,17 +326,25 @@ Connection.prototype.parseMessage = function(reader) {
338
326
//read message length
339
327
var length = this . header . readInt32BE ( 1 , true ) ;
340
328
329
+ //packet length includes the 4 bytes of the length 32 bit int
341
330
var bodyLength = length - 4 ;
331
+
342
332
//some packets have an empty body
343
333
if ( bodyLength ) {
334
+ //try to read the entire body packet
344
335
var body = reader . read ( bodyLength ) ;
336
+ //could not read
337
+ //try again when more data comes in
345
338
if ( ! body ) {
346
339
return false ;
347
340
}
348
- //blow away header
341
+ //set buffer for further packet-specific parsing
349
342
this . buffer = body ;
343
+ //reset parse offset to 0 for this packet
350
344
this . offset = 0 ;
351
345
}
346
+ //if we have a full body packet
347
+ //we can remove the saved header
352
348
this . header = false ;
353
349
354
350
var msg = {
0 commit comments