@@ -27,11 +27,11 @@ function __construct( $serverSocketAddr ) {
27
27
function __destruct () {
28
28
29
29
foreach ( $ this ->sockets as $ socket )
30
- $ this ->closeConnection ( $ socket );
30
+ $ this ->disconnect ( $ socket );
31
31
32
32
// If we were listening on a socket, remove the socket file
33
33
if ( $ this ->serverSocket ) {
34
- $ this ->closeConnection ( $ this ->serverSocket );
34
+ $ this ->disconnect ( $ this ->serverSocket );
35
35
unlink ( $ this ->serverSocketAddr );
36
36
}
37
37
}
@@ -69,7 +69,7 @@ private function addClientSocket( $socket ) {
69
69
// array_unshift when removing sockets, as it's slow, so we use unset()
70
70
// instead.
71
71
for ( $ i = 0 ; $ i < $ socketCount ; $ i ++ ) {
72
- if ( ! isset ( $ this ->sockets [ $ i ] ) ) {
72
+ if ( empty ( $ this ->sockets [ $ i ] ) ) {
73
73
$ foundSpot = true ;
74
74
$ this ->sockets [ $ i ] = $ socket ;
75
75
break ;
@@ -137,8 +137,10 @@ function run() {
137
137
while ( true ) {
138
138
139
139
// We have no more sockets to poll, all have disconnected
140
- if ( !$ this ->sockets && !$ this ->serverSocket )
140
+ if ( !$ this ->sockets && !$ this ->serverSocket ) {
141
+ $ this ->log ( 'No more sockets to poll, exiting run() loop ' );
141
142
break ;
143
+ }
142
144
143
145
$ readables = $ this ->sockets ;
144
146
if ( $ this ->serverSocket )
@@ -173,16 +175,18 @@ function run() {
173
175
if ( $ writables )
174
176
$ this ->handleWritableSockets ( $ writables );
175
177
176
- if ( $ this ->stop )
178
+ if ( $ this ->stop ) {
179
+ $ this ->log ( 'stop() was called, exiting run() loop ' );
177
180
break ;
181
+ }
178
182
}
179
183
180
184
foreach ( $ this ->sockets as $ socket )
181
- $ this ->closeConnection ( $ socket );
185
+ $ this ->disconnect ( $ socket );
182
186
$ this ->sockets = array ();
183
187
184
188
if ( $ this ->serverSocket )
185
- $ this ->closeConnection ( $ this ->serverSocket );
189
+ $ this ->disconnect ( $ this ->serverSocket );
186
190
}
187
191
188
192
function stop () {
@@ -205,16 +209,18 @@ private function handleReadableSockets( $sockets ) {
205
209
if ( !$ messages )
206
210
continue ;
207
211
208
- // $this->log( 'Buffer had '. count( $messages ) .' messages' );
212
+ $ this ->log ( 'Buffer had ' . count ( $ messages ) .' messages ' );
209
213
210
214
foreach ( $ messages as $ message ) {
211
215
foreach ( $ this ->callbacks as $ callback ) {
212
216
call_user_func ( $ callback , $ message , $ this , $ socket );
213
217
}
214
218
}
215
219
216
- if ( $ this ->stop )
220
+ if ( $ this ->stop ) {
221
+ $ this ->log ( 'stop() was called, so will not read from other sockets ' );
217
222
break ;
223
+ }
218
224
}
219
225
}
220
226
@@ -236,7 +242,7 @@ private function handleWritableSockets( $sockets ) {
236
242
throw new \Exception ( 'Could not write to socket ' );
237
243
}
238
244
239
- // $this->log( 'Sent '. $sentBytes .' b to '. $socketIndex );
245
+ $ this ->log ( 'Sent ' . $ sentBytes .' b to ' . $ socketIndex );
240
246
$ this ->writeBuffer [ $ socketIndex ] = substr ( $ buffer , $ sentBytes );
241
247
}
242
248
}
@@ -251,7 +257,7 @@ private function acceptClient() {
251
257
}
252
258
253
259
$ this ->addClientSocket ( $ socket );
254
- // $this->log( 'Accepted client '. ( count( $this->sockets ) - 1 ) );
260
+ $ this ->log ( 'Accepted client ' . ( count ( $ this ->sockets ) - 1 ) );
255
261
256
262
return $ socket ;
257
263
}
@@ -282,12 +288,12 @@ private function getMessagesFromSocket( $socket ) {
282
288
// Connection was dropped by peer
283
289
if ( $ dataLen === 0 ) {
284
290
// Don't read/write from/to this socket in the future
285
- $ this ->closeConnection ( $ this ->sockets [ $ socketIndex ] );
291
+ $ this ->disconnect ( $ this ->sockets [ $ socketIndex ] );
286
292
unset( $ this ->sockets [ $ socketIndex ] );
287
293
return array ();
288
294
}
289
295
290
- // $this->log( 'Recvd '. $dataLen .' b from '. $socketIndex );
296
+ $ this ->log ( 'Recvd ' . $ dataLen .' b from ' . $ socketIndex );
291
297
$ this ->populateMessageBuffer ( $ data , $ buffer );
292
298
293
299
// Get finished Message objects from the MessageBuffer
@@ -304,7 +310,7 @@ private function getMessagesFromSocket( $socket ) {
304
310
// partially) other messages' data
305
311
if ( $ overflowBytes > 0 ) {
306
312
307
- // $this->log( 'Recvd multiple messages from socket (overflow: '. $overflowBytes .' b)' );
313
+ $ this ->log ( 'Recvd multiple messages from socket (overflow: ' . $ overflowBytes .' b) ' );
308
314
309
315
$ overflow = substr ( $ buffer ->message ->body , -$ overflowBytes );
310
316
$ buffer ->message ->body .= substr ( $ buffer ->message ->body , 0 , -$ overflowBytes );
@@ -365,7 +371,7 @@ private function populateMessageBuffer( $data, MessageBuffer &$buffer ) {
365
371
}
366
372
}
367
373
368
- private function closeConnection ( $ socket ) {
374
+ private function disconnect ( $ socket ) {
369
375
370
376
// Close the connection until the worker client sends us a new result. We
371
377
// silence any errors so that we don't have to test the connection status
0 commit comments