From 8cf10b7920790e8c4764e361214a044c43c0b6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 24 Jan 2017 14:25:31 +0100 Subject: [PATCH 1/2] Support socket context options passed to Server --- README.md | 24 +++++++++++++++++++++ src/Server.php | 38 +++++++++++++++++++++++++++++++--- tests/FunctionalServerTest.php | 15 ++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2fc31025..e7d7fec6 100644 --- a/README.md +++ b/README.md @@ -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): diff --git a/src/Server.php b/src/Server.php index a8674f02..f108f1a8 100644 --- a/src/Server.php +++ b/src/Server.php @@ -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') @@ -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"; diff --git a/tests/FunctionalServerTest.php b/tests/FunctionalServerTest.php index 5f88d545..1b606ec6 100644 --- a/tests/FunctionalServerTest.php +++ b/tests/FunctionalServerTest.php @@ -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); + } } From 110f63ec9aeff8f9a13cebf36600c66912bed107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 24 Jan 2017 18:44:17 +0100 Subject: [PATCH 2/2] Skip context tests on legacy HHVM < 3.13 See https://3v4l.org/hB4Tc for the basic gist of this --- tests/FunctionalServerTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/FunctionalServerTest.php b/tests/FunctionalServerTest.php index 1b606ec6..3b2610b8 100644 --- a/tests/FunctionalServerTest.php +++ b/tests/FunctionalServerTest.php @@ -165,6 +165,11 @@ public function testEmitsConnectionWithRemoteIpv6() 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(