React.php 8.0 KB

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