React.php 7.2 KB

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