Skip to content

Commit 1c3e2b2

Browse files
committed
First step towards unifying socket addresses
1 parent be74f01 commit 1c3e2b2

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Datagram/Factory.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ public function __construct(LoopInterface $loop, Resolver $resolver = null)
1919
$this->resolver = $resolver;
2020
}
2121

22-
public function createClient($host, $port)
22+
public function createClient($address)
2323
{
2424
$factory = $this;
2525
$loop = $this->loop;
2626

27-
return $this->resolve($host)->then(function ($ip) use ($loop, $port, $factory) {
28-
$address = $factory->createAddress($ip, $port);
29-
$socket = stream_socket_client('udp://' . $address, $errno, $errstr);
27+
return $this->resolve($address)->then(function ($address) use ($loop) {
28+
$socket = stream_socket_client($address, $errno, $errstr);
3029
if (!$socket) {
3130
throw new Exception('Unable to create client socket: ' . $errstr, $errno);
3231
}
@@ -35,15 +34,13 @@ public function createClient($host, $port)
3534
});
3635
}
3736

38-
public function createServer($port, $host = '127.0.0.1')
37+
public function createServer($address)
3938
{
4039
$factory = $this;
4140
$loop = $this->loop;
4241

43-
return $this->resolve($host)->then(function ($ip) use ($loop, $port, $factory) {
44-
$address = $factory->createAddress($ip, $port);
45-
46-
$socket = stream_socket_server("udp://" . $address, $errno, $errstr, STREAM_SERVER_BIND);
42+
return $this->resolve($address)->then(function ($address) use ($loop) {
43+
$socket = stream_socket_server($address, $errno, $errstr, STREAM_SERVER_BIND);
4744
if (!$socket) {
4845
throw new Exception('Unable to create server socket: ' . $errstr, $errno);
4946
}
@@ -52,8 +49,12 @@ public function createServer($port, $host = '127.0.0.1')
5249
});
5350
}
5451

55-
protected function resolve($host)
52+
protected function resolve($address)
5653
{
54+
if (strpos($address, '://') === false) {
55+
$address = 'udp://' . $address;
56+
}
57+
$parts = parse_url($address);
5758
// there's no need to resolve if the host is already given as an IP address
5859
if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
5960
return When::resolve($host);

0 commit comments

Comments
 (0)