SafeSocket.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Swoole\Coroutine\Socket;
  16. class SafeSocket implements SocketInterface
  17. {
  18. protected Channel $channel;
  19. protected bool $loop = false;
  20. public function __construct(protected Socket $socket, int $capacity = 65535, protected bool $throw = true)
  21. {
  22. $this->channel = new Channel($capacity);
  23. }
  24. /**
  25. * @throws SocketTimeoutException when send data timeout
  26. * @throws SocketClosedException when the client is closed
  27. */
  28. public function sendAll(string $data, float $timeout = 0): int|false
  29. {
  30. $this->loop();
  31. $res = $this->channel->push([$data, $timeout], $timeout);
  32. if ($res === false) {
  33. if ($this->channel->isClosing()) {
  34. $this->throw && throw new SocketClosedException('The channel is closed.');
  35. }
  36. if ($this->channel->isTimeout()) {
  37. $this->throw && throw new SocketTimeoutException('The channel is full.');
  38. }
  39. }
  40. return strlen($data);
  41. }
  42. /**
  43. * @throws SocketTimeoutException when send data timeout
  44. * @throws SocketClosedException when the client is closed
  45. */
  46. public function recvAll(int $length = 65536, float $timeout = 0): string|false
  47. {
  48. $res = $this->socket->recvAll($length, $timeout);
  49. if (! $res) {
  50. if ($this->socket->errCode === SOCKET_ETIMEDOUT) {
  51. $this->throw && throw new SocketTimeoutException('Recv timeout');
  52. }
  53. $this->throw && throw new SocketClosedException('The socket is closed.');
  54. }
  55. return $res;
  56. }
  57. /**
  58. * @throws SocketTimeoutException when send data timeout
  59. * @throws SocketClosedException when the client is closed
  60. */
  61. public function recvPacket(float $timeout = 0): string|false
  62. {
  63. $res = $this->socket->recvPacket($timeout);
  64. if (! $res) {
  65. if ($this->socket->errCode === SOCKET_ETIMEDOUT) {
  66. $this->throw && throw new SocketTimeoutException('Recv timeout');
  67. }
  68. $this->throw && throw new SocketClosedException('The socket is closed.');
  69. }
  70. return $res;
  71. }
  72. public function close(): bool
  73. {
  74. $this->channel->close();
  75. return $this->socket->close();
  76. }
  77. protected function loop(): void
  78. {
  79. if ($this->loop) {
  80. return;
  81. }
  82. $this->loop = true;
  83. go(function () {
  84. while (true) {
  85. $data = $this->channel->pop(-1);
  86. if ($this->channel->isClosing()) {
  87. return;
  88. }
  89. [$data, $timeout] = $data;
  90. $this->socket->sendAll($data, $timeout);
  91. }
  92. });
  93. }
  94. }