Skip to content

Commit c1fc9e1

Browse files
authored
Merge pull request #64 from clue-labs/context
Support socket context options passed to Server
2 parents 761ffae + 110f63e commit c1fc9e1

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,30 @@ This method MUST NOT be called after calling [`shutdown()`](#shutdown).
168168
The `Server` class implements the [`ServerInterface`](#serverinterface) and
169169
is responsible for accepting plaintext TCP/IP connections.
170170

171+
```php
172+
$server = new Server($loop);
173+
174+
$server->listen(8080);
175+
```
176+
177+
Optionally, you can specify [socket context options](http://php.net/manual/en/context.socket.php)
178+
for the underlying stream socket resource like this:
179+
180+
```php
181+
$server = new Server($loop, array(
182+
'backlog' => 200,
183+
'so_reuseport' => true,
184+
'ipv6_v6only' => true
185+
));
186+
187+
$server->listen(8080, '::1');
188+
```
189+
190+
> Note that available [socket context options](http://php.net/manual/en/context.socket.php),
191+
their defaults and effects of changing these may vary depending on your system
192+
and/or PHP version.
193+
Passing unknown context options has no effect.
194+
171195
Whenever a client connects, it will emit a `connection` event with a connection
172196
instance implementing [`ConnectionInterface`](#connectioninterface):
173197

src/Server.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,42 @@ class Server extends EventEmitter implements ServerInterface
3333
{
3434
public $master;
3535
private $loop;
36-
37-
public function __construct(LoopInterface $loop)
36+
private $context;
37+
38+
/**
39+
* Creates a plaintext TCP/IP server.
40+
*
41+
* ```php
42+
* $server = new Server($loop);
43+
*
44+
* $server->listen(8080);
45+
* ```
46+
*
47+
* Optionally, you can specify [socket context options](http://php.net/manual/en/context.socket.php)
48+
* for the underlying stream socket resource like this:
49+
*
50+
* ```php
51+
* $server = new Server($loop, array(
52+
* 'backlog' => 200,
53+
* 'so_reuseport' => true,
54+
* 'ipv6_v6only' => true
55+
* ));
56+
*
57+
* $server->listen(8080, '::1');
58+
* ```
59+
*
60+
* Note that available [socket context options](http://php.net/manual/en/context.socket.php),
61+
* their defaults and effects of changing these may vary depending on your system
62+
* and/or PHP version.
63+
* Passing unknown context options has no effect.
64+
*
65+
* @param LoopInterface $loop
66+
* @param array $context
67+
*/
68+
public function __construct(LoopInterface $loop, array $context = array())
3869
{
3970
$this->loop = $loop;
71+
$this->context = $context;
4072
}
4173

4274
public function listen($port, $host = '127.0.0.1')
@@ -51,7 +83,7 @@ public function listen($port, $host = '127.0.0.1')
5183
$errno,
5284
$errstr,
5385
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN,
54-
stream_context_create()
86+
stream_context_create(array('socket' => $this->context))
5587
);
5688
if (false === $this->master) {
5789
$message = "Could not bind to tcp://$host:$port: $errstr";

tests/FunctionalServerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,24 @@ public function testEmitsConnectionWithRemoteIpv6()
162162

163163
$this->assertEquals('::1', $peer);
164164
}
165+
166+
public function testAppliesContextOptionsToSocketStreamResource()
167+
{
168+
if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.13', '<')) {
169+
// https://3v4l.org/hB4Tc
170+
$this->markTestSkipped('Not supported on legacy HHVM < 3.13');
171+
}
172+
173+
$loop = Factory::create();
174+
175+
$server = new Server($loop, array(
176+
'backlog' => 4
177+
));
178+
179+
$server->listen(0);
180+
181+
$all = stream_context_get_options($server->master);
182+
183+
$this->assertEquals(array('socket' => array('backlog' => 4)), $all);
184+
}
165185
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy