@@ -71,7 +71,7 @@ public function connect($uri)
71
71
// HHVM fails to parse URIs with a query but no path, so let's simplify our URI here
72
72
$ remote = 'tcp:// ' . $ parts ['host ' ] . ': ' . $ parts ['port ' ];
73
73
74
- $ socket = @stream_socket_client (
74
+ $ stream = @stream_socket_client (
75
75
$ remote ,
76
76
$ errno ,
77
77
$ errstr ,
@@ -80,39 +80,30 @@ public function connect($uri)
80
80
stream_context_create ($ context )
81
81
);
82
82
83
- if (false === $ socket ) {
83
+ if (false === $ stream ) {
84
84
return Promise \reject (new RuntimeException (
85
85
sprintf ("Connection to %s failed: %s " , $ uri , $ errstr ),
86
86
$ errno
87
87
));
88
88
}
89
89
90
- stream_set_blocking ($ socket , 0 );
91
-
92
90
// wait for connection
93
-
94
- return $ this ->waitForStreamOnce ($ socket );
95
- }
96
-
97
- private function waitForStreamOnce ($ stream )
98
- {
99
91
$ loop = $ this ->loop ;
100
-
101
- return new Promise \Promise (function ($ resolve , $ reject ) use ($ loop , $ stream ) {
102
- $ loop ->addWriteStream ($ stream , function ($ stream ) use ($ loop , $ resolve , $ reject ) {
92
+ return new Promise \Promise (function ($ resolve , $ reject ) use ($ loop , $ stream , $ uri ) {
93
+ $ loop ->addWriteStream ($ stream , function ($ stream ) use ($ loop , $ resolve , $ reject , $ uri ) {
103
94
$ loop ->removeWriteStream ($ stream );
104
95
105
96
// The following hack looks like the only way to
106
97
// detect connection refused errors with PHP's stream sockets.
107
98
if (false === stream_socket_get_name ($ stream , true )) {
108
99
fclose ($ stream );
109
100
110
- $ reject (new RuntimeException ('Connection refused ' ));
101
+ $ reject (new RuntimeException ('Connection to ' . $ uri . ' failed: Connection refused ' ));
111
102
} else {
112
103
$ resolve (new Connection ($ stream , $ loop ));
113
104
}
114
105
});
115
- }, function () use ($ loop , $ stream ) {
106
+ }, function () use ($ loop , $ stream, $ uri ) {
116
107
$ loop ->removeWriteStream ($ stream );
117
108
fclose ($ stream );
118
109
@@ -123,7 +114,7 @@ private function waitForStreamOnce($stream)
123
114
}
124
115
// @codeCoverageIgnoreEnd
125
116
126
- throw new RuntimeException ('Cancelled while waiting for TCP/IP connection to be established ' );
117
+ throw new RuntimeException ('Connection to ' . $ uri . ' cancelled during TCP/IP handshake ' );
127
118
});
128
119
}
129
120
}
0 commit comments