Skip to content

Commit 2d5a2e5

Browse files
authored
Merge pull request #67 from clue-labs/getaddress
Replace getPort() with getAddress()
2 parents c94c3e5 + b0c8d37 commit 2d5a2e5

File tree

12 files changed

+103
-58
lines changed

12 files changed

+103
-58
lines changed

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For the code of the current stable 0.4.x release, checkout the
1919
* [ServerInterface](#serverinterface)
2020
* [connection event](#connection-event)
2121
* [error event](#error-event)
22-
* [getPort()](#getport)
22+
* [getAddress()](#getaddress)
2323
* [close()](#close)
2424
* [Server](#server)
2525
* [SecureServer](#secureserver)
@@ -115,18 +115,27 @@ Note that this is not a fatal error event, i.e. the server keeps listening for
115115
new connections even after this event.
116116

117117

118-
#### getPort()
118+
#### getAddress()
119119

120-
The `getPort(): ?int` method can be used to
121-
return the port this server is currently listening on.
120+
The `getAddress(): ?string` method can be used to
121+
return the full address (IP and port) this server is currently listening on.
122122

123123
```php
124-
$port = $server->getPort();
125-
echo 'Server listening on port ' . $port . PHP_EOL;
124+
$address = $server->getAddress();
125+
echo 'Server listening on ' . $address . PHP_EOL;
126126
```
127127

128-
It will return the port number or `NULL` if it is unknown (not applicable to
129-
this server socket or already closed).
128+
It will return the full address (IP and port) or `NULL` if it is unknown
129+
(not applicable to this server socket or already closed).
130+
131+
If this is a TCP/IP based server and you only want the local port, you may
132+
use something like this:
133+
134+
```php
135+
$address = $server->getAddress();
136+
$port = parse_url('tcp://' . $address, PHP_URL_PORT);
137+
echo 'Server listening on port ' . $port . PHP_EOL;
138+
```
130139

131140
#### close()
132141

@@ -159,7 +168,7 @@ In order to use a random port assignment, you can use the port `0`:
159168

160169
```php
161170
$server = new Server(0, $loop);
162-
$port = $server->getPort();
171+
$address = $server->getAddress();
163172
```
164173

165174
In order to change the host the socket is listening on, you can provide an IP

examples/01-echo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@
3636

3737
$server->on('error', 'printf');
3838

39-
echo 'bound to ' . $server->getPort() . PHP_EOL;
39+
echo 'Listening on ' . $server->getAddress() . PHP_EOL;
4040

4141
$loop->run();

examples/02-chat-server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@
5858

5959
$server->on('error', 'printf');
6060

61-
echo 'Listening on ' . $server->getPort() . PHP_EOL;
61+
echo 'Listening on ' . $server->getAddress() . PHP_EOL;
6262

6363
$loop->run();

examples/03-benchmark.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353

5454
$server->on('error', 'printf');
5555

56-
echo 'bound to ' . $server->getPort() . PHP_EOL;
56+
echo 'Listening on ' . $server->getAddress() . PHP_EOL;
5757

5858
$loop->run();

src/SecureServer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function __construct(Server $tcp, LoopInterface $loop, array $context)
6868
});
6969
}
7070

71-
public function getPort()
71+
public function getAddress()
7272
{
73-
return $this->tcp->getPort();
73+
return $this->tcp->getAddress();
7474
}
7575

7676
public function close()

src/Server.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Server extends EventEmitter implements ServerInterface
5858
*
5959
* ```php
6060
* $server = new Server(0, $loop);
61-
* $port = $server->getPort();
61+
* $address = $server->getAddress();
6262
* ```
6363
*
6464
* In order to change the host the socket is listening on, you can provide an IP
@@ -174,15 +174,22 @@ public function handleConnection($socket)
174174
$this->emit('connection', array($client));
175175
}
176176

177-
public function getPort()
177+
public function getAddress()
178178
{
179179
if (!is_resource($this->master)) {
180180
return null;
181181
}
182182

183-
$name = stream_socket_get_name($this->master, false);
183+
$address = stream_socket_get_name($this->master, false);
184184

185-
return (int) substr(strrchr($name, ':'), 1);
185+
// check if this is an IPv6 address which includes multiple colons but no square brackets
186+
$pos = strrpos($address, ':');
187+
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
188+
$port = substr($address, $pos + 1);
189+
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
190+
}
191+
192+
return $address;
186193
}
187194

188195
public function close()

src/ServerInterface.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,28 @@
4949
interface ServerInterface extends EventEmitterInterface
5050
{
5151
/**
52-
* Returns the port this server is currently listening on
52+
* Returns the full address (IP and port) this server is currently listening on
5353
*
54-
* @return ?int the port number or NULL if it is unknown (not applicable to this server socket or already closed)
54+
* ```php
55+
* $address = $server->getAddress();
56+
* echo 'Server listening on ' . $address . PHP_EOL;
57+
* ```
58+
*
59+
* It will return the full address (IP and port) or `NULL` if it is unknown
60+
* (not applicable to this server socket or already closed).
61+
*
62+
* If this is a TCP/IP based server and you only want the local port, you may
63+
* use something like this:
64+
*
65+
* ```php
66+
* $address = $server->getAddress();
67+
* $port = parse_url('tcp://' . $address, PHP_URL_PORT);
68+
* echo 'Server listening on port ' . $port . PHP_EOL;
69+
* ```
70+
*
71+
* @return ?string the full listening address (IP and port) or NULL if it is unknown (not applicable to this server socket or already closed)
5572
*/
56-
public function getPort();
73+
public function getAddress();
5774

5875
/**
5976
* Shuts down this listening socket

tests/FunctionalSecureServerTest.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace React\Tests\Socket;
44

55
use React\EventLoop\Factory;
6-
use React\SocketClient\TcpConnector;
7-
use Clue\React\Block;
8-
use React\Socket\SecureServer;
9-
use React\SocketClient\SecureConnector;
106
use React\Stream\Stream;
7+
use React\Socket\SecureServer;
118
use React\Socket\ConnectionInterface;
129
use React\Socket\Server;
10+
use React\Socket\ServerInterface;
11+
use React\SocketClient\TcpConnector;
12+
use React\SocketClient\SecureConnector;
13+
use Clue\React\Block;
1314

1415
class FunctionalSecureServerTest extends TestCase
1516
{
@@ -31,7 +32,7 @@ public function testEmitsConnectionForNewConnection()
3132
'local_cert' => __DIR__ . '/../examples/localhost.pem'
3233
));
3334
$server->on('connection', $this->expectCallableOnce());
34-
$port = $server->getPort();
35+
$port = $this->getPort($server);
3536

3637
$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
3738
'verify_peer' => false
@@ -50,7 +51,7 @@ public function testWritesDataToConnection()
5051
'local_cert' => __DIR__ . '/../examples/localhost.pem'
5152
));
5253
$server->on('connection', $this->expectCallableOnce());
53-
$port = $server->getPort();
54+
$port = $this->getPort($server);
5455

5556
$server->on('connection', function (ConnectionInterface $conn) {
5657
$conn->write('foo');
@@ -78,7 +79,7 @@ public function testWritesDataInMultipleChunksToConnection()
7879
'local_cert' => __DIR__ . '/../examples/localhost.pem'
7980
));
8081
$server->on('connection', $this->expectCallableOnce());
81-
$port = $server->getPort();
82+
$port = $this->getPort($server);
8283

8384
$server->on('connection', function (ConnectionInterface $conn) {
8485
$conn->write(str_repeat('*', 400000));
@@ -111,7 +112,7 @@ public function testEmitsDataFromConnection()
111112
'local_cert' => __DIR__ . '/../examples/localhost.pem'
112113
));
113114
$server->on('connection', $this->expectCallableOnce());
114-
$port = $server->getPort();
115+
$port = $this->getPort($server);
115116

116117
$once = $this->expectCallableOnceWith('foo');
117118
$server->on('connection', function (ConnectionInterface $conn) use ($once) {
@@ -140,7 +141,7 @@ public function testEmitsDataInMultipleChunksFromConnection()
140141
'local_cert' => __DIR__ . '/../examples/localhost.pem'
141142
));
142143
$server->on('connection', $this->expectCallableOnce());
143-
$port = $server->getPort();
144+
$port = $this->getPort($server);
144145

145146
$received = 0;
146147
$server->on('connection', function (ConnectionInterface $conn) use (&$received) {
@@ -173,7 +174,7 @@ public function testPipesDataBackInMultipleChunksFromConnection()
173174
'local_cert' => __DIR__ . '/../examples/localhost.pem'
174175
));
175176
$server->on('connection', $this->expectCallableOnce());
176-
$port = $server->getPort();
177+
$port = $this->getPort($server);
177178

178179
$server->on('connection', function (ConnectionInterface $conn) use (&$received) {
179180
$conn->pipe($conn);
@@ -209,7 +210,7 @@ public function testEmitsConnectionForNewConnectionWithEncryptedCertificate()
209210
'passphrase' => 'swordfish'
210211
));
211212
$server->on('connection', $this->expectCallableOnce());
212-
$port = $server->getPort();
213+
$port = $this->getPort($server);
213214

214215
$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
215216
'verify_peer' => false
@@ -229,7 +230,7 @@ public function testEmitsErrorForServerWithInvalidCertificate()
229230
));
230231
$server->on('connection', $this->expectCallableNever());
231232
$server->on('error', $this->expectCallableOnce());
232-
$port = $server->getPort();
233+
$port = $this->getPort($server);
233234

234235
$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
235236
'verify_peer' => false
@@ -250,7 +251,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase
250251
));
251252
$server->on('connection', $this->expectCallableNever());
252253
$server->on('error', $this->expectCallableOnce());
253-
$port = $server->getPort();
254+
$port = $this->getPort($server);
254255

255256
$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
256257
'verify_peer' => false
@@ -272,7 +273,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph
272273
));
273274
$server->on('connection', $this->expectCallableNever());
274275
$server->on('error', $this->expectCallableOnce());
275-
$port = $server->getPort();
276+
$port = $this->getPort($server);
276277

277278
$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
278279
'verify_peer' => false
@@ -293,7 +294,7 @@ public function testEmitsErrorForConnectionWithPeerVerification()
293294
));
294295
$server->on('connection', $this->expectCallableNever());
295296
$server->on('error', $this->expectCallableOnce());
296-
$port = $server->getPort();
297+
$port = $this->getPort($server);
297298

298299
$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
299300
'verify_peer' => true
@@ -314,7 +315,7 @@ public function testEmitsErrorIfConnectionIsCancelled()
314315
));
315316
$server->on('connection', $this->expectCallableNever());
316317
$server->on('error', $this->expectCallableOnce());
317-
$port = $server->getPort();
318+
$port = $this->getPort($server);
318319

319320
$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
320321
'verify_peer' => false
@@ -336,7 +337,7 @@ public function testEmitsNothingIfConnectionIsIdle()
336337
));
337338
$server->on('connection', $this->expectCallableNever());
338339
$server->on('error', $this->expectCallableNever());
339-
$port = $server->getPort();
340+
$port = $this->getPort($server);
340341

341342
$connector = new TcpConnector($loop);
342343
$promise = $connector->create('127.0.0.1', $port);
@@ -355,7 +356,7 @@ public function testEmitsErrorIfConnectionIsNotSecureHandshake()
355356
));
356357
$server->on('connection', $this->expectCallableNever());
357358
$server->on('error', $this->expectCallableOnce());
358-
$port = $server->getPort();
359+
$port = $this->getPort($server);
359360

360361
$connector = new TcpConnector($loop);
361362
$promise = $connector->create('127.0.0.1', $port);
@@ -379,4 +380,9 @@ public function testFailsToCreateSecureServerOnClosedSocket()
379380

380381
new SecureServer($server, $loop, array());
381382
}
383+
384+
private function getPort(ServerInterface $server)
385+
{
386+
return parse_url($server->getAddress(), PHP_URL_PORT);
387+
}
382388
}

tests/FunctionalServerTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace React\Tests\Socket;
44

55
use React\EventLoop\Factory;
6-
use React\SocketClient\TcpConnector;
76
use React\Socket\Server;
8-
use Clue\React\Block;
97
use React\Socket\ConnectionException;
108
use React\Socket\ConnectionInterface;
9+
use React\Socket\ServerInterface;
10+
use React\SocketClient\TcpConnector;
11+
use Clue\React\Block;
1112

1213
class FunctionalServerTest extends TestCase
1314
{
@@ -17,7 +18,7 @@ public function testEmitsConnectionForNewConnection()
1718

1819
$server = new Server(0, $loop);
1920
$server->on('connection', $this->expectCallableOnce());
20-
$port = $server->getPort();
21+
$port = $this->getPort($server);
2122

2223
$connector = new TcpConnector($loop);
2324
$promise = $connector->create('127.0.0.1', $port);
@@ -36,7 +37,7 @@ public function testEmitsConnectionWithRemoteIp()
3637
$server->on('connection', function (ConnectionInterface $conn) use (&$peer) {
3738
$peer = $conn->getRemoteAddress();
3839
});
39-
$port = $server->getPort();
40+
$port = $this->getPort($server);
4041

4142
$connector = new TcpConnector($loop);
4243
$promise = $connector->create('127.0.0.1', $port);
@@ -59,7 +60,7 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer()
5960
$peer = $conn->getRemoteAddress();
6061
});
6162
});
62-
$port = $server->getPort();
63+
$port = $this->getPort($server);
6364

6465
$connector = new TcpConnector($loop);
6566
$promise = $connector->create('127.0.0.1', $port);
@@ -82,7 +83,7 @@ public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedL
8283
$conn->close();
8384
$peer = $conn->getRemoteAddress();
8485
});
85-
$port = $server->getPort();
86+
$port = $this->getPort($server);
8687

8788
$connector = new TcpConnector($loop);
8889
$promise = $connector->create('127.0.0.1', $port);
@@ -100,7 +101,7 @@ public function testEmitsConnectionEvenIfConnectionIsCancelled()
100101

101102
$server = new Server(0, $loop);
102103
$server->on('connection', $this->expectCallableOnce());
103-
$port = $server->getPort();
104+
$port = $this->getPort($server);
104105

105106
$connector = new TcpConnector($loop);
106107
$promise = $connector->create('127.0.0.1', $port);
@@ -122,7 +123,7 @@ public function testEmitsConnectionForNewIpv6Connection()
122123
}
123124

124125
$server->on('connection', $this->expectCallableOnce());
125-
$port = $server->getPort();
126+
$port = $this->getPort($server);
126127

127128
$connector = new TcpConnector($loop);
128129
$promise = $connector->create('::1', $port);
@@ -146,7 +147,7 @@ public function testEmitsConnectionWithRemoteIpv6()
146147
$server->on('connection', function (ConnectionInterface $conn) use (&$peer) {
147148
$peer = $conn->getRemoteAddress();
148149
});
149-
$port = $server->getPort();
150+
$port = $this->getPort($server);
150151

151152
$connector = new TcpConnector($loop);
152153
$promise = $connector->create('::1', $port);
@@ -215,4 +216,9 @@ public function testFailsToListenOnUriWIthHostname()
215216

216217
new Server('localhost:8080', $loop);
217218
}
219+
220+
private function getPort(ServerInterface $server)
221+
{
222+
return parse_url('tcp://' . $server->getAddress(), PHP_URL_PORT);
223+
}
218224
}

0 commit comments

Comments
 (0)