Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace React\Socket;
4
 
5
use React\EventLoop\LoopInterface;
6
use React\Promise;
7
use InvalidArgumentException;
8
use RuntimeException;
9
 
10
/**
11
 * Unix domain socket connector
12
 *
13
 * Unix domain sockets use atomic operations, so we can as well emulate
14
 * async behavior.
15
 */
16
final class UnixConnector implements ConnectorInterface
17
{
18
    private $loop;
19
 
20
    public function __construct(LoopInterface $loop)
21
    {
22
        $this->loop = $loop;
23
    }
24
 
25
    public function connect($path)
26
    {
27
        if (\strpos($path, '://') === false) {
28
            $path = 'unix://' . $path;
29
        } elseif (\substr($path, 0, 7) !== 'unix://') {
30
            return Promise\reject(new \InvalidArgumentException('Given URI "' . $path . '" is invalid'));
31
        }
32
 
33
        $resource = @\stream_socket_client($path, $errno, $errstr, 1.0);
34
 
35
        if (!$resource) {
36
            return Promise\reject(new \RuntimeException('Unable to connect to unix domain socket "' . $path . '": ' . $errstr, $errno));
37
        }
38
 
39
        $connection = new Connection($resource, $this->loop);
40
        $connection->unix = true;
41
 
42
        return Promise\resolve($connection);
43
    }
44
}