Revolt.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. declare(strict_types=1);
  15. namespace Workerman\Events;
  16. use Revolt\EventLoop;
  17. use Revolt\EventLoop\Driver;
  18. /**
  19. * Revolt eventloop
  20. */
  21. class Revolt implements EventInterface
  22. {
  23. /**
  24. * @var Driver
  25. */
  26. protected Driver $driver;
  27. /**
  28. * All listeners for read event.
  29. * @var array
  30. */
  31. protected array $readEvents = [];
  32. /**
  33. * All listeners for write event.
  34. * @var array
  35. */
  36. protected array $writeEvents = [];
  37. /**
  38. * Event listeners of signal.
  39. * @var array
  40. */
  41. protected array $eventSignal = [];
  42. /**
  43. * Event listeners of timer.
  44. * @var array
  45. */
  46. protected array $eventTimer = [];
  47. /**
  48. * Timer id.
  49. * @var int
  50. */
  51. protected int $timerId = 1;
  52. /**
  53. * Construct.
  54. */
  55. public function __construct()
  56. {
  57. $this->driver = EventLoop::getDriver();
  58. }
  59. /**
  60. * Get driver
  61. *
  62. * @return Driver
  63. */
  64. public function driver(): Driver
  65. {
  66. return $this->driver;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function run(): void
  72. {
  73. $this->driver->run();
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function stop(): void
  79. {
  80. foreach ($this->eventSignal as $cbId) {
  81. $this->driver->cancel($cbId);
  82. }
  83. $this->driver->stop();
  84. if (function_exists('pcntl_signal')) {
  85. pcntl_signal(SIGINT, SIG_IGN);
  86. }
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function delay(float $delay, callable $func, array $args = []): int
  92. {
  93. $timerId = $this->timerId++;
  94. $closure = function () use ($func, $args, $timerId) {
  95. unset($this->eventTimer[$timerId]);
  96. $func(...$args);
  97. };
  98. $cbId = $this->driver->delay($delay, $closure);
  99. $this->eventTimer[$timerId] = $cbId;
  100. return $timerId;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function repeat(float $interval, callable $func, array $args = []): int
  106. {
  107. $timerId = $this->timerId++;
  108. $closure = function () use ($func, $args) {
  109. $func(...$args);
  110. };
  111. $cbId = $this->driver->repeat($interval, $closure);
  112. $this->eventTimer[$timerId] = $cbId;
  113. return $timerId;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function onReadable($stream, callable $func): void
  119. {
  120. $fdKey = (int)$stream;
  121. if (isset($this->readEvents[$fdKey])) {
  122. $this->driver->cancel($this->readEvents[$fdKey]);
  123. unset($this->readEvents[$fdKey]);
  124. }
  125. $this->readEvents[$fdKey] = $this->driver->onReadable($stream, function () use ($stream, $func) {
  126. $func($stream);
  127. });
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function offReadable($stream): bool
  133. {
  134. $fdKey = (int)$stream;
  135. if (isset($this->readEvents[$fdKey])) {
  136. $this->driver->cancel($this->readEvents[$fdKey]);
  137. unset($this->readEvents[$fdKey]);
  138. return true;
  139. }
  140. return false;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function onWritable($stream, callable $func): void
  146. {
  147. $fdKey = (int)$stream;
  148. if (isset($this->writeEvents[$fdKey])) {
  149. $this->driver->cancel($this->writeEvents[$fdKey]);
  150. unset($this->writeEvents[$fdKey]);
  151. }
  152. $this->writeEvents[$fdKey] = $this->driver->onWritable($stream, function () use ($stream, $func) {
  153. $func($stream);
  154. });
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function offWritable($stream): bool
  160. {
  161. $fdKey = (int)$stream;
  162. if (isset($this->writeEvents[$fdKey])) {
  163. $this->driver->cancel($this->writeEvents[$fdKey]);
  164. unset($this->writeEvents[$fdKey]);
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function onSignal(int $signal, callable $func): void
  173. {
  174. $fdKey = $signal;
  175. if (isset($this->eventSignal[$fdKey])) {
  176. $this->driver->cancel($this->eventSignal[$fdKey]);
  177. unset($this->eventSignal[$fdKey]);
  178. }
  179. $this->eventSignal[$fdKey] = $this->driver->onSignal($signal, function () use ($signal, $func) {
  180. $func($signal);
  181. });
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function offSignal(int $signal): bool
  187. {
  188. $fdKey = $signal;
  189. if (isset($this->eventSignal[$fdKey])) {
  190. $this->driver->cancel($this->eventSignal[$fdKey]);
  191. unset($this->eventSignal[$fdKey]);
  192. return true;
  193. }
  194. return false;
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function offDelay(int $timerId): bool
  200. {
  201. if (isset($this->eventTimer[$timerId])) {
  202. $this->driver->cancel($this->eventTimer[$timerId]);
  203. unset($this->eventTimer[$timerId]);
  204. return true;
  205. }
  206. return false;
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function offRepeat(int $timerId): bool
  212. {
  213. return $this->offDelay($timerId);
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function deleteAllTimer(): void
  219. {
  220. foreach ($this->eventTimer as $cbId) {
  221. $this->driver->cancel($cbId);
  222. }
  223. $this->eventTimer = [];
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function getTimerCount(): int
  229. {
  230. return count($this->eventTimer);
  231. }
  232. /**
  233. * {@inheritdoc}
  234. */
  235. public function setErrorHandler(callable $errorHandler): void
  236. {
  237. $this->driver->setErrorHandler($errorHandler);
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function getErrorHandler(): ?callable
  243. {
  244. return $this->driver->getErrorHandler();
  245. }
  246. }