Skip to content

Commit 13001a9

Browse files
committed
TCP socket context options should be nested in outer array
1 parent 2084961 commit 13001a9

3 files changed

Lines changed: 51 additions & 11 deletions

File tree

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,25 +377,29 @@ $second = new Server(8080, $loop);
377377
```
378378

379379
> Note that these error conditions may vary depending on your system and/or
380-
configuration.
381-
See the exception message and code for more details about the actual error
382-
condition.
380+
configuration.
381+
See the exception message and code for more details about the actual error
382+
condition.
383383

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

387387
```php
388388
$server = new Server('[::1]:8080', $loop, array(
389-
'backlog' => 200,
390-
'so_reuseport' => true,
391-
'ipv6_v6only' => true
389+
'tcp' => array(
390+
'backlog' => 200,
391+
'so_reuseport' => true,
392+
'ipv6_v6only' => true
393+
)
392394
));
393395
```
394396

395397
> Note that available [socket context options](http://php.net/manual/en/context.socket.php),
396-
their defaults and effects of changing these may vary depending on your system
397-
and/or PHP version.
398-
Passing unknown context options has no effect.
398+
their defaults and effects of changing these may vary depending on your system
399+
and/or PHP version.
400+
Passing unknown context options has no effect.
401+
For BC reasons, you can also pass the TCP socket context options as a simple
402+
array without wrapping this in another array under the `tcp` key.
399403

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

src/Server.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@ final class Server extends EventEmitter implements ServerInterface
1111

1212
public function __construct($uri, LoopInterface $loop, array $context = array())
1313
{
14-
$server = new TcpServer($uri, $loop, $context);
14+
// sanitize TCP context options if not properly wrapped
15+
if ($context && !isset($context['tcp'])) {
16+
$context = array('tcp' => $context);
17+
}
18+
19+
// apply default options if not explicitly given
20+
$context += array(
21+
'tcp' => array(),
22+
);
23+
24+
$server = new TcpServer($uri, $loop, $context['tcp']);
1525
$this->server = $server;
1626

1727
$that = $this;

tests/ServerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use React\EventLoop\Factory;
66
use React\Socket\Server;
77
use Clue\React\Block;
8+
use React\Socket\ConnectionInterface;
89

910
class ServerTest extends TestCase
1011
{
@@ -81,4 +82,29 @@ public function testDoesNotAllowConnectionToClosedServer()
8182

8283
$this->assertFalse($client);
8384
}
85+
86+
public function testEmitsConnectionWithInheritedContextOptions()
87+
{
88+
if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.13', '<')) {
89+
// https://3v4l.org/hB4Tc
90+
$this->markTestSkipped('Not supported on legacy HHVM < 3.13');
91+
}
92+
93+
$loop = Factory::create();
94+
95+
$server = new Server(0, $loop, array(
96+
'backlog' => 4
97+
));
98+
99+
$all = null;
100+
$server->on('connection', function (ConnectionInterface $conn) use (&$all) {
101+
$all = stream_context_get_options($conn->stream);
102+
});
103+
104+
$client = stream_socket_client($server->getAddress());
105+
106+
Block\sleep(0.1, $loop);
107+
108+
$this->assertEquals(array('socket' => array('backlog' => 4)), $all);
109+
}
84110
}

0 commit comments

Comments
 (0)