33namespace React \HttpClient ;
44
55use React \EventLoop \LoopInterface ;
6- use React \Stream \Stream ;
76use React \Dns \Resolver \Resolver ;
7+ use React \Stream \Stream ;
8+ use React \Promise \Deferred ;
9+ use React \Promise \FulfilledPromise ;
10+ use React \Promise \RejectedPromise ;
811
912class ConnectionManager implements ConnectionManagerInterface
1013{
@@ -17,73 +20,70 @@ public function __construct(LoopInterface $loop, Resolver $resolver)
1720 $ this ->resolver = $ resolver ;
1821 }
1922
20- public function getConnection ($ callback , $ host , $ port )
23+ public function getConnection ($ host , $ port )
2124 {
2225 $ that = $ this ;
23- $ this ->resolve (function ($ address , $ error = null ) use ($ that , $ callback , $ host , $ port ) {
24- if ($ error ) {
25- call_user_func ($ callback , null , new \RuntimeException (
26- sprintf ("failed to resolve %s " , $ host ),
27- 0 ,
28- $ error
29- ));
30- return ;
31- }
32- $ that ->getConnectionForAddress ($ callback , $ address , $ port );
33- }, $ host );
26+
27+ return $ this
28+ ->resolveHostname ($ host )
29+ ->then (function ($ address ) use ($ port , $ that ) {
30+ return $ that ->getConnectionForAddress ($ address , $ port );
31+ });
3432 }
3533
36- public function getConnectionForAddress ($ callback , $ address , $ port )
34+ public function getConnectionForAddress ($ address , $ port )
3735 {
3836 $ url = $ this ->getSocketUrl ($ address , $ port );
3937
4038 $ socket = stream_socket_client ($ url , $ errno , $ errstr , ini_get ("default_socket_timeout " ), STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT );
4139
4240 if (!$ socket ) {
43- call_user_func ( $ callback , null , new \RuntimeException (
41+ return new RejectedPromise ( new \RuntimeException (
4442 sprintf ("connection to %s:%d failed: %s " , $ addresss , $ port , $ errstr ),
4543 $ errno
4644 ));
47- return ;
4845 }
4946
5047 stream_set_blocking ($ socket , 0 );
5148
5249 // wait for connection
5350
54- $ loop = $ this ->loop ;
55- $ that = $ this ;
51+ return $ this
52+ ->waitForStreamOnce ($ socket )
53+ ->then (array ($ this , 'handleConnectedSocket ' ));
54+ }
5655
57- $ this ->loop ->addWriteStream ($ socket , function () use ($ that , $ callback , $ socket , $ loop ) {
56+ protected function waitForStreamOnce ($ stream )
57+ {
58+ $ deferred = new Deferred ();
5859
59- $ loop -> removeWriteStream ( $ socket ) ;
60+ $ loop = $ this -> loop ;
6061
61- $ that ->handleConnectedSocket ($ callback , $ socket );
62+ $ this ->loop ->addWriteStream ($ stream , function ($ stream ) use ($ loop , $ deferred ) {
63+ $ loop ->removeWriteStream ($ stream );
64+ $ deferred ->resolve ($ stream );
6265 });
66+
67+ return $ deferred ->promise ();
6368 }
6469
65- public function handleConnectedSocket ($ callback , $ socket )
70+ public function handleConnectedSocket ($ socket )
6671 {
67- call_user_func ( $ callback , new Stream ($ socket , $ this ->loop ) );
72+ return new Stream ($ socket , $ this ->loop );
6873 }
6974
7075 protected function getSocketUrl ($ host , $ port )
7176 {
7277 return sprintf ('tcp://%s:%s ' , $ host , $ port );
7378 }
7479
75- protected function resolve ( $ callback , $ host )
80+ protected function resolveHostname ( $ host )
7681 {
7782 if (false !== filter_var ($ host , FILTER_VALIDATE_IP )) {
78- call_user_func ($ callback , $ host );
79- return ;
83+ return new FulfilledPromise ($ host );
8084 }
8185
82- $ this ->resolver ->resolve ($ host , function ($ address ) use ($ callback ) {
83- call_user_func ($ callback , $ address );
84- }, function ($ error ) use ($ callback ) {
85- call_user_func ($ callback , null , $ error );
86- });
86+ return $ this ->resolver ->resolve ($ host );
8787 }
8888}
8989
0 commit comments