-
Notifications
You must be signed in to change notification settings - Fork 24
/
Connector.php
110 lines (87 loc) · 3.01 KB
/
Connector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace React\SocketClient;
use React\EventLoop\LoopInterface;
use React\Dns\Resolver\Resolver;
use React\Stream\Stream;
use React\Promise;
use React\Promise\Deferred;
class Connector implements ConnectorInterface
{
private $loop;
private $resolver;
public function __construct(LoopInterface $loop, Resolver $resolver)
{
$this->loop = $loop;
$this->resolver = $resolver;
}
public function create($host, $port)
{
return $this
->resolveHostname($host)
->then(function ($address) use ($port, $host) {
return $this->createSocketForAddress($address, $port, $host);
});
}
public function createSocketForAddress($address, $port, $hostName = null)
{
$url = $this->getSocketUrl($address, $port);
$contextOpts = array();
if ($hostName !== null) {
$contextOpts['ssl']['SNI_enabled'] = true;
$contextOpts['ssl']['SNI_server_name'] = $hostName;
}
$flags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
$context = stream_context_create($contextOpts);
$socket = stream_socket_client($url, $errno, $errstr, 0, $flags, $context);
if (!$socket) {
return Promise\reject(new \RuntimeException(
sprintf("connection to %s:%d failed: %s", $address, $port, $errstr),
$errno
));
}
stream_set_blocking($socket, 0);
// wait for connection
return $this
->waitForStreamOnce($socket)
->then(array($this, 'checkConnectedSocket'))
->then(array($this, 'handleConnectedSocket'));
}
protected function waitForStreamOnce($stream)
{
$deferred = new Deferred();
$loop = $this->loop;
$this->loop->addWriteStream($stream, function ($stream) use ($loop, $deferred) {
$loop->removeWriteStream($stream);
$deferred->resolve($stream);
});
return $deferred->promise();
}
public function checkConnectedSocket($socket)
{
// The following hack looks like the only way to
// detect connection refused errors with PHP's stream sockets.
if (false === stream_socket_get_name($socket, true)) {
return Promise\reject(new ConnectionException('Connection refused'));
}
return Promise\resolve($socket);
}
public function handleConnectedSocket($socket)
{
return new Stream($socket, $this->loop);
}
protected function getSocketUrl($host, $port)
{
if (strpos($host, ':') !== false) {
// enclose IPv6 addresses in square brackets before appending port
$host = '[' . $host . ']';
}
return sprintf('tcp://%s:%s', $host, $port);
}
protected function resolveHostname($host)
{
if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
return Promise\resolve($host);
}
return $this->resolver->resolve($host);
}
}