Skip to content

Allow setting socket/tcp options for server sockets. #10

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

Closed
wants to merge 4 commits into from
Closed
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,33 @@ send it a string.

$loop->run();
```

## Advanced Usage

### Disabling TCP Nagle
Nagle helps to improve the efficiency of TCP communication by reducing the
number of packets that need to be sent over the network.

This helps to reduce TCP overheads.

Sometimes you want to disable Nagle to ensure that data will be sent
immediately.
```php
$socket = new React\Socket\Server($loop);
$socket->setTCPOption(TCP_NODELAY, true);
$socket->on('connection', function ($conn) {
$conn->write("This data will be sent.\n");
usleep(200);
$conn->write("... after 200ms!\n");
$conn->close();
});
$socket->listen(1337);
```

### Setting `SO_REUSEADDR`
The `SO_REUSEADDR` socket option allows a socket to forcibly bind to a
port in use by another socket. This means multiple processes could be
launched listening on the same port.
```php
$socket->setSocketOption(SO_REUSEADDR, true);
```
29 changes: 29 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Server extends EventEmitter implements ServerInterface
{
public $master;
private $loop;
private $options = array();

public function __construct(LoopInterface $loop)
{
Expand Down Expand Up @@ -45,6 +46,13 @@ public function handleConnection($socket)
{
stream_set_blocking($socket, 0);

// apply any socket options on the connection!
foreach ($this->options as $socket_level => $options) {
foreach($options as $option_name => $option_value) {
socket_set_option($socket, $socket_level, $option_name, $option_value);
}
}

$client = $this->createConnection($socket);

$this->emit('connection', array($client));
Expand All @@ -68,4 +76,25 @@ public function createConnection($socket)
{
return new Connection($socket, $this->loop);
}

public function setOption($option_level, $option_name, $option_value)
{
if (!is_array($this->options[$option_level])) {
$this->options[$option_level] = array();
}

$this->options[$option_level][$option_name] = $option_value;

return true;
}

public function setSocketOption($option_name, $option_value)
{
return $this->setOption(SOL_SOCKET, $option_name, $option_value);
}

public function setTCPOption($option_name, $option_value)
{
return $this->setOption(SOL_TCP, $option_name, $option_value);
}
}
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