|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Socket; |
| 4 | + |
| 5 | +use Evenement\EventEmitter; |
| 6 | +use React\EventLoop\LoopInterface; |
| 7 | +use InvalidArgumentException; |
| 8 | +use RuntimeException; |
| 9 | + |
| 10 | +/** |
| 11 | + * The `UnixServer` class implements the `ServerInterface` and |
| 12 | + * is responsible for accepting plaintext connections on unix domain sockets. |
| 13 | + * |
| 14 | + * ```php |
| 15 | + * $server = new UnixServer('unix:///tmp/app.sock', $loop); |
| 16 | + * ``` |
| 17 | + * |
| 18 | + * See also the `ServerInterface` for more details. |
| 19 | + * |
| 20 | + * @see ServerInterface |
| 21 | + * @see ConnectionInterface |
| 22 | + */ |
| 23 | +final class UnixServer extends EventEmitter implements ServerInterface |
| 24 | +{ |
| 25 | + private $master; |
| 26 | + private $loop; |
| 27 | + private $listening = false; |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a plaintext socket server and starts listening on the given unix socket |
| 31 | + * |
| 32 | + * This starts accepting new incoming connections on the given address. |
| 33 | + * See also the `connection event` documented in the `ServerInterface` |
| 34 | + * for more details. |
| 35 | + * |
| 36 | + * ```php |
| 37 | + * $server = new UnixServer('unix:///tmp/app.sock', $loop); |
| 38 | + * ``` |
| 39 | + * |
| 40 | + * @param string $path |
| 41 | + * @param LoopInterface $loop |
| 42 | + * @param array $context |
| 43 | + * @throws InvalidArgumentException if the listening address is invalid |
| 44 | + * @throws RuntimeException if listening on this address fails (already in use etc.) |
| 45 | + */ |
| 46 | + public function __construct($path, LoopInterface $loop, array $context = array()) |
| 47 | + { |
| 48 | + $this->loop = $loop; |
| 49 | + |
| 50 | + if (strpos($path, '://') === false) { |
| 51 | + $path = 'unix://' . $path; |
| 52 | + } elseif (substr($path, 0, 7) !== 'unix://') { |
| 53 | + throw new \InvalidArgumentException('Given URI "' . $path . '" is invalid'); |
| 54 | + } |
| 55 | + |
| 56 | + $this->master = @stream_socket_server( |
| 57 | + $path, |
| 58 | + $errno, |
| 59 | + $errstr, |
| 60 | + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, |
| 61 | + stream_context_create(array('socket' => $context)) |
| 62 | + ); |
| 63 | + if (false === $this->master) { |
| 64 | + throw new RuntimeException('Failed to listen on unix domain socket "' . $path . '": ' . $errstr, $errno); |
| 65 | + } |
| 66 | + stream_set_blocking($this->master, 0); |
| 67 | + |
| 68 | + $this->resume(); |
| 69 | + } |
| 70 | + |
| 71 | + public function getAddress() |
| 72 | + { |
| 73 | + if (!is_resource($this->master)) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + return 'unix://' . stream_socket_get_name($this->master, false); |
| 78 | + } |
| 79 | + |
| 80 | + public function pause() |
| 81 | + { |
| 82 | + if (!$this->listening) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $this->loop->removeReadStream($this->master); |
| 87 | + $this->listening = false; |
| 88 | + } |
| 89 | + |
| 90 | + public function resume() |
| 91 | + { |
| 92 | + if ($this->listening || !is_resource($this->master)) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + $that = $this; |
| 97 | + $this->loop->addReadStream($this->master, function ($master) use ($that) { |
| 98 | + $newSocket = @stream_socket_accept($master); |
| 99 | + if (false === $newSocket) { |
| 100 | + $that->emit('error', array(new \RuntimeException('Error accepting new connection'))); |
| 101 | + |
| 102 | + return; |
| 103 | + } |
| 104 | + $that->handleConnection($newSocket); |
| 105 | + }); |
| 106 | + $this->listening = true; |
| 107 | + } |
| 108 | + |
| 109 | + public function close() |
| 110 | + { |
| 111 | + if (!is_resource($this->master)) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + $this->pause(); |
| 116 | + fclose($this->master); |
| 117 | + $this->removeAllListeners(); |
| 118 | + } |
| 119 | + |
| 120 | + /** @internal */ |
| 121 | + public function handleConnection($socket) |
| 122 | + { |
| 123 | + $connection = new Connection($socket, $this->loop); |
| 124 | + $connection->unix = true; |
| 125 | + |
| 126 | + $this->emit('connection', array( |
| 127 | + $connection |
| 128 | + )); |
| 129 | + } |
| 130 | +} |
0 commit comments