@@ -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 ->resolveAddress ($ 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 ->resolveAddress ($ 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,7 +49,39 @@ public function createServer($port, $host = '127.0.0.1')
5249 });
5350 }
5451
55- protected function resolve ($ host )
52+ protected function resolveAddress ($ address )
53+ {
54+ if (strpos ($ address , ':// ' ) === false ) {
55+ $ address = 'udp:// ' . $ address ;
56+ }
57+ $ parts = parse_url ($ address );
58+
59+ if (!$ parts || !isset ($ parts ['host ' ])) {
60+ return When::resolve ($ address );
61+ }
62+
63+ // remove square brackets for IPv6 addresses
64+ $ host = trim ($ parts ['host ' ], '[] ' );
65+
66+ return $ this ->resolveHost ($ host )->then (function ($ host ) use ($ parts ) {
67+ $ address = $ parts ['scheme ' ] . ':// ' ;
68+
69+ if (isset ($ parts ['port ' ]) && strpos ($ host , ': ' ) !== false ) {
70+ // enclose IPv6 address in square brackets if a port will be appended
71+ $ host = '[ ' . $ host . '] ' ;
72+ }
73+
74+ $ address .= $ host ;
75+
76+ if (isset ($ parts ['port ' ])) {
77+ $ address .= ': ' . $ parts ['port ' ];
78+ }
79+
80+ return $ address ;
81+ });
82+ }
83+
84+ protected function resolveHost ($ host )
5685 {
5786 // there's no need to resolve if the host is already given as an IP address
5887 if (false !== filter_var ($ host , FILTER_VALIDATE_IP )) {
@@ -64,17 +93,8 @@ protected function resolve($host)
6493 }
6594
6695 if ($ this ->resolver === null ) {
67- return When::reject (\ Exception ('No resolver given in order to get IP address for given hostname ' ));
96+ return When::reject (new Exception ('No resolver given in order to get IP address for given hostname ' ));
6897 }
6998 return $ this ->resolver ->resolve ($ host );
7099 }
71-
72- public function createAddress ($ host , $ port )
73- {
74- if (strpos ($ host , ': ' ) !== false ) {
75- // enclose IPv6 address in square brackets
76- $ host = '[ ' . $ host . '] ' ;
77- }
78- return $ host . ': ' . $ port ;
79- }
80100}
0 commit comments