React.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 React\EventLoop\LoopInterface;
  16. use React\EventLoop\Timer\TimerInterface;
  17. /**
  18. * select eventloop
  19. */
  20. class React implements LoopInterface
  21. {
  22. /**
  23. * @var React\EventLoop\LoopInterface
  24. */
  25. protected $_loop = null;
  26. /**
  27. * React constructor.
  28. */
  29. public function __construct() {
  30. if (function_exists('event_base_new')) {
  31. $this->_loop = new \Workerman\Events\React\LibEventLoop();
  32. } elseif (class_exists('EventBase', false)) {
  33. $this->_loop = new \Workerman\Events\React\ExtEventLoop();
  34. } else {
  35. $this->_loop = new \Workerman\Events\React\StreamSelectLoop();
  36. }
  37. }
  38. /**
  39. * Add event listener to event loop.
  40. *
  41. * @param $fd
  42. * @param $flag
  43. * @param $func
  44. * @param array $args
  45. * @return bool
  46. */
  47. public function add($fd, $flag, $func, $args = array())
  48. {
  49. $args = (array)$args;
  50. switch ($flag) {
  51. case EventInterface::EV_READ:
  52. return $this->_loop->addReadStream($fd, $func);
  53. case EventInterface::EV_WRITE:
  54. return $this->_loop->addWriteStream($fd, $func);
  55. case EventInterface::EV_SIGNAL:
  56. return $this->_loop->addSignal($fd, $func);
  57. case EventInterface::EV_TIMER:
  58. return $this->_loop->addPeriodicTimer($fd, function() use ($func, $args) {
  59. call_user_func_array($func, $args);
  60. });
  61. case EventInterface::EV_TIMER_ONCE:
  62. return $this->_loop->addTimer($fd, function() use ($func, $args) {
  63. call_user_func_array($func, $args);
  64. });
  65. }
  66. return false;
  67. }
  68. /**
  69. * Remove event listener from event loop.
  70. *
  71. * @param mixed $fd
  72. * @param int $flag
  73. * @return bool
  74. */
  75. public function del($fd, $flag)
  76. {
  77. switch ($flag) {
  78. case EventInterface::EV_READ:
  79. return $this->_loop->removeReadStream($fd);
  80. case EventInterface::EV_WRITE:
  81. return $this->_loop->removeWriteStream($fd);
  82. case EventInterface::EV_SIGNAL:
  83. return $this->_loop->removeSignal($fd);
  84. case EventInterface::EV_TIMER:
  85. case EventInterface::EV_TIMER_ONCE;
  86. return $this->_loop->cancelTimer($fd);
  87. }
  88. return false;
  89. }
  90. /**
  91. * Main loop.
  92. *
  93. * @return void
  94. */
  95. public function loop()
  96. {
  97. $this->_loop->run();
  98. }
  99. /**
  100. * Register a listener to be notified when a stream is ready to read.
  101. *
  102. * @param resource $stream The PHP stream resource to check.
  103. * @param callable $listener Invoked when the stream is ready.
  104. */
  105. public function addReadStream($stream, callable $listener) {
  106. return call_user_func(array($this->_loop, 'addReadStream'), $stream, $listener);
  107. }
  108. /**
  109. * Register a listener to be notified when a stream is ready to write.
  110. *
  111. * @param resource $stream The PHP stream resource to check.
  112. * @param callable $listener Invoked when the stream is ready.
  113. */
  114. public function addWriteStream($stream, callable $listener) {
  115. return call_user_func(array($this->_loop, 'addWriteStream'), $stream, $listener);
  116. }
  117. /**
  118. * Remove the read event listener for the given stream.
  119. *
  120. * @param resource $stream The PHP stream resource.
  121. */
  122. public function removeReadStream($stream) {
  123. return call_user_func(array($this->_loop, 'removeReadStream'), $stream);
  124. }
  125. /**
  126. * Remove the write event listener for the given stream.
  127. *
  128. * @param resource $stream The PHP stream resource.
  129. */
  130. public function removeWriteStream($stream) {
  131. return call_user_func(array($this->_loop, 'removeWriteStream'), $stream);
  132. }
  133. /**
  134. * Remove all listeners for the given stream.
  135. *
  136. * @param resource $stream The PHP stream resource.
  137. */
  138. public function removeStream($stream) {
  139. return call_user_func(array($this->_loop, 'removeStream'), $stream);
  140. }
  141. /**
  142. * Enqueue a callback to be invoked once after the given interval.
  143. *
  144. * The execution order of timers scheduled to execute at the same time is
  145. * not guaranteed.
  146. *
  147. * @param int|float $interval The number of seconds to wait before execution.
  148. * @param callable $callback The callback to invoke.
  149. *
  150. * @return TimerInterface
  151. */
  152. public function addTimer($interval, callable $callback) {
  153. return call_user_func(array($this->_loop, 'addTimer'), $interval, $callback);
  154. }
  155. /**
  156. * Enqueue a callback to be invoked repeatedly after the given interval.
  157. *
  158. * The execution order of timers scheduled to execute at the same time is
  159. * not guaranteed.
  160. *
  161. * @param int|float $interval The number of seconds to wait before execution.
  162. * @param callable $callback The callback to invoke.
  163. *
  164. * @return TimerInterface
  165. */
  166. public function addPeriodicTimer($interval, callable $callback) {
  167. return call_user_func(array($this->_loop, 'addPeriodicTimer'), $interval, $callback);
  168. }
  169. /**
  170. * Cancel a pending timer.
  171. *
  172. * @param TimerInterface $timer The timer to cancel.
  173. */
  174. public function cancelTimer(TimerInterface $timer) {
  175. return call_user_func(array($this->_loop, 'cancelTimer'), $timer);
  176. }
  177. /**
  178. * Check if a given timer is active.
  179. *
  180. * @param TimerInterface $timer The timer to check.
  181. *
  182. * @return boolean True if the timer is still enqueued for execution.
  183. */
  184. public function isTimerActive(TimerInterface $timer) {
  185. return call_user_func(array($this->_loop, 'isTimerActive'), $timer);
  186. }
  187. /**
  188. * Schedule a callback to be invoked on the next tick of the event loop.
  189. *
  190. * Callbacks are guaranteed to be executed in the order they are enqueued,
  191. * before any timer or stream events.
  192. *
  193. * @param callable $listener The callback to invoke.
  194. */
  195. public function nextTick(callable $listener) {
  196. return call_user_func(array($this->_loop, 'nextTick'), $listener);
  197. }
  198. /**
  199. * Schedule a callback to be invoked on a future tick of the event loop.
  200. *
  201. * Callbacks are guaranteed to be executed in the order they are enqueued.
  202. *
  203. * @param callable $listener The callback to invoke.
  204. */
  205. public function futureTick(callable $listener) {
  206. return call_user_func(array($this->_loop, 'futureTick'), $listener);
  207. }
  208. /**
  209. * Perform a single iteration of the event loop.
  210. */
  211. public function tick() {
  212. return call_user_func(array($this->_loop, 'tick'));
  213. }
  214. /**
  215. * Run the event loop until there are no more tasks to perform.
  216. */
  217. public function run() {
  218. return call_user_func(array($this->_loop, 'run'));
  219. }
  220. /**
  221. * Instruct a running event loop to stop.
  222. */
  223. public function stop() {
  224. return call_user_func(array($this->_loop, 'stop'));
  225. }
  226. }