Revolt.php 5.4 KB

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