File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -113,15 +113,16 @@ new connections even after this event.
113113
114114#### getPort()
115115
116- The ` getPort(): int ` method can be used to
116+ The ` getPort(): ? int ` method can be used to
117117return the port this server is currently listening on.
118118
119119``` php
120120$port = $server->getPort();
121121echo 'Server listening on port ' . $port . PHP_EOL;
122122```
123123
124- This method MUST NOT be called after calling [ ` shutdown() ` ] ( #shutdown ) .
124+ It will return the port number or ` NULL ` if it is unknown (not applicable to
125+ this server socket or already closed).
125126
126127#### shutdown()
127128
@@ -135,7 +136,7 @@ echo 'Shutting down server socket' . PHP_EOL;
135136$server->shutdown();
136137```
137138
138- This method MUST NOT be called more than once on the same instance.
139+ Calling this method more than once on the same instance is a NO-OP .
139140
140141### Server
141142
Original file line number Diff line number Diff line change @@ -43,6 +43,10 @@ class SecureServer extends EventEmitter implements ServerInterface
4343
4444 public function __construct (Server $ tcp , LoopInterface $ loop , array $ context )
4545 {
46+ if (!is_resource ($ tcp ->master )) {
47+ throw new ConnectionException ('TCP server already shut down ' );
48+ }
49+
4650 // default to empty passphrase to surpress blocking passphrase prompt
4751 $ context += array (
4852 'passphrase ' => ''
Original file line number Diff line number Diff line change @@ -130,16 +130,22 @@ public function handleConnection($socket)
130130
131131 public function getPort ()
132132 {
133+ if (!is_resource ($ this ->master )) {
134+ return null ;
135+ }
136+
133137 $ name = stream_socket_get_name ($ this ->master , false );
134138
135139 return (int ) substr (strrchr ($ name , ': ' ), 1 );
136140 }
137141
138142 public function shutdown ()
139143 {
140- $ this ->loop ->removeStream ($ this ->master );
141- fclose ($ this ->master );
142- $ this ->removeAllListeners ();
144+ if (is_resource ($ this ->master )) {
145+ $ this ->loop ->removeStream ($ this ->master );
146+ fclose ($ this ->master );
147+ $ this ->removeAllListeners ();
148+ }
143149 }
144150
145151 public function createConnection ($ socket )
Original file line number Diff line number Diff line change @@ -51,9 +51,7 @@ interface ServerInterface extends EventEmitterInterface
5151 /**
5252 * Returns the port this server is currently listening on
5353 *
54- * This method MUST NOT be called after calling shutdown().
55- *
56- * @return int the port number
54+ * @return ?int the port number or NULL if it is unknown (not applicable to this server socket or already closed)
5755 */
5856 public function getPort ();
5957
@@ -62,7 +60,7 @@ public function getPort();
6260 *
6361 * This will stop listening for new incoming connections on this socket.
6462 *
65- * This method MUST NOT be called more than once on the same instance.
63+ * Calling this method more than once on the same instance is a NO-OP .
6664 *
6765 * @return void
6866 */
Original file line number Diff line number Diff line change @@ -366,4 +366,17 @@ public function testEmitsErrorIfConnectionIsNotSecureHandshake()
366366
367367 Block \sleep (self ::TIMEOUT , $ loop );
368368 }
369+
370+ /**
371+ * @expectedException React\Socket\ConnectionException
372+ */
373+ public function testFailsToCreateSecureServerOnClosedSocket ()
374+ {
375+ $ loop = Factory::create ();
376+
377+ $ server = new Server (0 , $ loop );
378+ $ server ->shutdown ();
379+
380+ new SecureServer ($ server , $ loop , array ());
381+ }
369382}
Original file line number Diff line number Diff line change @@ -139,6 +139,18 @@ public function testLoopWillEndWhenServerIsShutDown()
139139 $ this ->loop ->run ();
140140 }
141141
142+ public function testShutDownTwiceIsNoOp ()
143+ {
144+ $ this ->server ->shutdown ();
145+ $ this ->server ->shutdown ();
146+ }
147+
148+ public function testGetPortAfterShutDownReturnsNull ()
149+ {
150+ $ this ->server ->shutdown ();
151+ $ this ->assertNull ($ this ->server ->getPort ());
152+ }
153+
142154 public function testLoopWillEndWhenServerIsShutDownAfterSingleConnection ()
143155 {
144156 $ client = stream_socket_client ('tcp://localhost: ' . $ this ->port );
You can’t perform that action at this time.
0 commit comments