Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace getPort() with getAddress() #67

Merged
merged 1 commit into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ For the code of the current stable 0.4.x release, checkout the
* [ServerInterface](#serverinterface)
* [connection event](#connection-event)
* [error event](#error-event)
* [getPort()](#getport)
* [getAddress()](#getaddress)
* [close()](#close)
* [Server](#server)
* [SecureServer](#secureserver)
Expand Down Expand Up @@ -115,18 +115,27 @@ Note that this is not a fatal error event, i.e. the server keeps listening for
new connections even after this event.


#### getPort()
#### getAddress()

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

```php
$port = $server->getPort();
echo 'Server listening on port ' . $port . PHP_EOL;
$address = $server->getAddress();
echo 'Server listening on ' . $address . PHP_EOL;
```

It will return the port number or `NULL` if it is unknown (not applicable to
this server socket or already closed).
It will return the full address (IP and port) or `NULL` if it is unknown
(not applicable to this server socket or already closed).

If this is a TCP/IP based server and you only want the local port, you may
use something like this:

```php
$address = $server->getAddress();
$port = parse_url('tcp://' . $address, PHP_URL_PORT);
echo 'Server listening on port ' . $port . PHP_EOL;
```

#### close()

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

```php
$server = new Server(0, $loop);
$port = $server->getPort();
$address = $server->getAddress();
```

In order to change the host the socket is listening on, you can provide an IP
Expand Down
2 changes: 1 addition & 1 deletion examples/01-echo.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@

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

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

$loop->run();
2 changes: 1 addition & 1 deletion examples/02-chat-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@

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

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

$loop->run();
2 changes: 1 addition & 1 deletion examples/03-benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@

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

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

$loop->run();
4 changes: 2 additions & 2 deletions src/SecureServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public function __construct(Server $tcp, LoopInterface $loop, array $context)
});
}

public function getPort()
public function getAddress()
{
return $this->tcp->getPort();
return $this->tcp->getAddress();
}

public function close()
Expand Down
15 changes: 11 additions & 4 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Server extends EventEmitter implements ServerInterface
*
* ```php
* $server = new Server(0, $loop);
* $port = $server->getPort();
* $address = $server->getAddress();
* ```
*
* In order to change the host the socket is listening on, you can provide an IP
Expand Down Expand Up @@ -174,15 +174,22 @@ public function handleConnection($socket)
$this->emit('connection', array($client));
}

public function getPort()
public function getAddress()
{
if (!is_resource($this->master)) {
return null;
}

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

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

return $address;
}

public function close()
Expand Down
23 changes: 20 additions & 3 deletions src/ServerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,28 @@
interface ServerInterface extends EventEmitterInterface
{
/**
* Returns the port this server is currently listening on
* Returns the full address (IP and port) this server is currently listening on
*
* @return ?int the port number or NULL if it is unknown (not applicable to this server socket or already closed)
* ```php
* $address = $server->getAddress();
* echo 'Server listening on ' . $address . PHP_EOL;
* ```
*
* It will return the full address (IP and port) or `NULL` if it is unknown
* (not applicable to this server socket or already closed).
*
* If this is a TCP/IP based server and you only want the local port, you may
* use something like this:
*
* ```php
* $address = $server->getAddress();
* $port = parse_url('tcp://' . $address, PHP_URL_PORT);
* echo 'Server listening on port ' . $port . PHP_EOL;
* ```
*
* @return ?string the full listening address (IP and port) or NULL if it is unknown (not applicable to this server socket or already closed)
*/
public function getPort();
public function getAddress();

/**
* Shuts down this listening socket
Expand Down
42 changes: 24 additions & 18 deletions tests/FunctionalSecureServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace React\Tests\Socket;

use React\EventLoop\Factory;
use React\SocketClient\TcpConnector;
use Clue\React\Block;
use React\Socket\SecureServer;
use React\SocketClient\SecureConnector;
use React\Stream\Stream;
use React\Socket\SecureServer;
use React\Socket\ConnectionInterface;
use React\Socket\Server;
use React\Socket\ServerInterface;
use React\SocketClient\TcpConnector;
use React\SocketClient\SecureConnector;
use Clue\React\Block;

class FunctionalSecureServerTest extends TestCase
{
Expand All @@ -31,7 +32,7 @@ public function testEmitsConnectionForNewConnection()
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
Expand All @@ -50,7 +51,7 @@ public function testWritesDataToConnection()
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$server->on('connection', function (ConnectionInterface $conn) {
$conn->write('foo');
Expand Down Expand Up @@ -78,7 +79,7 @@ public function testWritesDataInMultipleChunksToConnection()
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$server->on('connection', function (ConnectionInterface $conn) {
$conn->write(str_repeat('*', 400000));
Expand Down Expand Up @@ -111,7 +112,7 @@ public function testEmitsDataFromConnection()
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$once = $this->expectCallableOnceWith('foo');
$server->on('connection', function (ConnectionInterface $conn) use ($once) {
Expand Down Expand Up @@ -140,7 +141,7 @@ public function testEmitsDataInMultipleChunksFromConnection()
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$received = 0;
$server->on('connection', function (ConnectionInterface $conn) use (&$received) {
Expand Down Expand Up @@ -173,7 +174,7 @@ public function testPipesDataBackInMultipleChunksFromConnection()
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$server->on('connection', function (ConnectionInterface $conn) use (&$received) {
$conn->pipe($conn);
Expand Down Expand Up @@ -209,7 +210,7 @@ public function testEmitsConnectionForNewConnectionWithEncryptedCertificate()
'passphrase' => 'swordfish'
));
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
Expand All @@ -229,7 +230,7 @@ public function testEmitsErrorForServerWithInvalidCertificate()
));
$server->on('connection', $this->expectCallableNever());
$server->on('error', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
Expand All @@ -250,7 +251,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase
));
$server->on('connection', $this->expectCallableNever());
$server->on('error', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
Expand All @@ -272,7 +273,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph
));
$server->on('connection', $this->expectCallableNever());
$server->on('error', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
Expand All @@ -293,7 +294,7 @@ public function testEmitsErrorForConnectionWithPeerVerification()
));
$server->on('connection', $this->expectCallableNever());
$server->on('error', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => true
Expand All @@ -314,7 +315,7 @@ public function testEmitsErrorIfConnectionIsCancelled()
));
$server->on('connection', $this->expectCallableNever());
$server->on('error', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
Expand All @@ -336,7 +337,7 @@ public function testEmitsNothingIfConnectionIsIdle()
));
$server->on('connection', $this->expectCallableNever());
$server->on('error', $this->expectCallableNever());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new TcpConnector($loop);
$promise = $connector->create('127.0.0.1', $port);
Expand All @@ -355,7 +356,7 @@ public function testEmitsErrorIfConnectionIsNotSecureHandshake()
));
$server->on('connection', $this->expectCallableNever());
$server->on('error', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

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

new SecureServer($server, $loop, array());
}

private function getPort(ServerInterface $server)
{
return parse_url($server->getAddress(), PHP_URL_PORT);
}
}
24 changes: 15 additions & 9 deletions tests/FunctionalServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace React\Tests\Socket;

use React\EventLoop\Factory;
use React\SocketClient\TcpConnector;
use React\Socket\Server;
use Clue\React\Block;
use React\Socket\ConnectionException;
use React\Socket\ConnectionInterface;
use React\Socket\ServerInterface;
use React\SocketClient\TcpConnector;
use Clue\React\Block;

class FunctionalServerTest extends TestCase
{
Expand All @@ -17,7 +18,7 @@ public function testEmitsConnectionForNewConnection()

$server = new Server(0, $loop);
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new TcpConnector($loop);
$promise = $connector->create('127.0.0.1', $port);
Expand All @@ -36,7 +37,7 @@ public function testEmitsConnectionWithRemoteIp()
$server->on('connection', function (ConnectionInterface $conn) use (&$peer) {
$peer = $conn->getRemoteAddress();
});
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new TcpConnector($loop);
$promise = $connector->create('127.0.0.1', $port);
Expand All @@ -59,7 +60,7 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer()
$peer = $conn->getRemoteAddress();
});
});
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new TcpConnector($loop);
$promise = $connector->create('127.0.0.1', $port);
Expand All @@ -82,7 +83,7 @@ public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedL
$conn->close();
$peer = $conn->getRemoteAddress();
});
$port = $server->getPort();
$port = $this->getPort($server);

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

$server = new Server(0, $loop);
$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

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

$server->on('connection', $this->expectCallableOnce());
$port = $server->getPort();
$port = $this->getPort($server);

$connector = new TcpConnector($loop);
$promise = $connector->create('::1', $port);
Expand All @@ -146,7 +147,7 @@ public function testEmitsConnectionWithRemoteIpv6()
$server->on('connection', function (ConnectionInterface $conn) use (&$peer) {
$peer = $conn->getRemoteAddress();
});
$port = $server->getPort();
$port = $this->getPort($server);

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

new Server('localhost:8080', $loop);
}

private function getPort(ServerInterface $server)
{
return parse_url('tcp://' . $server->getAddress(), PHP_URL_PORT);
}
}
Loading