File tree Expand file tree Collapse file tree 5 files changed +90
-10
lines changed Expand file tree Collapse file tree 5 files changed +90
-10
lines changed Original file line number Diff line number Diff line change @@ -90,4 +90,44 @@ public function close()
90
90
{
91
91
$ this ->server ->close ();
92
92
}
93
+
94
+ /**
95
+ * [internal] Internal helper method to accept new connection from given server socket
96
+ *
97
+ * @param resource $socket server socket to accept connection from
98
+ * @return resource new client socket if any
99
+ * @throws \RuntimeException if accepting fails
100
+ * @internal
101
+ */
102
+ public static function accept ($ socket )
103
+ {
104
+ $ newSocket = @\stream_socket_accept ($ socket , 0 );
105
+
106
+ if (false === $ newSocket ) {
107
+ // Match errstr from PHP's warning message.
108
+ // stream_socket_accept(): accept failed: Connection timed out
109
+ $ error = \error_get_last ();
110
+ $ errstr = \preg_replace ('#.*: # ' , '' , $ error ['message ' ]);
111
+
112
+ // Go through list of possible error constants to find matching errno.
113
+ // @codeCoverageIgnoreStart
114
+ $ errno = 0 ;
115
+ if (\function_exists ('socket_strerror ' )) {
116
+ foreach (\get_defined_constants (false ) as $ name => $ value ) {
117
+ if (\strpos ($ name , 'SOCKET_E ' ) === 0 && \socket_strerror ($ value ) === $ errstr ) {
118
+ $ errno = $ value ;
119
+ break ;
120
+ }
121
+ }
122
+ }
123
+ // @codeCoverageIgnoreEnd
124
+
125
+ throw new \RuntimeException (
126
+ 'Unable to accept new connection: ' . $ errstr ,
127
+ $ errno
128
+ );
129
+ }
130
+
131
+ return $ newSocket ;
132
+ }
93
133
}
Original file line number Diff line number Diff line change @@ -211,10 +211,10 @@ public function resume()
211
211
212
212
$ that = $ this ;
213
213
$ this ->loop ->addReadStream ($ this ->master , function ($ master ) use ($ that ) {
214
- $ newSocket = @ \stream_socket_accept ( $ master , 0 );
215
- if ( false === $ newSocket) {
216
- $ that -> emit ( ' error ' , array ( new \RuntimeException ( ' Error accepting new connection ' )));
217
-
214
+ try {
215
+ $ newSocket = SocketServer:: accept ( $ master );
216
+ } catch ( \RuntimeException $ e ) {
217
+ $ that -> emit ( ' error ' , array ( $ e ));
218
218
return ;
219
219
}
220
220
$ that ->handleConnection ($ newSocket );
Original file line number Diff line number Diff line change @@ -113,10 +113,10 @@ public function resume()
113
113
114
114
$ that = $ this ;
115
115
$ this ->loop ->addReadStream ($ this ->master , function ($ master ) use ($ that ) {
116
- $ newSocket = @ \stream_socket_accept ( $ master , 0 );
117
- if ( false === $ newSocket) {
118
- $ that -> emit ( ' error ' , array ( new \RuntimeException ( ' Error accepting new connection ' )));
119
-
116
+ try {
117
+ $ newSocket = SocketServer:: accept ( $ master );
118
+ } catch ( \RuntimeException $ e ) {
119
+ $ that -> emit ( ' error ' , array ( $ e ));
120
120
return ;
121
121
}
122
122
$ that ->handleConnection ($ newSocket );
Original file line number Diff line number Diff line change @@ -287,7 +287,10 @@ public function testEmitsErrorWhenAcceptListenerFails()
287
287
288
288
$ server = new TcpServer (0 , $ loop );
289
289
290
- $ server ->on ('error ' , $ this ->expectCallableOnceWith ($ this ->isInstanceOf ('RuntimeException ' )));
290
+ $ exception = null ;
291
+ $ server ->on ('error ' , function ($ e ) use (&$ exception ) {
292
+ $ exception = $ e ;
293
+ });
291
294
292
295
$ this ->assertNotNull ($ listener );
293
296
$ socket = stream_socket_server ('tcp://127.0.0.1:0 ' );
@@ -297,6 +300,23 @@ public function testEmitsErrorWhenAcceptListenerFails()
297
300
$ time = microtime (true ) - $ time ;
298
301
299
302
$ this ->assertLessThan (1 , $ time );
303
+
304
+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
305
+ assert ($ exception instanceof \RuntimeException);
306
+ $ this ->assertStringStartsWith ('Unable to accept new connection: ' , $ exception ->getMessage ());
307
+
308
+ return $ exception ;
309
+ }
310
+
311
+ /**
312
+ * @param \RuntimeException $e
313
+ * @requires extension sockets
314
+ * @depends testEmitsErrorWhenAcceptListenerFails
315
+ */
316
+ public function testEmitsTimeoutErrorWhenAcceptListenerFails (\RuntimeException $ exception )
317
+ {
318
+ $ this ->assertEquals ('Unable to accept new connection: ' . socket_strerror (SOCKET_ETIMEDOUT ), $ exception ->getMessage ());
319
+ $ this ->assertEquals (SOCKET_ETIMEDOUT , $ exception ->getCode ());
300
320
}
301
321
302
322
public function testListenOnBusyPortThrows ()
Original file line number Diff line number Diff line change @@ -292,7 +292,10 @@ public function testEmitsErrorWhenAcceptListenerFails()
292
292
293
293
$ server = new UnixServer ($ this ->getRandomSocketUri (), $ loop );
294
294
295
- $ server ->on ('error ' , $ this ->expectCallableOnceWith ($ this ->isInstanceOf ('RuntimeException ' )));
295
+ $ exception = null ;
296
+ $ server ->on ('error ' , function ($ e ) use (&$ exception ) {
297
+ $ exception = $ e ;
298
+ });
296
299
297
300
$ this ->assertNotNull ($ listener );
298
301
$ socket = stream_socket_server ('tcp://127.0.0.1:0 ' );
@@ -302,6 +305,23 @@ public function testEmitsErrorWhenAcceptListenerFails()
302
305
$ time = microtime (true ) - $ time ;
303
306
304
307
$ this ->assertLessThan (1 , $ time );
308
+
309
+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
310
+ assert ($ exception instanceof \RuntimeException);
311
+ $ this ->assertStringStartsWith ('Unable to accept new connection: ' , $ exception ->getMessage ());
312
+
313
+ return $ exception ;
314
+ }
315
+
316
+ /**
317
+ * @param \RuntimeException $e
318
+ * @requires extension sockets
319
+ * @depends testEmitsErrorWhenAcceptListenerFails
320
+ */
321
+ public function testEmitsTimeoutErrorWhenAcceptListenerFails (\RuntimeException $ exception )
322
+ {
323
+ $ this ->assertEquals ('Unable to accept new connection: ' . socket_strerror (SOCKET_ETIMEDOUT ), $ exception ->getMessage ());
324
+ $ this ->assertEquals (SOCKET_ETIMEDOUT , $ exception ->getCode ());
305
325
}
306
326
307
327
public function testListenOnBusyPortThrows ()
You can’t perform that action at this time.
0 commit comments