@@ -31,55 +31,56 @@ export abstract class DbgpConnection {
3131 this . _parsingState = ParsingState . DataLength ;
3232 this . _dataLengthBuffer = new Buffer ( 0 ) ;
3333 this . _responseBuffer = new Buffer ( 0 ) ;
34+ socket . on ( 'data' , ( data : Buffer ) => this . _handleDataChunk ( data ) ) ;
35+ }
36+
37+ private _handleDataChunk ( data : Buffer ) {
3438 // Anatomy of packets: [data length] [NULL] [xml] [NULL]
35- const handleDataChunk = ( data : Buffer ) => {
36- // are we waiting for the data length or for the response?
37- if ( this . _parsingState === ParsingState . DataLength ) {
38- // does data contain a NULL byte?
39- const nullByteIndex = data . indexOf ( 0 ) ;
40- if ( nullByteIndex !== - 1 ) {
41- // YES -> we received the data length and are ready to receive the response
42- this . _dataLength = parseInt ( iconv . decode ( data . slice ( 0 , nullByteIndex ) , ENCODING ) ) ;
43- // reset buffer
44- this . _dataLengthBuffer = new Buffer ( 0 ) ;
45- // switch to response parsing state
46- this . _parsingState = ParsingState . Response ;
47- // if data contains more info (except the NULL byte)
48- if ( data . length > nullByteIndex + 1 ) {
49- // handle the rest of the packet as part of the response
50- const rest = data . slice ( nullByteIndex + 1 ) ;
51- handleDataChunk ( rest ) ;
52- }
53- } else {
54- // NO -> this is only part of the data length. We buffer it and wait for the next data event
55- this . _dataLengthBuffer = Buffer . concat ( [ this . _dataLengthBuffer , data ] , this . _dataLengthBuffer . length + data . length ) ;
39+ // are we waiting for the data length or for the response?
40+ if ( this . _parsingState === ParsingState . DataLength ) {
41+ // does data contain a NULL byte?
42+ const nullByteIndex = data . indexOf ( 0 ) ;
43+ if ( nullByteIndex !== - 1 ) {
44+ // YES -> we received the data length and are ready to receive the response
45+ this . _dataLength = parseInt ( iconv . decode ( data . slice ( 0 , nullByteIndex ) , ENCODING ) ) ;
46+ // reset buffer
47+ this . _dataLengthBuffer = new Buffer ( 0 ) ;
48+ // switch to response parsing state
49+ this . _parsingState = ParsingState . Response ;
50+ // if data contains more info (except the NULL byte)
51+ if ( data . length > nullByteIndex + 1 ) {
52+ // handle the rest of the packet as part of the response
53+ const rest = data . slice ( nullByteIndex + 1 ) ;
54+ this . _handleDataChunk ( rest ) ;
5655 }
57- } else if ( this . _parsingState === ParsingState . Response ) {
58- // does the new data together with the buffered data add up to the data length?
59- if ( this . _responseBuffer . length + data . length >= this . _dataLength ) {
60- // YES -> we received the whole response
61- const lastResponsePiece = data . slice ( 0 , this . _dataLength - this . _responseBuffer . length ) ;
62- // append the last piece of the response
63- const response = Buffer . concat ( [ this . _responseBuffer , lastResponsePiece ] , this . _dataLength ) ;
64- // call response handler
65- this . handleResponse ( parseResponse ( response ) ) ;
66- // reset buffer
67- this . _responseBuffer = new Buffer ( 0 ) ;
68- // switch to data length parsing state
69- this . _parsingState = ParsingState . DataLength ;
70- // if data contains more info (except the NULL byte)
71- if ( data . length > lastResponsePiece . length + 1 ) {
72- // handle the rest of the packet (after the NULL byte) as data length
73- const rest = data . slice ( lastResponsePiece . length + 1 ) ;
74- handleDataChunk ( rest ) ;
75- }
76- } else {
77- // NO -> this is not the whole response yet. We buffer it and wait for the next data event.
78- this . _responseBuffer = Buffer . concat ( [ this . _responseBuffer , data ] , this . _responseBuffer . length + data . length ) ;
56+ } else {
57+ // NO -> this is only part of the data length. We buffer it and wait for the next data event
58+ this . _dataLengthBuffer = Buffer . concat ( [ this . _dataLengthBuffer , data ] , this . _dataLengthBuffer . length + data . length ) ;
59+ }
60+ } else if ( this . _parsingState === ParsingState . Response ) {
61+ // does the new data together with the buffered data add up to the data length?
62+ if ( this . _responseBuffer . length + data . length >= this . _dataLength ) {
63+ // YES -> we received the whole response
64+ const lastResponsePiece = data . slice ( 0 , this . _dataLength - this . _responseBuffer . length ) ;
65+ // append the last piece of the response
66+ const response = Buffer . concat ( [ this . _responseBuffer , lastResponsePiece ] , this . _dataLength ) ;
67+ // call response handler
68+ this . handleResponse ( parseResponse ( response ) ) ;
69+ // reset buffer
70+ this . _responseBuffer = new Buffer ( 0 ) ;
71+ // switch to data length parsing state
72+ this . _parsingState = ParsingState . DataLength ;
73+ // if data contains more info (except the NULL byte)
74+ if ( data . length > lastResponsePiece . length + 1 ) {
75+ // handle the rest of the packet (after the NULL byte) as data length
76+ const rest = data . slice ( lastResponsePiece . length + 1 ) ;
77+ this . _handleDataChunk ( rest ) ;
7978 }
79+ } else {
80+ // NO -> this is not the whole response yet. We buffer it and wait for the next data event.
81+ this . _responseBuffer = Buffer . concat ( [ this . _responseBuffer , data ] , this . _responseBuffer . length + data . length ) ;
8082 }
81- } ;
82- socket . on ( 'data' , handleDataChunk ) ;
83+ }
8384 }
8485
8586 protected abstract handleResponse ( response : XMLDocument ) : void ;
0 commit comments