-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathHappyEyeBallsConnector.php
More file actions
57 lines (48 loc) · 1.56 KB
/
Copy pathHappyEyeBallsConnector.php
File metadata and controls
57 lines (48 loc) · 1.56 KB
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
<?php
namespace React\Socket;
use React\Dns\Resolver\ResolverInterface;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use function React\Promise\reject;
final class HappyEyeBallsConnector implements ConnectorInterface
{
private $connector;
private $resolver;
public function __construct(ConnectorInterface $connector, ResolverInterface $resolver)
{
$this->connector = $connector;
$this->resolver = $resolver;
}
public function connect($uri)
{
$original = $uri;
if (\strpos($uri, '://') === false) {
$uri = 'tcp://' . $uri;
$parts = \parse_url($uri);
if (isset($parts['scheme'])) {
unset($parts['scheme']);
}
} else {
$parts = \parse_url($uri);
}
if (!$parts || !isset($parts['host'])) {
return reject(new \InvalidArgumentException(
'Given URI "' . $original . '" is invalid (EINVAL)',
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : (\defined('PCNTL_EINVAL') ? \PCNTL_EINVAL : 22)
));
}
$host = \trim($parts['host'], '[]');
// skip DNS lookup / URI manipulation if this URI already contains an IP
if (@\inet_pton($host) !== false) {
return $this->connector->connect($original);
}
$builder = new HappyEyeBallsConnectionBuilder(
$this->connector,
$this->resolver,
$uri,
$host,
$parts
);
return $builder->connect();
}
}