Swoole.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Swoole\Event;
  16. use Swoole\Timer;
  17. use Swoole\Process;
  18. class Swoole implements EventInterface
  19. {
  20. /**
  21. * All listeners for read timer
  22. * @var array
  23. */
  24. protected $eventTimer = [];
  25. /**
  26. * All listeners for read event.
  27. * @var array
  28. */
  29. protected $readEvents = [];
  30. /**
  31. * All listeners for write event.
  32. * @var array
  33. */
  34. protected $writeEvents = [];
  35. /**
  36. * @var int
  37. */
  38. protected $mapId = 0;
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function delay(float $delay, $func, $args)
  43. {
  44. $t = (int)($delay * 1000);
  45. $t = $t < 1 ? 1 : $t;
  46. $timerId = Timer::after($t, function () use ($func, $args, &$timerId) {
  47. unset($this->eventTimer[$timerId]);
  48. try {
  49. $func(...(array)$args);
  50. } catch (\Throwable $e) {
  51. Worker::stopAll(250, $e);
  52. }
  53. });
  54. $this->eventTimer[$timerId] = $timerId;
  55. return $timerId;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function deleteTimer($timerId)
  61. {
  62. if (isset($this->eventTimer[$timerId])) {
  63. $res = Timer::clear($timerId);
  64. unset($this->eventTimer[$timerId]);
  65. return $res;
  66. }
  67. return false;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function repeat(float $interval, $func, $args)
  73. {
  74. if ($this->mapId > \PHP_INT_MAX) {
  75. $this->mapId = 0;
  76. }
  77. $t = (int)($interval * 1000);
  78. $t = $t < 1 ? 1 : $t;
  79. $timerId = Timer::tick($t, function () use ($func, $args) {
  80. try {
  81. $func(...(array)$args);
  82. } catch (\Throwable $e) {
  83. Worker::stopAll(250, $e);
  84. }
  85. });
  86. $this->eventTimer[$timerId] = $timerId;
  87. return $timerId;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function onReadable($stream, $func)
  93. {
  94. $fd = (int)$stream;
  95. if (!isset($this->readEvents[$fd]) && !isset($this->writeEvents[$fd])) {
  96. Event::add($stream, $func, null, \SWOOLE_EVENT_READ);
  97. } else {
  98. if (isset($this->writeEvents[$fd])) {
  99. Event::set($stream, $func, null, \SWOOLE_EVENT_READ | \SWOOLE_EVENT_WRITE);
  100. } else {
  101. Event::set($stream, $func, null, \SWOOLE_EVENT_READ);
  102. }
  103. }
  104. $this->readEvents[$fd] = $stream;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function offReadable($stream)
  110. {
  111. $fd = (int)$stream;
  112. if (!isset($this->readEvents[$fd])) {
  113. return;
  114. }
  115. unset($this->readEvents[$fd]);
  116. if (!isset($this->writeEvents[$fd])) {
  117. Event::del($stream);
  118. return;
  119. }
  120. Event::set($stream, null, null, \SWOOLE_EVENT_WRITE);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function onWritable($stream, $func)
  126. {
  127. $fd = (int)$stream;
  128. if (!isset($this->readEvents[$fd]) && !isset($this->writeEvents[$fd])) {
  129. Event::add($stream, null, $func, \SWOOLE_EVENT_WRITE);
  130. } else {
  131. if (isset($this->readEvents[$fd])) {
  132. Event::set($stream, null, $func, \SWOOLE_EVENT_WRITE | \SWOOLE_EVENT_READ);
  133. } else {
  134. Event::set($stream, null, $func, \SWOOLE_EVENT_WRITE);
  135. }
  136. }
  137. $this->writeEvents[$fd] = $stream;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function offWritable($stream)
  143. {
  144. $fd = (int)$stream;
  145. if (!isset($this->writeEvents[$fd])) {
  146. return;
  147. }
  148. unset($this->writeEvents[$fd]);
  149. if (!isset($this->readEvents[$fd])) {
  150. Event::del($stream);
  151. return;
  152. }
  153. Event::set($stream, null, null, \SWOOLE_EVENT_READ);
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function onSignal($signal, $func)
  159. {
  160. return Process::signal($signal, $func);
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function offSignal($signal)
  166. {
  167. return Process::signal($signal, function () {});
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function deleteAllTimer()
  173. {
  174. foreach ($this->eventTimer as $timerId) {
  175. Timer::clear($timerId);
  176. }
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function run()
  182. {
  183. Event::wait();
  184. }
  185. /**
  186. * Destroy loop.
  187. *
  188. * @return void
  189. */
  190. public function stop()
  191. {
  192. Event::exit();
  193. \posix_kill(posix_getpid(), SIGINT);
  194. }
  195. /**
  196. * Get timer count.
  197. *
  198. * @return integer
  199. */
  200. public function getTimerCount()
  201. {
  202. return \count($this->eventTimer);
  203. }
  204. }