Skip to content

Commit

Permalink
Support socket context options passed to Server
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jan 25, 2017
1 parent 761ffae commit 8cf10b7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ This method MUST NOT be called after calling [`shutdown()`](#shutdown).
The `Server` class implements the [`ServerInterface`](#serverinterface) and
is responsible for accepting plaintext TCP/IP connections.

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

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

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(
'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),
their defaults and effects of changing these may vary depending on your system
and/or PHP version.
Passing unknown context options has no effect.

Whenever a client connects, it will emit a `connection` event with a connection
instance implementing [`ConnectionInterface`](#connectioninterface):

Expand Down
38 changes: 35 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,42 @@ class Server extends EventEmitter implements ServerInterface
{
public $master;
private $loop;

public function __construct(LoopInterface $loop)
private $context;

/**
* Creates a plaintext TCP/IP server.
*
* ```php
* $server = new Server($loop);
*
* $server->listen(8080);
* ```
*
* 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(
* '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),
* their defaults and effects of changing these may vary depending on your system
* and/or PHP version.
* Passing unknown context options has no effect.
*
* @param LoopInterface $loop
* @param array $context
*/
public function __construct(LoopInterface $loop, array $context = array())
{
$this->loop = $loop;
$this->context = $context;
}

public function listen($port, $host = '127.0.0.1')
Expand All @@ -51,7 +83,7 @@ public function listen($port, $host = '127.0.0.1')
$errno,
$errstr,
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN,
stream_context_create()
stream_context_create(array('socket' => $this->context))
);
if (false === $this->master) {
$message = "Could not bind to tcp://$host:$port: $errstr";
Expand Down
15 changes: 15 additions & 0 deletions tests/FunctionalServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,19 @@ public function testEmitsConnectionWithRemoteIpv6()

$this->assertEquals('::1', $peer);
}

public function testAppliesContextOptionsToSocketStreamResource()
{
$loop = Factory::create();

$server = new Server($loop, array(
'backlog' => 4
));

$server->listen(0);

$all = stream_context_get_options($server->master);

$this->assertEquals(array('socket' => array('backlog' => 4)), $all);
}
}

0 comments on commit 8cf10b7

Please sign in to comment.