@@ -66,14 +66,10 @@ class Client
6666 */
6767 protected $ recvCid = 0 ;
6868 /**
69- * The sign of if this Client is closing
70- * @var int
71- */
72- protected $ closing = 0 ;
73- /**
74- * @var Channel
69+ * The sign of if Client->close() was called
70+ * @var bool
7571 */
76- protected $ closeWaiter ;
72+ protected $ closed = false ;
7773
7874 protected $ host ;
7975 protected $ port ;
@@ -169,36 +165,32 @@ protected function start()
169165 } // else: receiver not found, discard it
170166
171167 // push finished, check if close wait and no coroutine is waiting, if Y, stop recv loop
172- if (!$ this ->closing || !empty ($ this ->recvChannelMap )) {
168+ if (!$ this ->closed || !empty ($ this ->recvChannelMap )) {
173169 continue ;
174170 }
175171 }
176172
177173 // if you want to close it or retry connect failed, stop recv loop
178- if ($ this ->closing ) {
174+ if ($ this ->closed ) {
179175 $ need_break = true ;
180176 } else {
181177 $ need_break = !$ this ->client ->connect ();
182178 }
183179
184- // ↑↓ We must `retry-connect` before we push `false` response
185- // ↑↓ Then the pop channel coroutine can knows that if this client is available
186-
187- // clear all, we will auto reconnect, but it need user retry again by himself
188- if (!empty ($ this ->recvChannelMap )) {
189- foreach ($ this ->recvChannelMap as $ the_channel ) {
190- $ the_channel ->close ();
191- }
192- $ this ->recvChannelMap = [];
193- }
194-
195180 if ($ need_break ) {
196181 break ;
197182 }
198183 }
199184
200185 $ this ->recvCid = 0 ;
201- $ this ->closed ();
186+ $ this ->client ->close ();
187+ $ this ->closeWriteSide ();
188+ while (!empty ($ this ->recvChannelMap )) {
189+ foreach ($ this ->recvChannelMap as $ index => $ the_channel ) {
190+ unset($ this ->recvChannelMap [$ index ]);
191+ $ the_channel ->close ();
192+ }
193+ }
202194 });
203195
204196 // send wait
@@ -220,7 +212,7 @@ protected function start()
220212 $ this ->sendRetChannel ->close ();
221213
222214 $ this ->sendCid = 0 ;
223- $ this ->closed ();
215+ $ this ->closeReadSide ();
224216 });
225217 }
226218
@@ -340,46 +332,25 @@ public function recv(int $streamId, float $timeout = null)
340332 return false ;
341333 }
342334
343- public function close (): void
335+ protected function closeReadSide (): void
344336 {
345- if ($ this ->closing ) {
346- return ;
337+ if ($ this ->recvCid > 0 ) {
338+ $ this -> client -> close () ;
347339 }
348- $ this ->closing = 2 ;
349- // close write side first
350- $ this ->sendChannel ->close ();
351- $ this ->client ->close ();
352340 }
353341
354- protected function closed (): void
342+ protected function closeWriteSide (): void
355343 {
356- if ($ this ->closing > 0 ) {
357- $ this ->closing --;
358- }
359- // close success and notify the close waiter
360- if ($ this ->closeWaiter ) {
361- $ closeWaiter = $ this ->closeWaiter ;
362- $ this ->closeWaiter = null ;
363- $ closeWaiter ->push (true );
344+ if ($ this ->sendCid > 0 ) {
345+ $ this ->sendChannel ->close ();
364346 }
365347 }
366348
367- public function closeWait (): void
349+ public function close (): void
368350 {
369- if ($ this ->closing ) {
370- return ;
371- }
372- $ this ->closing = 2 ;
373- $ this ->closeWaiter = $ closeWaiter = new Channel ;
374- $ n = 0 ;
375- if ($ this ->recvCid > 0 ) {
376- $ n ++;
377- }
378- if ($ this ->sendCid > 0 ) {
379- $ n ++;
380- }
381- while ($ n --) {
382- $ closeWaiter ->pop ();
383- }
351+ $ this ->closed = true ;
352+ // close write side first
353+ $ this ->closeWriteSide ();
354+ $ this ->closeReadSide ();
384355 }
385356}
0 commit comments