React.php 7.5 KB

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