@@ -107,10 +107,13 @@ class CacheServer {
107
107
this . errCallback = errCallback ;
108
108
109
109
this . _server = net . createServer ( socket => {
110
- helpers . log ( consts . LOG_INFO , `${ socket . remoteAddress } :${ socket . remotePort } connected.` ) ;
110
+ const remoteAddress = socket . remoteAddress ;
111
+ const remotePort = socket . remotePort ;
112
+
113
+ helpers . log ( consts . LOG_INFO , `${ remoteAddress } :${ remotePort } connected.` ) ;
111
114
112
115
const cmdProc = new CommandProcessor ( this . cache ) ;
113
- const streamProc = new ClientStreamProcessor ( { clientAddress : `${ socket . remoteAddress } :${ socket . remotePort } ` } ) ;
116
+ const streamProc = new ClientStreamProcessor ( { clientAddress : `${ remoteAddress } :${ remotePort } ` } ) ;
114
117
115
118
const mirrors = this . _mirrors ;
116
119
if ( mirrors . length > 0 ) {
@@ -119,23 +122,32 @@ class CacheServer {
119
122
} ) ;
120
123
}
121
124
125
+ const unpipeStreams = ( ) => {
126
+ socket . unpipe ( ) ;
127
+ streamProc . unpipe ( ) ;
128
+ cmdProc . unpipe ( ) ;
129
+ } ;
130
+
131
+ cmdProc . on ( 'finish' , ( ) => {
132
+ helpers . log ( consts . LOG_DBG , `${ remoteAddress } :${ remotePort } CommandProcessor finished.` ) ;
133
+ process . nextTick ( unpipeStreams ) ;
134
+ } ) ;
135
+
122
136
socket . on ( 'close' , ( ) => {
123
- helpers . log ( consts . LOG_INFO , `${ socket . remoteAddress } :${ socket . remotePort } closed connection.` ) ;
124
- socket . unpipe ( ) ;
125
- streamProc . unpipe ( ) ;
126
- cmdProc . unpipe ( ) ;
127
- } ) . on ( 'error' , err => {
128
- helpers . log ( consts . LOG_ERR , err . message ) ;
129
- } ) ;
137
+ helpers . log ( consts . LOG_INFO , `${ remoteAddress } :${ remotePort } Closed connection.` ) ;
138
+ } ) . on ( 'error' , err => {
139
+ helpers . log ( consts . LOG_ERR , `${ remoteAddress } :${ remotePort } Connection ERROR: ${ err . message } ` ) ;
140
+ unpipeStreams ( ) ;
141
+ } ) ;
130
142
131
- if ( this . isRecordingClient ) {
132
- const sessionId = `${ socket . remoteAddress } _${ socket . remotePort } _${ Date . now ( ) } ` ;
133
- socket . pipe ( new ClientStreamRecorder ( { sessionId} ) ) ; // Record the incoming byte stream to disk
134
- }
143
+ if ( this . isRecordingClient ) {
144
+ const sessionId = `${ remoteAddress } _${ remotePort } _${ Date . now ( ) } ` ;
145
+ socket . pipe ( new ClientStreamRecorder ( { sessionId} ) ) ; // Record the incoming byte stream to disk
146
+ }
135
147
136
- socket . pipe ( streamProc ) // Transform the incoming byte stream into commands and file data
137
- . pipe ( cmdProc ) // Execute commands and interface with the cache module
138
- . pipe ( socket ) ; // Connect back to socket to send files
148
+ socket . pipe ( streamProc ) // Transform the incoming byte stream into commands and file data
149
+ . pipe ( cmdProc ) // Execute commands and interface with the cache module
150
+ . pipe ( socket ) ; // Connect back to socket to send files
139
151
} ) . on ( 'error' , err => {
140
152
if ( err . code === 'EADDRINUSE' ) {
141
153
helpers . log ( consts . LOG_ERR , `Port ${ this . port } is already in use...` ) ;
0 commit comments