Ev.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Workerman\Worker;
  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 $readEvents = [];
  27. /**
  28. * All listeners for write event.
  29. *
  30. * @var array
  31. */
  32. protected $writeEvents = [];
  33. /**
  34. * Event listeners of signal.
  35. *
  36. * @var array
  37. */
  38. protected $eventSignal = [];
  39. /**
  40. * All timer event listeners.
  41. *
  42. * @var array
  43. */
  44. protected $eventTimer = [];
  45. /**
  46. * Timer id.
  47. *
  48. * @var int
  49. */
  50. protected static $timerId = 1;
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function delay(float $delay, $func, $args)
  55. {
  56. $timerId = self::$timerId;
  57. $event = new \EvTimer($delay, 0, function () use ($func, $args, $timerId) {
  58. unset($this->eventTimer[$timerId]);
  59. $func(...(array)$args);
  60. });
  61. $this->eventTimer[self::$timerId] = $event;
  62. return self::$timerId++;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function offDelay($timerId)
  68. {
  69. if (isset($this->eventTimer[$timerId])) {
  70. $this->eventTimer[$timerId]->stop();
  71. unset($this->eventTimer[$timerId]);
  72. return true;
  73. }
  74. return false;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function offRepeat($timerId)
  80. {
  81. return $this->offDelay($timerId);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function repeat(float $interval, $func, $args)
  87. {
  88. $event = new \EvTimer($interval, $interval, function () use ($func, $args) {
  89. $func(...(array)$args);
  90. });
  91. $this->eventTimer[self::$timerId] = $event;
  92. return self::$timerId++;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function onReadable($stream, $func)
  98. {
  99. $fdKey = (int)$stream;
  100. $event = new \EvIo($stream, \Ev::READ, function () use ($func, $stream) {
  101. $func($stream);
  102. });
  103. $this->readEvents[$fdKey] = $event;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function offReadable($stream)
  109. {
  110. $fdKey = (int)$stream;
  111. if (isset($this->readEvents[$fdKey])) {
  112. $this->readEvents[$fdKey]->stop();
  113. unset($this->readEvents[$fdKey]);
  114. }
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function onWritable($stream, $func)
  120. {
  121. $fdKey = (int)$stream;
  122. $event = new \EvIo($stream, \Ev::WRITE, function () use ($func, $stream) {
  123. $func($stream);
  124. });
  125. $this->readEvents[$fdKey] = $event;
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function offWritable($stream)
  131. {
  132. $fdKey = (int)$stream;
  133. if (isset($this->writeEvents[$fdKey])) {
  134. $this->writeEvents[$fdKey]->stop();
  135. unset($this->writeEvents[$fdKey]);
  136. }
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function onSignal($signal, $func)
  142. {
  143. $event = new \EvSignal($signal, function () use ($func, $signal) {
  144. $func($signal);
  145. });
  146. $this->eventSignal[$signal] = $event;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function offSignal($signal)
  152. {
  153. if (isset($this->eventSignal[$signal])) {
  154. $this->eventSignal[$signal]->stop();
  155. unset($this->eventSignal[$signal]);
  156. }
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function deleteAllTimer()
  162. {
  163. foreach ($this->eventTimer as $event) {
  164. $event->stop();
  165. }
  166. $this->eventTimer = [];
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function run()
  172. {
  173. \Ev::run();
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function stop()
  179. {
  180. \Ev::stop();
  181. }
  182. /**
  183. * {@inheritdoc}
  184. */
  185. public function getTimerCount()
  186. {
  187. return \count($this->eventTimer);
  188. }
  189. }