13
13
namespace PHPinnacle \Ridge ;
14
14
15
15
use Amp \Loop ;
16
+ use PHPinnacle \Ridge \Exception \ConnectionException ;
16
17
use function Amp \asyncCall ;
17
18
use function Amp \call ;
18
19
use Amp \Deferred ;
@@ -148,6 +149,8 @@ function () {
148
149
function (): void
149
150
{
150
151
if ($ this ->connection ->connected () === false ) {
152
+ $ this ->state = self ::STATE_NOT_CONNECTED ;
153
+
151
154
throw Exception \ClientException::disconnected ();
152
155
}
153
156
}
@@ -167,37 +170,41 @@ public function disconnect(int $code = 0, string $reason = ''): Promise
167
170
168
171
return call (
169
172
function () use ($ code , $ reason ) {
170
- if (\in_array ($ this ->state , [self ::STATE_NOT_CONNECTED , self ::STATE_DISCONNECTING ])) {
171
- return ;
172
- }
173
-
174
- if ($ this ->state !== self ::STATE_CONNECTED ) {
175
- throw Exception \ClientException::notConnected ();
176
- }
173
+ try {
174
+ if (\in_array ($ this ->state , [self ::STATE_NOT_CONNECTED , self ::STATE_DISCONNECTING ])) {
175
+ return ;
176
+ }
177
177
178
- if ($ this ->connectionMonitorWatcherId !== null ){
179
- Loop::cancel ($ this ->connectionMonitorWatcherId );
178
+ if ($ this ->state !== self ::STATE_CONNECTED ) {
179
+ throw Exception \ClientException::notConnected ();
180
+ }
180
181
181
- $ this ->connectionMonitorWatcherId = null ;
182
- }
182
+ if ( $ this ->connectionMonitorWatcherId !== null ){
183
+ Loop:: cancel ( $ this -> connectionMonitorWatcherId );
183
184
184
- $ this ->state = self ::STATE_DISCONNECTING ;
185
+ $ this ->connectionMonitorWatcherId = null ;
186
+ }
185
187
186
- if ($ code === 0 ) {
187
- $ promises = [];
188
+ $ this ->state = self ::STATE_DISCONNECTING ;
188
189
189
- foreach ($ this ->channels as $ channel ) {
190
- $ promises [] = $ channel ->close ($ code , $ reason );
191
- }
190
+ if ($ code === 0 ) {
191
+ $ promises = [];
192
192
193
- yield $ promises ;
194
- }
193
+ foreach ($ this ->channels as $ channel ) {
194
+ $ promises [] = $ channel ->close ($ code , $ reason );
195
+ }
195
196
196
- yield $ this ->connectionClose ($ code , $ reason );
197
+ yield $ promises ;
198
+ }
197
199
198
- $ this ->connection -> close ( );
200
+ yield $ this ->connectionClose ( $ code , $ reason );
199
201
200
- $ this ->state = self ::STATE_NOT_CONNECTED ;
202
+ $ this ->connection ->close ();
203
+ }
204
+ finally
205
+ {
206
+ $ this ->state = self ::STATE_NOT_CONNECTED ;
207
+ }
201
208
}
202
209
);
203
210
}
@@ -250,7 +257,13 @@ function () {
250
257
});
251
258
252
259
return $ channel ;
253
- } catch (\Throwable $ error ) {
260
+ }
261
+ catch (ConnectionException $ exception ) {
262
+ $ this ->state = self ::STATE_NOT_CONNECTED ;
263
+
264
+ throw $ exception ;
265
+ }
266
+ catch (\Throwable $ error ) {
254
267
throw Exception \ClientException::unexpectedResponse ($ error );
255
268
}
256
269
}
0 commit comments