Skip to content

Commit 157a568

Browse files
committed
Use async resolver when binding server to hostname
1 parent 97910a0 commit 157a568

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Datagram/Factory.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function createClient($host, $port)
2323
{
2424
$factory = $this;
2525
$loop = $this->loop;
26+
2627
return $this->resolve($host)->then(function ($ip) use ($loop, $port, $factory) {
2728
$address = $factory->createAddress($ip, $port);
2829
$socket = stream_socket_client('udp://' . $address, $errno, $errstr);
@@ -36,14 +37,19 @@ public function createClient($host, $port)
3637

3738
public function createServer($port, $host = '127.0.0.1')
3839
{
39-
$address = $this->createAddress($host, $port);
40+
$factory = $this;
41+
$loop = $this->loop;
4042

41-
$socket = stream_socket_server("udp://" . $address, $errno, $errstr, STREAM_SERVER_BIND);
42-
if (!$socket) {
43-
return When::reject(new Exception('Unable to create server socket: ' . $errstr, $errno));
44-
}
43+
return $this->resolve($host)->then(function ($ip) use ($loop, $port, $factory) {
44+
$address = $factory->createAddress($ip, $port);
4545

46-
return When::resolve(new Socket($this->loop, $socket));
46+
$socket = stream_socket_server("udp://" . $address, $errno, $errstr, STREAM_SERVER_BIND);
47+
if (!$socket) {
48+
throw new Exception('Unable to create server socket: ' . $errstr, $errno);
49+
}
50+
51+
return new Socket($loop, $socket);
52+
});
4753
}
4854

4955
protected function resolve($host)

0 commit comments

Comments
 (0)