Ev.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * @link http://www.workerman.net/
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Workerman\Events;
  14. use Closure;
  15. use EvWatcher;
  16. /**
  17. * Ev eventloop
  18. */
  19. class Ev implements EventInterface
  20. {
  21. /**
  22. * All listeners for read event.
  23. *
  24. * @var array
  25. */
  26. protected array $readEvents = [];
  27. /**
  28. * All listeners for write event.
  29. *
  30. * @var array
  31. */
  32. protected array $writeEvents = [];
  33. /**
  34. * Event listeners of signal.
  35. *
  36. * @var array
  37. */
  38. protected array $eventSignal = [];
  39. /**
  40. * All timer event listeners.
  41. *
  42. * @var array
  43. */
  44. protected array $eventTimer = [];
  45. /**
  46. * @var ?callable
  47. */
  48. protected $errorHandler = null;
  49. /**
  50. * Timer id.
  51. *
  52. * @var int
  53. */
  54. protected static int $timerId = 1;
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function delay(float $delay, callable $func, array $args = []): int
  59. {
  60. $timerId = self::$timerId;
  61. $event = new \EvTimer($delay, 0, function () use ($func, $args, $timerId) {
  62. unset($this->eventTimer[$timerId]);
  63. $func(...$args);
  64. });
  65. $this->eventTimer[self::$timerId] = $event;
  66. return self::$timerId++;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function offDelay(int $timerId): bool
  72. {
  73. if (isset($this->eventTimer[$timerId])) {
  74. $this->eventTimer[$timerId]->stop();
  75. unset($this->eventTimer[$timerId]);
  76. return true;
  77. }
  78. return false;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function offRepeat(int $timerId): bool
  84. {
  85. return $this->offDelay($timerId);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function repeat(float $interval, callable $func, array $args = []): int
  91. {
  92. $event = new \EvTimer($interval, $interval, function () use ($func, $args) {
  93. $func(...$args);
  94. });
  95. $this->eventTimer[self::$timerId] = $event;
  96. return self::$timerId++;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function onReadable($stream, callable $func)
  102. {
  103. $fdKey = (int)$stream;
  104. $event = new \EvIo($stream, \Ev::READ, function () use ($func, $stream) {
  105. $func($stream);
  106. });
  107. $this->readEvents[$fdKey] = $event;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function offReadable($stream): bool
  113. {
  114. $fdKey = (int)$stream;
  115. if (isset($this->readEvents[$fdKey])) {
  116. $this->readEvents[$fdKey]->stop();
  117. unset($this->readEvents[$fdKey]);
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function onWritable($stream, callable $func)
  126. {
  127. $fdKey = (int)$stream;
  128. $event = new \EvIo($stream, \Ev::WRITE, function () use ($func, $stream) {
  129. $func($stream);
  130. });
  131. $this->readEvents[$fdKey] = $event;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function offWritable($stream): bool
  137. {
  138. $fdKey = (int)$stream;
  139. if (isset($this->writeEvents[$fdKey])) {
  140. $this->writeEvents[$fdKey]->stop();
  141. unset($this->writeEvents[$fdKey]);
  142. return true;
  143. }
  144. return false;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function onSignal(int $signal, callable $func)
  150. {
  151. $event = new \EvSignal($signal, function () use ($func, $signal) {
  152. $func($signal);
  153. });
  154. $this->eventSignal[$signal] = $event;
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function offSignal(int $signal): bool
  160. {
  161. if (isset($this->eventSignal[$signal])) {
  162. $this->eventSignal[$signal]->stop();
  163. unset($this->eventSignal[$signal]);
  164. return true;
  165. }
  166. return false;
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function deleteAllTimer()
  172. {
  173. foreach ($this->eventTimer as $event) {
  174. $event->stop();
  175. }
  176. $this->eventTimer = [];
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function run()
  182. {
  183. \Ev::run();
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function stop()
  189. {
  190. \Ev::stop();
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function getTimerCount(): int
  196. {
  197. return \count($this->eventTimer);
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function setErrorHandler(callable $errorHandler)
  203. {
  204. $this->errorHandler = $errorHandler;
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function getErrorHandler(): ?callable
  210. {
  211. return $this->errorHandler;
  212. }
  213. }