Skip to content

Commit

Permalink
Merge pull request #64 from clue-labs/context
Browse files Browse the repository at this point in the history
Support socket context options passed to Server
  • Loading branch information
clue authored Jan 26, 2017
2 parents 761ffae + 110f63e commit c1fc9e1
Show file tree
Hide file tree
Showing 3 changed files with 79 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
20 changes: 20 additions & 0 deletions tests/FunctionalServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,24 @@ public function testEmitsConnectionWithRemoteIpv6()

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

public function testAppliesContextOptionsToSocketStreamResource()
{
if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.13', '<')) {
// https://3v4l.org/hB4Tc
$this->markTestSkipped('Not supported on legacy HHVM < 3.13');
}

$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 c1fc9e1

Please sign in to comment.