Swow.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. declare(strict_types=1);
  3. namespace Workerman\Events;
  4. use Swow\Coroutine;
  5. use Swow\Signal;
  6. use Swow\SignalException;
  7. use function Swow\Sync\waitAll;
  8. final class Swow implements EventInterface
  9. {
  10. /**
  11. * All listeners for read timer.
  12. *
  13. * @var array<int, int>
  14. */
  15. private array $eventTimer = [];
  16. /**
  17. * All listeners for read event.
  18. *
  19. * @var array<int, Coroutine>
  20. */
  21. private array $readEvents = [];
  22. /**
  23. * All listeners for write event.
  24. *
  25. * @var array<int, Coroutine>
  26. */
  27. private array $writeEvents = [];
  28. /**
  29. * All listeners for signal.
  30. *
  31. * @var array<int, Coroutine>
  32. */
  33. private array $signalListener = [];
  34. /**
  35. * @var ?callable
  36. */
  37. private $errorHandler = null;
  38. /**
  39. * Get timer count.
  40. *
  41. * @return integer
  42. */
  43. public function getTimerCount(): int
  44. {
  45. return count($this->eventTimer);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function delay(float $delay, callable $func, array $args = []): int
  51. {
  52. $t = (int)($delay * 1000);
  53. $t = max($t, 1);
  54. $coroutine = Coroutine::run(function () use ($t, $func, $args): void {
  55. msleep($t);
  56. unset($this->eventTimer[Coroutine::getCurrent()->getId()]);
  57. $this->safeCall($func, $args);
  58. });
  59. $timerId = $coroutine->getId();
  60. $this->eventTimer[$timerId] = $timerId;
  61. return $timerId;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function repeat(float $interval, callable $func, array $args = []): int
  67. {
  68. $t = (int)($interval * 1000);
  69. $t = max($t, 1);
  70. $coroutine = Coroutine::run(function () use ($t, $func, $args): void {
  71. // @phpstan-ignore-next-line While loop condition is always true.
  72. while (true) {
  73. msleep($t);
  74. $this->safeCall($func, $args);
  75. }
  76. });
  77. $timerId = $coroutine->getId();
  78. $this->eventTimer[$timerId] = $timerId;
  79. return $timerId;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function offDelay(int $timerId): bool
  85. {
  86. if (isset($this->eventTimer[$timerId])) {
  87. try {
  88. (Coroutine::getAll()[$timerId])->kill();
  89. return true;
  90. } finally {
  91. unset($this->eventTimer[$timerId]);
  92. }
  93. }
  94. return false;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function offRepeat(int $timerId): bool
  100. {
  101. return $this->offDelay($timerId);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function deleteAllTimer(): void
  107. {
  108. foreach ($this->eventTimer as $timerId) {
  109. $this->offDelay($timerId);
  110. }
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function onReadable($stream, callable $func): void
  116. {
  117. $fd = (int)$stream;
  118. if (isset($this->readEvents[$fd])) {
  119. $this->offReadable($stream);
  120. }
  121. Coroutine::run(function () use ($stream, $func, $fd): void {
  122. try {
  123. $this->readEvents[$fd] = Coroutine::getCurrent();
  124. while (true) {
  125. if (!is_resource($stream)) {
  126. $this->offReadable($stream);
  127. break;
  128. }
  129. $rEvent = stream_poll_one($stream, STREAM_POLLIN | STREAM_POLLHUP);
  130. if (!isset($this->readEvents[$fd]) || $this->readEvents[$fd] !== Coroutine::getCurrent()) {
  131. break;
  132. }
  133. if ($rEvent !== STREAM_POLLNONE) {
  134. $this->safeCall($func, [$stream]);
  135. }
  136. if ($rEvent !== STREAM_POLLIN) {
  137. $this->offReadable($stream);
  138. break;
  139. }
  140. }
  141. } catch (\RuntimeException) {
  142. $this->offReadable($stream);
  143. }
  144. });
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function offReadable($stream): bool
  150. {
  151. // 在当前协程执行 $coroutine->kill() 会导致不可预知问题,所以没有使用$coroutine->kill()
  152. $fd = (int)$stream;
  153. if (isset($this->readEvents[$fd])) {
  154. unset($this->readEvents[$fd]);
  155. return true;
  156. }
  157. return false;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function onWritable($stream, callable $func): void
  163. {
  164. $fd = (int)$stream;
  165. if (isset($this->writeEvents[$fd])) {
  166. $this->offWritable($stream);
  167. }
  168. Coroutine::run(function () use ($stream, $func, $fd): void {
  169. try {
  170. $this->writeEvents[$fd] = Coroutine::getCurrent();
  171. while (true) {
  172. $rEvent = stream_poll_one($stream, STREAM_POLLOUT | STREAM_POLLHUP);
  173. if (!isset($this->writeEvents[$fd]) || $this->writeEvents[$fd] !== Coroutine::getCurrent()) {
  174. break;
  175. }
  176. if ($rEvent !== STREAM_POLLNONE) {
  177. $this->safeCall($func, [$stream]);
  178. }
  179. if ($rEvent !== STREAM_POLLOUT) {
  180. $this->offWritable($stream);
  181. break;
  182. }
  183. }
  184. } catch (\RuntimeException) {
  185. $this->offWritable($stream);
  186. }
  187. });
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function offWritable($stream): bool
  193. {
  194. $fd = (int)$stream;
  195. if (isset($this->writeEvents[$fd])) {
  196. unset($this->writeEvents[$fd]);
  197. return true;
  198. }
  199. return false;
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function onSignal(int $signal, callable $func): void
  205. {
  206. Coroutine::run(function () use ($signal, $func): void {
  207. $this->signalListener[$signal] = Coroutine::getCurrent();
  208. while (1) {
  209. try {
  210. Signal::wait($signal);
  211. if (!isset($this->signalListener[$signal]) ||
  212. $this->signalListener[$signal] !== Coroutine::getCurrent()) {
  213. break;
  214. }
  215. $this->safeCall($func, [$signal]);
  216. } catch (SignalException) {
  217. // do nothing
  218. }
  219. }
  220. });
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function offSignal(int $signal): bool
  226. {
  227. if (!isset($this->signalListener[$signal])) {
  228. return false;
  229. }
  230. unset($this->signalListener[$signal]);
  231. return true;
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. public function run(): void
  237. {
  238. waitAll();
  239. }
  240. /**
  241. * Destroy loop.
  242. *
  243. * @return void
  244. */
  245. public function stop(): void
  246. {
  247. Coroutine::killAll();
  248. }
  249. /**
  250. * {@inheritdoc}
  251. */
  252. public function setErrorHandler(callable $errorHandler): void
  253. {
  254. $this->errorHandler = $errorHandler;
  255. }
  256. /**
  257. * @param callable $func
  258. * @param array $args
  259. * @return void
  260. */
  261. private function safeCall(callable $func, array $args = []): void
  262. {
  263. try {
  264. $func(...$args);
  265. } catch (\Throwable $e) {
  266. if ($this->errorHandler === null) {
  267. echo $e;
  268. } else {
  269. ($this->errorHandler)($e);
  270. }
  271. }
  272. }
  273. }