Skip to content

Commit

Permalink
Fixed bug that process broken when recv failed. (#79)
Browse files Browse the repository at this point in the history
* Fixed bug that the `errCode` and `errMsg` is invalid cased by closing socket.

* Fixed bug that process broken when recv failed.
  • Loading branch information
limingxinleo authored Mar 14, 2023
1 parent cf93625 commit 3ca20b3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Client/SwooleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/Socket/SwooleSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3ca20b3

Please sign in to comment.