Channel.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\ChannelInterface;
  13. use Hyperf\Engine\Exception\RuntimeException;
  14. if (PHP_VERSION_ID > 80000 && SWOOLE_VERSION_ID >= 50000) {
  15. class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface
  16. {
  17. protected bool $closed = false;
  18. public function push(mixed $data, float $timeout = -1): bool
  19. {
  20. return parent::push($data, $timeout);
  21. }
  22. public function pop(float $timeout = -1): mixed
  23. {
  24. return parent::pop($timeout);
  25. }
  26. public function getCapacity(): int
  27. {
  28. return $this->capacity;
  29. }
  30. public function getLength(): int
  31. {
  32. return $this->length();
  33. }
  34. public function isAvailable(): bool
  35. {
  36. return ! $this->isClosing();
  37. }
  38. public function close(): bool
  39. {
  40. $this->closed = true;
  41. return parent::close();
  42. }
  43. public function hasProducers(): bool
  44. {
  45. throw new RuntimeException('Not supported.');
  46. }
  47. public function hasConsumers(): bool
  48. {
  49. throw new RuntimeException('Not supported.');
  50. }
  51. public function isReadable(): bool
  52. {
  53. throw new RuntimeException('Not supported.');
  54. }
  55. public function isWritable(): bool
  56. {
  57. throw new RuntimeException('Not supported.');
  58. }
  59. public function isClosing(): bool
  60. {
  61. return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED;
  62. }
  63. public function isTimeout(): bool
  64. {
  65. return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT;
  66. }
  67. }
  68. } else {
  69. class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface
  70. {
  71. /**
  72. * @var bool
  73. */
  74. protected $closed = false;
  75. public function getCapacity(): int
  76. {
  77. return $this->capacity;
  78. }
  79. public function getLength(): int
  80. {
  81. return $this->length();
  82. }
  83. public function isAvailable(): bool
  84. {
  85. return ! $this->isClosing();
  86. }
  87. public function close(): bool
  88. {
  89. $this->closed = true;
  90. return parent::close();
  91. }
  92. public function hasProducers(): bool
  93. {
  94. throw new RuntimeException('Not supported.');
  95. }
  96. public function hasConsumers(): bool
  97. {
  98. throw new RuntimeException('Not supported.');
  99. }
  100. public function isReadable(): bool
  101. {
  102. throw new RuntimeException('Not supported.');
  103. }
  104. public function isWritable(): bool
  105. {
  106. throw new RuntimeException('Not supported.');
  107. }
  108. public function isClosing(): bool
  109. {
  110. return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED;
  111. }
  112. public function isTimeout(): bool
  113. {
  114. return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT;
  115. }
  116. }
  117. }