React.php 6.9 KB

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