Base.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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\React;
  15. use Workerman\Events\EventInterface;
  16. use React\EventLoop\TimerInterface;
  17. /**
  18. * Class StreamSelectLoop
  19. * @package Workerman\Events\React
  20. */
  21. class Base implements \React\EventLoop\LoopInterface
  22. {
  23. /**
  24. * @var array
  25. */
  26. protected $_timerIdMap = array();
  27. /**
  28. * @var int
  29. */
  30. protected $_timerIdIndex = 0;
  31. /**
  32. * @var array
  33. */
  34. protected $_signalHandlerMap = array();
  35. /**
  36. * @var \React\EventLoop\LoopInterface
  37. */
  38. protected $_eventLoop = null;
  39. /**
  40. * Base constructor.
  41. */
  42. public function __construct()
  43. {
  44. $this->_eventLoop = new \React\EventLoop\StreamSelectLoop();
  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->addReadStream($fd, $func);
  61. case EventInterface::EV_WRITE:
  62. return $this->addWriteStream($fd, $func);
  63. case EventInterface::EV_SIGNAL:
  64. if (isset($this->_signalHandlerMap[$fd])) {
  65. $this->removeSignal($fd, $this->_signalHandlerMap[$fd]);
  66. }
  67. $this->_signalHandlerMap[$fd] = $func;
  68. return $this->addSignal($fd, $func);
  69. case EventInterface::EV_TIMER:
  70. $timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
  71. call_user_func_array($func, $args);
  72. });
  73. $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
  74. return $this->_timerIdIndex;
  75. case EventInterface::EV_TIMER_ONCE:
  76. $index = ++$this->_timerIdIndex;
  77. $timer_obj = $this->addTimer($fd, function() use ($func, $args, $index) {
  78. $this->del($index,EventInterface::EV_TIMER_ONCE);
  79. call_user_func_array($func, $args);
  80. });
  81. $this->_timerIdMap[$index] = $timer_obj;
  82. return $this->_timerIdIndex;
  83. }
  84. return false;
  85. }
  86. /**
  87. * Remove event listener from event loop.
  88. *
  89. * @param mixed $fd
  90. * @param int $flag
  91. * @return bool
  92. */
  93. public function del($fd, $flag)
  94. {
  95. switch ($flag) {
  96. case EventInterface::EV_READ:
  97. return $this->removeReadStream($fd);
  98. case EventInterface::EV_WRITE:
  99. return $this->removeWriteStream($fd);
  100. case EventInterface::EV_SIGNAL:
  101. if (!isset($this->_eventLoop[$fd])) {
  102. return false;
  103. }
  104. $func = $this->_eventLoop[$fd];
  105. unset($this->_eventLoop[$fd]);
  106. return $this->removeSignal($fd, $func);
  107. case EventInterface::EV_TIMER:
  108. case EventInterface::EV_TIMER_ONCE:
  109. if (isset($this->_timerIdMap[$fd])){
  110. $timer_obj = $this->_timerIdMap[$fd];
  111. unset($this->_timerIdMap[$fd]);
  112. $this->cancelTimer($timer_obj);
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. /**
  119. * Main loop.
  120. *
  121. * @return void
  122. */
  123. public function loop()
  124. {
  125. $this->run();
  126. }
  127. /**
  128. * Destroy loop.
  129. *
  130. * @return void
  131. */
  132. public function destroy()
  133. {
  134. }
  135. /**
  136. * Get timer count.
  137. *
  138. * @return integer
  139. */
  140. public function getTimerCount()
  141. {
  142. return count($this->_timerIdMap);
  143. }
  144. /**
  145. * @param resource $stream
  146. * @param callable $listener
  147. */
  148. public function addReadStream($stream, $listener)
  149. {
  150. return $this->_eventLoop->addReadStream($stream, $listener);
  151. }
  152. /**
  153. * @param resource $stream
  154. * @param callable $listener
  155. */
  156. public function addWriteStream($stream, $listener)
  157. {
  158. return $this->_eventLoop->addWriteStream($stream, $listener);
  159. }
  160. /**
  161. * @param resource $stream
  162. */
  163. public function removeReadStream($stream)
  164. {
  165. return $this->_eventLoop->removeReadStream($stream);
  166. }
  167. /**
  168. * @param resource $stream
  169. */
  170. public function removeWriteStream($stream)
  171. {
  172. return $this->_eventLoop->removeWriteStream($stream);
  173. }
  174. /**
  175. * @param float|int $interval
  176. * @param callable $callback
  177. * @return \React\EventLoop\Timer\Timer|TimerInterface
  178. */
  179. public function addTimer($interval, $callback)
  180. {
  181. return $this->_eventLoop->addTimer($interval, $callback);
  182. }
  183. /**
  184. * @param float|int $interval
  185. * @param callable $callback
  186. * @return \React\EventLoop\Timer\Timer|TimerInterface
  187. */
  188. public function addPeriodicTimer($interval, $callback)
  189. {
  190. return $this->_eventLoop->addPeriodicTimer($interval, $callback);
  191. }
  192. /**
  193. * @param TimerInterface $timer
  194. */
  195. public function cancelTimer(TimerInterface $timer)
  196. {
  197. return $this->_eventLoop->cancelTimer($timer);
  198. }
  199. /**
  200. * @param callable $listener
  201. */
  202. public function futureTick($listener)
  203. {
  204. return $this->_eventLoop->futureTick($listener);
  205. }
  206. /**
  207. * @param int $signal
  208. * @param callable $listener
  209. */
  210. public function addSignal($signal, $listener)
  211. {
  212. return $this->_eventLoop->addSignal($signal, $listener);
  213. }
  214. /**
  215. * @param int $signal
  216. * @param callable $listener
  217. */
  218. public function removeSignal($signal, $listener)
  219. {
  220. return $this->_eventLoop->removeSignal($signal, $listener);
  221. }
  222. /**
  223. * Run.
  224. */
  225. public function run()
  226. {
  227. return $this->_eventLoop->run();
  228. }
  229. /**
  230. * Stop.
  231. */
  232. public function stop()
  233. {
  234. return $this->_eventLoop->stop();
  235. }
  236. }