reactphp/react — pull request #252 by mathroc (2013-12-25 23:12:56+00:00)
Title: Open file descriptor as socket

If we want to use a react server behind systemd or launchd (socket activation) we should be able to open socket that are not tcp socket.

context:
on Linux, systemd is able to listen to a port instead of a program and start the program when a connexion is open on the port. this allow the system to boot faster, use fewer ressources while the service is not used and restart the service if it crashes (or if we killed it after an upgrade) without losing any connexion

for that, systemd start the program with a an ENV variable telling it which file descriptor to connect to. With React\Socket\Server we can only connect to tcp (eg: "tcp://0.0.0.0:8080") but in this case it should be able to connect to "php://fd/$fd"

I can solve this problem like that:

```php
loop = $loop;
parent::__construct($loop);
}

public function listen($socket, $nada = '')
{
$this->master = @stream_socket_server($socket);
if (false === $this->master) {
$message = "Could not bind to $socket: $errstr";
throw new ConnectionException($message, $errno);
}
stream_set_blocking($this->master, 0);

$that = $this;

$this->loop->addReadStream($this->master, function ($master) use ($that) {
$newSocket = stream_socket_accept($master);
if (false === $newSocket) {
$that->emit('error', array(new \RuntimeException('Error accepting new connection')));

return;
}
$that->handleConnection($newSocket);
});
}
}
```

but i think it would be an improvement it it could work out of the box. here the `listen` method should not have 2 parameters.

I propose the following changes

* `React\Socket\ServerInterface` should not impose the second parameter on the `listen` method.
* `React\Socket\RawServer` should be implemented with `listen($socket)`
* `React\Socket\Server` should extend `React\Socket\RawServer` and implement `listen($port, $host = '0.0.0.0')`