@@ -90,21 +90,26 @@ public function connect($uri)
90
90
91
91
// wait for connection
92
92
93
- return $ this
94
- ->waitForStreamOnce ($ socket )
95
- ->then (array ($ this , 'checkConnectedSocket ' ))
96
- ->then (array ($ this , 'handleConnectedSocket ' ));
93
+ return $ this ->waitForStreamOnce ($ socket );
97
94
}
98
95
99
96
private function waitForStreamOnce ($ stream )
100
97
{
101
98
$ loop = $ this ->loop ;
102
99
103
- return new Promise \Promise (function ($ resolve ) use ($ loop , $ stream ) {
104
- $ loop ->addWriteStream ($ stream , function ($ stream ) use ($ loop , $ resolve ) {
100
+ return new Promise \Promise (function ($ resolve, $ reject ) use ($ loop , $ stream ) {
101
+ $ loop ->addWriteStream ($ stream , function ($ stream ) use ($ loop , $ resolve, $ reject ) {
105
102
$ loop ->removeWriteStream ($ stream );
106
103
107
- $ resolve ($ stream );
104
+ // The following hack looks like the only way to
105
+ // detect connection refused errors with PHP's stream sockets.
106
+ if (false === stream_socket_get_name ($ stream , true )) {
107
+ fclose ($ stream );
108
+
109
+ $ reject (new \RuntimeException ('Connection refused ' ));
110
+ } else {
111
+ $ resolve (new Connection ($ stream , $ loop ));
112
+ }
108
113
});
109
114
}, function () use ($ loop , $ stream ) {
110
115
$ loop ->removeWriteStream ($ stream );
@@ -113,24 +118,4 @@ private function waitForStreamOnce($stream)
113
118
throw new \RuntimeException ('Cancelled while waiting for TCP/IP connection to be established ' );
114
119
});
115
120
}
116
-
117
- /** @internal */
118
- public function checkConnectedSocket ($ socket )
119
- {
120
- // The following hack looks like the only way to
121
- // detect connection refused errors with PHP's stream sockets.
122
- if (false === stream_socket_get_name ($ socket , true )) {
123
- fclose ($ socket );
124
-
125
- return Promise \reject (new \RuntimeException ('Connection refused ' ));
126
- }
127
-
128
- return Promise \resolve ($ socket );
129
- }
130
-
131
- /** @internal */
132
- public function handleConnectedSocket ($ socket )
133
- {
134
- return new Connection ($ socket , $ this ->loop );
135
- }
136
121
}
0 commit comments