Skip to content

Replace listen() call with URIs passed to constructor #61

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

Merged
merged 4 commits into from
Jan 27, 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
58 changes: 18 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ For the code of the current stable 0.4.x release, checkout the
* [ServerInterface](#serverinterface)
* [connection event](#connection-event)
* [error event](#error-event)
* [listen()](#listen)
* [getPort()](#getport)
* [close()](#close)
* [Server](#server)
Expand All @@ -37,7 +36,7 @@ Here is a server that closes the connection if you send it anything:
```php
$loop = React\EventLoop\Factory::create();

$socket = new React\Socket\Server($loop);
$socket = new React\Socket\Server(8080, $loop);
$socket->on('connection', function (ConnectionInterface $conn) {
$conn->write("Hello " . $conn->getRemoteAddress() . "!\n");
$conn->write("Welcome to this amazing server!\n");
Expand All @@ -47,7 +46,6 @@ $socket->on('connection', function (ConnectionInterface $conn) {
$conn->close();
});
});
$socket->listen(1337);

$loop->run();
```
Expand All @@ -62,7 +60,7 @@ For anything more complex, consider using the
```php
$loop = React\EventLoop\Factory::create();

$client = stream_socket_client('tcp://127.0.0.1:1337');
$client = stream_socket_client('tcp://127.0.0.1:8080');
$conn = new React\Stream\Stream($client, $loop);
$conn->pipe(new React\Stream\Stream(STDOUT, $loop));
$conn->write("Hello World!\n");
Expand Down Expand Up @@ -116,41 +114,19 @@ $server->on('error', function (Exception $e) {
Note that this is not a fatal error event, i.e. the server keeps listening for
new connections even after this event.

#### listen()

The `listen(int $port, string $host = '127.0.0.1'): void` method can be used to
start listening on the given address.

This starts accepting new incoming connections on the given address.
See also the [connection event](#connection-event) for more details.

```php
$server->listen(8080);
```

By default, the server will listen on the localhost address and will not be
reachable from the outside.
You can change the host the socket is listening on through a second parameter
provided to the listen method:

```php
$socket->listen(1337, '192.168.0.1');
```

This method MUST NOT be called more than once on the same instance.

#### getPort()

The `getPort(): int` method can be used to
The `getPort(): ?int` method can be used to
return the port this server is currently listening on.

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

This method MUST NOT be called before calling [`listen()`](#listen).
This method MUST NOT be called after calling [`close()`](#close).
It will return the port number or `NULL` if it is unknown (not applicable to
this server socket or already closed).

#### close()

Expand All @@ -164,31 +140,35 @@ echo 'Shutting down server socket' . PHP_EOL;
$server->close();
```

This method MUST NOT be called before calling [`listen()`](#listen).
This method MUST NOT be called after calling [`close()`](#close).
Calling this method more than once on the same instance is a NO-OP.

### Server

The `Server` class implements the [`ServerInterface`](#serverinterface) and
is responsible for accepting plaintext TCP/IP connections.

```php
$server = new Server($loop);
$server = new Server(8080, $loop);
```

By default, the server will listen on the localhost address and will not be
reachable from the outside.
You can change the host the socket is listening on through a first parameter
provided to the constructor:

$server->listen(8080);
```php
$server = new Server('192.168.0.1:8080', $loop);
```

Optionally, you can specify [socket context options](http://php.net/manual/en/context.socket.php)
for the underlying stream socket resource like this:

```php
$server = new Server($loop, array(
$server = new Server('[::1]:8080', $loop, array(
'backlog' => 200,
'so_reuseport' => true,
'ipv6_v6only' => true
));

$server->listen(8080, '::1');
```

> Note that available [socket context options](http://php.net/manual/en/context.socket.php),
Expand Down Expand Up @@ -226,13 +206,10 @@ which in its most basic form may look something like this if you're using a
PEM encoded certificate file:

```php
$server = new Server($loop);

$server = new Server(8000, $loop);
$server = new SecureServer($server, $loop, array(
'local_cert' => 'server.pem'
));

$server->listen(8000);
```

> Note that the certificate file will not be loaded on instantiation but when an
Expand All @@ -244,6 +221,7 @@ If your private key is encrypted with a passphrase, you have to specify it
like this:

```php
$server = new Server(8000, $loop);
$server = new SecureServer($server, $loop, array(
'local_cert' => 'server.pem',
'passphrase' => 'secret'
Expand Down
4 changes: 1 addition & 3 deletions examples/01-echo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

$loop = Factory::create();

$server = new Server($loop);
$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop);

// secure TLS mode if certificate is given as second parameter
if (isset($argv[2])) {
Expand All @@ -29,8 +29,6 @@
));
}

$server->listen(isset($argv[1]) ? $argv[1] : 0);

$server->on('connection', function (ConnectionInterface $conn) use ($loop) {
echo '[connected]' . PHP_EOL;
$conn->pipe($conn);
Expand Down
4 changes: 1 addition & 3 deletions examples/02-chat-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

$loop = Factory::create();

$server = new Server($loop);
$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop);

// secure TLS mode if certificate is given as second parameter
if (isset($argv[2])) {
Expand All @@ -29,8 +29,6 @@
));
}

$server->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');

$clients = array();

$server->on('connection', function (ConnectionInterface $client) use (&$clients) {
Expand Down
4 changes: 1 addition & 3 deletions examples/03-benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

$loop = Factory::create();

$server = new Server($loop);
$server = new Server(isset($argv[1]) ? $argv[1] : 0, $loop);

// secure TLS mode if certificate is given as second parameter
if (isset($argv[2])) {
Expand All @@ -34,8 +34,6 @@
));
}

$server->listen(isset($argv[1]) ? $argv[1] : 0);

$server->on('connection', function (ConnectionInterface $conn) use ($loop) {
echo '[connected]' . PHP_EOL;

Expand Down
12 changes: 7 additions & 5 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class Connection extends Stream implements ConnectionInterface
{
public function handleClose()
{
if (is_resource($this->stream)) {
// http://chat.stackoverflow.com/transcript/message/7727858#7727858
stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
stream_set_blocking($this->stream, false);
fclose($this->stream);
if (!is_resource($this->stream)) {
return;
}

// http://chat.stackoverflow.com/transcript/message/7727858#7727858
stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
stream_set_blocking($this->stream, false);
fclose($this->stream);
}

public function getRemoteAddress()
Expand Down
21 changes: 8 additions & 13 deletions src/SecureServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,24 @@
class SecureServer extends EventEmitter implements ServerInterface
{
private $tcp;
private $context;
private $loop;
private $encryption;

public function __construct(Server $tcp, LoopInterface $loop, array $context)
{
if (!is_resource($tcp->master)) {
throw new ConnectionException('TCP server already shut down');
}

// default to empty passphrase to surpress blocking passphrase prompt
$context += array(
'passphrase' => ''
);

foreach ($context as $name => $value) {
stream_context_set_option($tcp->master, 'ssl', $name, $value);
}

$this->tcp = $tcp;
$this->context = $context;
$this->loop = $loop;
$this->encryption = new StreamEncryption($loop);

$that = $this;
Expand All @@ -64,15 +68,6 @@ public function __construct(Server $tcp, LoopInterface $loop, array $context)
});
}

public function listen($port, $host = '127.0.0.1')
{
$this->tcp->listen($port, $host);

foreach ($this->context as $name => $value) {
stream_context_set_option($this->tcp->master, 'ssl', $name, $value);
}
}

public function getPort()
{
return $this->tcp->getPort();
Expand Down
Loading