React.php 7.4 KB

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