Skip to content

Support socket context options passed to Server #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
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