From 3ca20b3e6b1e7da7919c5bbfcbed161055b97010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 14 Mar 2023 19:17:15 +0800 Subject: [PATCH] Fixed bug that process broken when recv failed. (#79) * Fixed bug that the `errCode` and `errMsg` is invalid cased by closing socket. * Fixed bug that process broken when recv failed. --- src/Client/SwooleClient.php | 5 +++-- src/Socket/SwooleSocket.php | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Client/SwooleClient.php b/src/Client/SwooleClient.php index 6b1bee4..12c9c54 100644 --- a/src/Client/SwooleClient.php +++ b/src/Client/SwooleClient.php @@ -150,12 +150,13 @@ private function startRecvCo(): void if ($e instanceof SocketException && !$this->connected) { return; } + $callback = $this->getConfig()->getExceptionCallback(); if ($callback) { $callback($e); - } else { - throw $e; } + + $this->close(); } } }); diff --git a/src/Socket/SwooleSocket.php b/src/Socket/SwooleSocket.php index b7b2f75..f4d7e48 100644 --- a/src/Socket/SwooleSocket.php +++ b/src/Socket/SwooleSocket.php @@ -125,8 +125,10 @@ public function recv(int $length, ?float $timeout = null): string while ($this->socket && !isset($this->receivedBuffer[$length - 1]) && (-1 == $timeout || $leftTime > 0)) { $buffer = $this->socket->recv($timeout); if ('' === $buffer || false === $buffer) { + $code = $this->socket->errCode; + $msg = $this->socket->errMsg; $this->close(); - throw new SocketException(sprintf('Could not recv data from stream, %s [%d]', $this->socket->errMsg, $this->socket->errCode)); + throw new SocketException(sprintf('Could not recv data from stream, %s [%d]', $msg, $code)); } $this->receivedBuffer .= $buffer; if ($timeout > 0) {