SafeSocket.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Engine;
  12. use Hyperf\Engine\Contract\SocketInterface;
  13. use Hyperf\Engine\Exception\SocketClosedException;
  14. use Hyperf\Engine\Exception\SocketTimeoutException;
  15. use Psr\Log\LoggerInterface;
  16. use Swoole\Coroutine\Socket;
  17. use Throwable;
  18. class SafeSocket implements SocketInterface
  19. {
  20. protected Channel $channel;
  21. protected bool $loop = false;
  22. public function __construct(
  23. protected Socket $socket,
  24. int $capacity = 65535,
  25. protected bool $throw = true,
  26. protected ?LoggerInterface $logger = null
  27. ) {
  28. $this->channel = new Channel($capacity);
  29. }
  30. /**
  31. * @throws SocketTimeoutException when send data timeout
  32. * @throws SocketClosedException when the client is closed
  33. */
  34. public function sendAll(string $data, float $timeout = 0): false|int
  35. {
  36. $this->loop();
  37. $res = $this->channel->push([$data, $timeout], $timeout);
  38. if ($res === false) {
  39. if ($this->channel->isClosing()) {
  40. $this->throw && throw new SocketClosedException('The channel is closed.');
  41. }
  42. if ($this->channel->isTimeout()) {
  43. $this->throw && throw new SocketTimeoutException('The channel is full.');
  44. }
  45. return false;
  46. }
  47. return strlen($data);
  48. }
  49. /**
  50. * @throws SocketTimeoutException when send data timeout
  51. * @throws SocketClosedException when the client is closed
  52. */
  53. public function recvAll(int $length = 65536, float $timeout = 0): false|string
  54. {
  55. $res = $this->socket->recvAll($length, $timeout);
  56. if (! $res) {
  57. if ($this->socket->errCode === SOCKET_ETIMEDOUT) {
  58. $this->throw && throw new SocketTimeoutException('Recv timeout');
  59. }
  60. $this->throw && throw new SocketClosedException('The socket is closed.');
  61. }
  62. return $res;
  63. }
  64. /**
  65. * @throws SocketTimeoutException when send data timeout
  66. * @throws SocketClosedException when the client is closed
  67. */
  68. public function recvPacket(float $timeout = 0): false|string
  69. {
  70. $res = $this->socket->recvPacket($timeout);
  71. if (! $res) {
  72. if ($this->socket->errCode === SOCKET_ETIMEDOUT) {
  73. $this->throw && throw new SocketTimeoutException('Recv timeout');
  74. }
  75. $this->throw && throw new SocketClosedException('The socket is closed.');
  76. }
  77. return $res;
  78. }
  79. public function close(): bool
  80. {
  81. $this->channel->close();
  82. return $this->socket->close();
  83. }
  84. public function setLogger(?LoggerInterface $logger): static
  85. {
  86. $this->logger = $logger;
  87. return $this;
  88. }
  89. protected function loop(): void
  90. {
  91. if ($this->loop) {
  92. return;
  93. }
  94. $this->loop = true;
  95. Coroutine::create(function () {
  96. try {
  97. while (true) {
  98. $data = $this->channel->pop(-1);
  99. if ($this->channel->isClosing()) {
  100. return;
  101. }
  102. [$data, $timeout] = $data;
  103. $this->socket->sendAll($data, $timeout);
  104. }
  105. } catch (Throwable $exception) {
  106. $this->logger?->critical((string) $exception);
  107. }
  108. });
  109. }
  110. }