12
12
13
13
namespace PHPinnacle \Ridge ;
14
14
15
+ use Amp \Loop ;
15
16
use function Amp \asyncCall ;
16
17
use function Amp \call ;
17
18
use Amp \Deferred ;
@@ -24,6 +25,8 @@ final class Client
24
25
private const STATE_CONNECTED = 2 ;
25
26
private const STATE_DISCONNECTING = 3 ;
26
27
28
+ private const CONNECTION_MONITOR_INTERVAL = 5000 ;
29
+
27
30
/**
28
31
* @var Config
29
32
*/
@@ -54,6 +57,11 @@ final class Client
54
57
*/
55
58
private $ properties ;
56
59
60
+ /**
61
+ * @var string|null
62
+ */
63
+ private $ connectionMonitorWatcherId ;
64
+
57
65
public function __construct (Config $ config )
58
66
{
59
67
$ this ->config = $ config ;
@@ -91,7 +99,7 @@ function () {
91
99
92
100
$ this ->state = self ::STATE_CONNECTING ;
93
101
94
- $ this ->connection = new Connection ($ this ->config ->uri (), fn () => $ this -> state = self :: STATE_NOT_CONNECTED );
102
+ $ this ->connection = new Connection ($ this ->config ->uri ());
95
103
96
104
yield $ this ->connection ->open (
97
105
$ this ->config ->timeout ,
@@ -128,10 +136,22 @@ function () {
128
136
129
137
$ this ->connection ->write ($ buffer );
130
138
$ this ->connection ->close ();
139
+
140
+ $ this ->disableConnectionMonitor ();
131
141
}
132
142
);
133
143
134
144
$ this ->state = self ::STATE_CONNECTED ;
145
+
146
+ $ this ->connectionMonitorWatcherId = Loop::repeat (
147
+ self ::CONNECTION_MONITOR_INTERVAL ,
148
+ function (): void
149
+ {
150
+ if ($ this ->connection ->connected () === false ) {
151
+ throw Exception \ClientException::disconnected ();
152
+ }
153
+ }
154
+ );
135
155
}
136
156
);
137
157
}
@@ -143,6 +163,8 @@ function () {
143
163
*/
144
164
public function disconnect (int $ code = 0 , string $ reason = '' ): Promise
145
165
{
166
+ $ this ->disableConnectionMonitor ();
167
+
146
168
return call (
147
169
function () use ($ code , $ reason ) {
148
170
if (\in_array ($ this ->state , [self ::STATE_NOT_CONNECTED , self ::STATE_DISCONNECTING ])) {
@@ -153,6 +175,12 @@ function () use ($code, $reason) {
153
175
throw Exception \ClientException::notConnected ();
154
176
}
155
177
178
+ if ($ this ->connectionMonitorWatcherId !== null ){
179
+ Loop::cancel ($ this ->connectionMonitorWatcherId );
180
+
181
+ $ this ->connectionMonitorWatcherId = null ;
182
+ }
183
+
156
184
$ this ->state = self ::STATE_DISCONNECTING ;
157
185
158
186
if ($ code === 0 ) {
@@ -231,7 +259,7 @@ function () {
231
259
232
260
public function isConnected (): bool
233
261
{
234
- return $ this ->state === self ::STATE_CONNECTED ;
262
+ return $ this ->state === self ::STATE_CONNECTED && $ this -> connection -> connected () ;
235
263
}
236
264
237
265
/**
@@ -422,4 +450,13 @@ static function (Protocol\AbstractFrame $frame) use ($deferred) {
422
450
423
451
return $ deferred ->promise ();
424
452
}
453
+
454
+ private function disableConnectionMonitor (): void {
455
+ if ($ this ->connectionMonitorWatcherId !== null ) {
456
+
457
+ Loop::cancel ($ this ->connectionMonitorWatcherId );
458
+
459
+ $ this ->connectionMonitorWatcherId = null ;
460
+ }
461
+ }
425
462
}
0 commit comments