Skip to content

Commit

Permalink
Update SafeSocket.php
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Apr 7, 2023
1 parent 06b0ce3 commit da38c87
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/SafeSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SafeSocket implements SocketInterface

protected bool $loop = false;

public function __construct(protected Socket $socket, int $capacity = 65535)
public function __construct(protected Socket $socket, int $capacity = 65535, protected bool $throw = true)
{
$this->channel = new Channel($capacity);
}
Expand All @@ -38,10 +38,10 @@ public function sendAll(string $data, float $timeout = 0): int|false
$res = $this->channel->push([$data, $timeout], $timeout);
if ($res === false) {
if ($this->channel->isClosing()) {
throw new SocketClosedException('The channel is closed.');
$this->throw && throw new SocketClosedException('The channel is closed.');
}
if ($this->channel->isTimeout()) {
throw new SocketTimeoutException('The channel is full.');
$this->throw && throw new SocketTimeoutException('The channel is full.');
}
}
return strlen($data);
Expand All @@ -56,11 +56,10 @@ public function recvAll(int $length = 65536, float $timeout = 0): string|false
$res = $this->socket->recvAll($length, $timeout);
if (! $res) {
if ($this->socket->errCode === SOCKET_ETIMEDOUT) {
throw new SocketTimeoutException('Recv timeout');
$this->throw && throw new SocketTimeoutException('Recv timeout');
}

$this->close();
throw new SocketClosedException('The socket is closed.');
$this->throw && throw new SocketClosedException('The socket is closed.');
}

return $res;
Expand All @@ -75,11 +74,10 @@ public function recvPacket(float $timeout = 0): string|false
$res = $this->socket->recvPacket($timeout);
if (! $res) {
if ($this->socket->errCode === SOCKET_ETIMEDOUT) {
throw new SocketTimeoutException('Recv timeout');
$this->throw && throw new SocketTimeoutException('Recv timeout');
}

$this->close();
throw new SocketClosedException('The socket is closed.');
$this->throw && throw new SocketClosedException('The socket is closed.');
}

return $res;
Expand Down

0 comments on commit da38c87

Please sign in to comment.