1212
1313namespace PHPinnacle \Ridge ;
1414
15+ use Amp \Loop ;
1516use function Amp \asyncCall ;
1617use function Amp \call ;
1718use Amp \Deferred ;
@@ -24,6 +25,8 @@ final class Client
2425 private const STATE_CONNECTED = 2 ;
2526 private const STATE_DISCONNECTING = 3 ;
2627
28+ private const CONNECTION_MONITOR_INTERVAL = 5000 ;
29+
2730 /**
2831 * @var Config
2932 */
@@ -54,6 +57,11 @@ final class Client
5457 */
5558 private $ properties ;
5659
60+ /**
61+ * @var string|null
62+ */
63+ private $ connectionMonitorWatcherId ;
64+
5765 public function __construct (Config $ config )
5866 {
5967 $ this ->config = $ config ;
@@ -91,7 +99,7 @@ function () {
9199
92100 $ this ->state = self ::STATE_CONNECTING ;
93101
94- $ this ->connection = new Connection ($ this ->config ->uri (), fn () => $ this -> state = self :: STATE_NOT_CONNECTED );
102+ $ this ->connection = new Connection ($ this ->config ->uri ());
95103
96104 yield $ this ->connection ->open (
97105 $ this ->config ->timeout ,
@@ -128,10 +136,22 @@ function () {
128136
129137 $ this ->connection ->write ($ buffer );
130138 $ this ->connection ->close ();
139+
140+ $ this ->disableConnectionMonitor ();
131141 }
132142 );
133143
134144 $ 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+ );
135155 }
136156 );
137157 }
@@ -143,6 +163,8 @@ function () {
143163 */
144164 public function disconnect (int $ code = 0 , string $ reason = '' ): Promise
145165 {
166+ $ this ->disableConnectionMonitor ();
167+
146168 return call (
147169 function () use ($ code , $ reason ) {
148170 if (\in_array ($ this ->state , [self ::STATE_NOT_CONNECTED , self ::STATE_DISCONNECTING ])) {
@@ -153,6 +175,12 @@ function () use ($code, $reason) {
153175 throw Exception \ClientException::notConnected ();
154176 }
155177
178+ if ($ this ->connectionMonitorWatcherId !== null ){
179+ Loop::cancel ($ this ->connectionMonitorWatcherId );
180+
181+ $ this ->connectionMonitorWatcherId = null ;
182+ }
183+
156184 $ this ->state = self ::STATE_DISCONNECTING ;
157185
158186 if ($ code === 0 ) {
@@ -231,7 +259,7 @@ function () {
231259
232260 public function isConnected (): bool
233261 {
234- return $ this ->state === self ::STATE_CONNECTED ;
262+ return $ this ->state === self ::STATE_CONNECTED && $ this -> connection -> connected () ;
235263 }
236264
237265 /**
@@ -422,4 +450,13 @@ static function (Protocol\AbstractFrame $frame) use ($deferred) {
422450
423451 return $ deferred ->promise ();
424452 }
453+
454+ private function disableConnectionMonitor (): void {
455+ if ($ this ->connectionMonitorWatcherId !== null ) {
456+
457+ Loop::cancel ($ this ->connectionMonitorWatcherId );
458+
459+ $ this ->connectionMonitorWatcherId = null ;
460+ }
461+ }
425462}
0 commit comments