Select.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. /**
  16. * select eventloop
  17. */
  18. class Select implements EventInterface
  19. {
  20. /**
  21. * All listeners for read/write event.
  22. *
  23. * @var array
  24. */
  25. public $_allEvents = array();
  26. /**
  27. * Event listeners of signal.
  28. *
  29. * @var array
  30. */
  31. public $_signalEvents = array();
  32. /**
  33. * Fds waiting for read event.
  34. *
  35. * @var array
  36. */
  37. protected $_readFds = array();
  38. /**
  39. * Fds waiting for write event.
  40. *
  41. * @var array
  42. */
  43. protected $_writeFds = array();
  44. /**
  45. * Timer scheduler.
  46. * {['data':timer_id, 'priority':run_timestamp], ..}
  47. *
  48. * @var \SplPriorityQueue
  49. */
  50. protected $_scheduler = null;
  51. /**
  52. * All timer event listeners.
  53. * [[func, args, flag, timer_interval], ..]
  54. *
  55. * @var array
  56. */
  57. protected $_task = array();
  58. /**
  59. * Timer id.
  60. *
  61. * @var int
  62. */
  63. protected $_timerId = 1;
  64. /**
  65. * Select timeout.
  66. *
  67. * @var int
  68. */
  69. protected $_selectTimeout = 100000000;
  70. /**
  71. * Paired socket channels
  72. *
  73. * @var array
  74. */
  75. protected $channel = array();
  76. /**
  77. * Construct.
  78. */
  79. public function __construct()
  80. {
  81. // Create a pipeline and put into the collection of the read to read the descriptor to avoid empty polling.
  82. $this->channel = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
  83. if ($this->channel) {
  84. stream_set_blocking($this->channel[0], 0);
  85. $this->_readFds[0] = $this->channel[0];
  86. }
  87. // Init SplPriorityQueue.
  88. $this->_scheduler = new \SplPriorityQueue();
  89. $this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function add($fd, $flag, $func, $args = array())
  95. {
  96. switch ($flag) {
  97. case self::EV_READ:
  98. $fd_key = (int)$fd;
  99. $this->_allEvents[$fd_key][$flag] = array($func, $fd);
  100. $this->_readFds[$fd_key] = $fd;
  101. break;
  102. case self::EV_WRITE:
  103. $fd_key = (int)$fd;
  104. $this->_allEvents[$fd_key][$flag] = array($func, $fd);
  105. $this->_writeFds[$fd_key] = $fd;
  106. break;
  107. case self::EV_SIGNAL:
  108. $fd_key = (int)$fd;
  109. $this->_signalEvents[$fd_key][$flag] = array($func, $fd);
  110. pcntl_signal($fd, array($this, 'signalHandler'));
  111. break;
  112. case self::EV_TIMER:
  113. case self::EV_TIMER_ONCE:
  114. $run_time = microtime(true) + $fd;
  115. $this->_scheduler->insert($this->_timerId, -$run_time);
  116. $this->_task[$this->_timerId] = array($func, (array)$args, $flag, $fd);
  117. $this->tick();
  118. return $this->_timerId++;
  119. }
  120. return true;
  121. }
  122. /**
  123. * Signal handler.
  124. *
  125. * @param int $signal
  126. */
  127. public function signalHandler($signal)
  128. {
  129. call_user_func_array($this->_signalEvents[$signal][self::EV_SIGNAL][0], array($signal));
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function del($fd, $flag)
  135. {
  136. $fd_key = (int)$fd;
  137. switch ($flag) {
  138. case self::EV_READ:
  139. unset($this->_allEvents[$fd_key][$flag], $this->_readFds[$fd_key]);
  140. if (empty($this->_allEvents[$fd_key])) {
  141. unset($this->_allEvents[$fd_key]);
  142. }
  143. return true;
  144. case self::EV_WRITE:
  145. unset($this->_allEvents[$fd_key][$flag], $this->_writeFds[$fd_key]);
  146. if (empty($this->_allEvents[$fd_key])) {
  147. unset($this->_allEvents[$fd_key]);
  148. }
  149. return true;
  150. case self::EV_SIGNAL:
  151. unset($this->_signalEvents[$fd_key]);
  152. pcntl_signal($fd, SIG_IGN);
  153. break;
  154. case self::EV_TIMER:
  155. case self::EV_TIMER_ONCE;
  156. unset($this->_task[$fd_key]);
  157. return true;
  158. }
  159. return false;
  160. }
  161. /**
  162. * Tick for timer.
  163. *
  164. * @return void
  165. */
  166. protected function tick()
  167. {
  168. while (!$this->_scheduler->isEmpty()) {
  169. $scheduler_data = $this->_scheduler->top();
  170. $timer_id = $scheduler_data['data'];
  171. $next_run_time = -$scheduler_data['priority'];
  172. $time_now = microtime(true);
  173. if ($time_now >= $next_run_time) {
  174. $this->_scheduler->extract();
  175. if (!isset($this->_task[$timer_id])) {
  176. continue;
  177. }
  178. // [func, args, flag, timer_interval]
  179. $task_data = $this->_task[$timer_id];
  180. if ($task_data[2] === self::EV_TIMER) {
  181. $next_run_time = $time_now + $task_data[3];
  182. $this->_scheduler->insert($timer_id, -$next_run_time);
  183. }
  184. call_user_func_array($task_data[0], $task_data[1]);
  185. if (isset($this->_task[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE) {
  186. $this->del($timer_id, self::EV_TIMER_ONCE);
  187. }
  188. continue;
  189. } else {
  190. $this->_selectTimeout = ($next_run_time - $time_now) * 1000000;
  191. return;
  192. }
  193. }
  194. $this->_selectTimeout = 100000000;
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function clearAllTimer()
  200. {
  201. $this->_scheduler = new \SplPriorityQueue();
  202. $this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
  203. $this->_task = array();
  204. }
  205. /**
  206. * {@inheritdoc}
  207. */
  208. public function loop()
  209. {
  210. $e = null;
  211. while (1) {
  212. // Calls signal handlers for pending signals
  213. pcntl_signal_dispatch();
  214. $read = $this->_readFds;
  215. $write = $this->_writeFds;
  216. // Waiting read/write/signal/timeout events.
  217. $ret = @stream_select($read, $write, $e, 0, $this->_selectTimeout);
  218. if (!$this->_scheduler->isEmpty()) {
  219. $this->tick();
  220. }
  221. if (!$ret) {
  222. continue;
  223. }
  224. foreach ($read as $fd) {
  225. $fd_key = (int)$fd;
  226. if (isset($this->_allEvents[$fd_key][self::EV_READ])) {
  227. call_user_func_array($this->_allEvents[$fd_key][self::EV_READ][0],
  228. array($this->_allEvents[$fd_key][self::EV_READ][1]));
  229. }
  230. }
  231. foreach ($write as $fd) {
  232. $fd_key = (int)$fd;
  233. if (isset($this->_allEvents[$fd_key][self::EV_WRITE])) {
  234. call_user_func_array($this->_allEvents[$fd_key][self::EV_WRITE][0],
  235. array($this->_allEvents[$fd_key][self::EV_WRITE][1]));
  236. }
  237. }
  238. }
  239. }
  240. }