Select.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. * Fds waiting for except event.
  46. *
  47. * @var array
  48. */
  49. protected $_exceptFds = array();
  50. /**
  51. * Timer scheduler.
  52. * {['data':timer_id, 'priority':run_timestamp], ..}
  53. *
  54. * @var \SplPriorityQueue
  55. */
  56. protected $_scheduler = null;
  57. /**
  58. * All timer event listeners.
  59. * [[func, args, flag, timer_interval], ..]
  60. *
  61. * @var array
  62. */
  63. protected $_eventTimer = array();
  64. /**
  65. * Timer id.
  66. *
  67. * @var int
  68. */
  69. protected $_timerId = 1;
  70. /**
  71. * Select timeout.
  72. *
  73. * @var int
  74. */
  75. protected $_selectTimeout = 100000000;
  76. /**
  77. * Paired socket channels
  78. *
  79. * @var array
  80. */
  81. protected $channel = array();
  82. /**
  83. * Construct.
  84. */
  85. public function __construct()
  86. {
  87. // Create a pipeline and put into the collection of the read to read the descriptor to avoid empty polling.
  88. $this->channel = stream_socket_pair(DIRECTORY_SEPARATOR === '/' ? STREAM_PF_UNIX : STREAM_PF_INET,
  89. STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
  90. if($this->channel) {
  91. stream_set_blocking($this->channel[0], 0);
  92. $this->_readFds[0] = $this->channel[0];
  93. }
  94. // Init SplPriorityQueue.
  95. $this->_scheduler = new \SplPriorityQueue();
  96. $this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function add($fd, $flag, $func, $args = array())
  102. {
  103. switch ($flag) {
  104. case self::EV_READ:
  105. $fd_key = (int)$fd;
  106. $this->_allEvents[$fd_key][$flag] = array($func, $fd);
  107. $this->_readFds[$fd_key] = $fd;
  108. break;
  109. case self::EV_WRITE:
  110. $fd_key = (int)$fd;
  111. $this->_allEvents[$fd_key][$flag] = array($func, $fd);
  112. $this->_writeFds[$fd_key] = $fd;
  113. break;
  114. case self::EV_EXCEPT:
  115. $fd_key = (int)$fd;
  116. $this->_allEvents[$fd_key][$flag] = array($func, $fd);
  117. $this->_exceptFds[$fd_key] = $fd;
  118. break;
  119. case self::EV_SIGNAL:
  120. // Windows not support signal.
  121. if(DIRECTORY_SEPARATOR !== '/') {
  122. return false;
  123. }
  124. $fd_key = (int)$fd;
  125. $this->_signalEvents[$fd_key][$flag] = array($func, $fd);
  126. pcntl_signal($fd, array($this, 'signalHandler'));
  127. break;
  128. case self::EV_TIMER:
  129. case self::EV_TIMER_ONCE:
  130. $run_time = microtime(true) + $fd;
  131. $this->_scheduler->insert($this->_timerId, -$run_time);
  132. $this->_eventTimer[$this->_timerId] = array($func, (array)$args, $flag, $fd);
  133. $this->tick();
  134. return $this->_timerId++;
  135. }
  136. return true;
  137. }
  138. /**
  139. * Signal handler.
  140. *
  141. * @param int $signal
  142. */
  143. public function signalHandler($signal)
  144. {
  145. call_user_func_array($this->_signalEvents[$signal][self::EV_SIGNAL][0], array($signal));
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function del($fd, $flag)
  151. {
  152. $fd_key = (int)$fd;
  153. switch ($flag) {
  154. case self::EV_READ:
  155. unset($this->_allEvents[$fd_key][$flag], $this->_readFds[$fd_key]);
  156. if (empty($this->_allEvents[$fd_key])) {
  157. unset($this->_allEvents[$fd_key]);
  158. }
  159. return true;
  160. case self::EV_WRITE:
  161. unset($this->_allEvents[$fd_key][$flag], $this->_writeFds[$fd_key]);
  162. if (empty($this->_allEvents[$fd_key])) {
  163. unset($this->_allEvents[$fd_key]);
  164. }
  165. return true;
  166. case self::EV_EXCEPT:
  167. unset($this->_allEvents[$fd_key][$flag], $this->_exceptFds[$fd_key]);
  168. if(empty($this->_allEvents[$fd_key]))
  169. {
  170. unset($this->_allEvents[$fd_key]);
  171. }
  172. return true;
  173. case self::EV_SIGNAL:
  174. if(DIRECTORY_SEPARATOR !== '/') {
  175. return false;
  176. }
  177. unset($this->_signalEvents[$fd_key]);
  178. pcntl_signal($fd, SIG_IGN);
  179. break;
  180. case self::EV_TIMER:
  181. case self::EV_TIMER_ONCE;
  182. unset($this->_eventTimer[$fd_key]);
  183. return true;
  184. }
  185. return false;
  186. }
  187. /**
  188. * Tick for timer.
  189. *
  190. * @return void
  191. */
  192. protected function tick()
  193. {
  194. while (!$this->_scheduler->isEmpty()) {
  195. $scheduler_data = $this->_scheduler->top();
  196. $timer_id = $scheduler_data['data'];
  197. $next_run_time = -$scheduler_data['priority'];
  198. $time_now = microtime(true);
  199. $this->_selectTimeout = ($next_run_time - $time_now) * 1000000;
  200. if ($this->_selectTimeout <= 0) {
  201. $this->_scheduler->extract();
  202. if (!isset($this->_eventTimer[$timer_id])) {
  203. continue;
  204. }
  205. // [func, args, flag, timer_interval]
  206. $task_data = $this->_eventTimer[$timer_id];
  207. if ($task_data[2] === self::EV_TIMER) {
  208. $next_run_time = $time_now + $task_data[3];
  209. $this->_scheduler->insert($timer_id, -$next_run_time);
  210. }
  211. call_user_func_array($task_data[0], $task_data[1]);
  212. if (isset($this->_eventTimer[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE) {
  213. $this->del($timer_id, self::EV_TIMER_ONCE);
  214. }
  215. continue;
  216. }
  217. return;
  218. }
  219. $this->_selectTimeout = 100000000;
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function clearAllTimer()
  225. {
  226. $this->_scheduler = new \SplPriorityQueue();
  227. $this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
  228. $this->_eventTimer = array();
  229. }
  230. /**
  231. * {@inheritdoc}
  232. */
  233. public function loop()
  234. {
  235. $e = null;
  236. while (1) {
  237. if(DIRECTORY_SEPARATOR === '/') {
  238. // Calls signal handlers for pending signals
  239. pcntl_signal_dispatch();
  240. }
  241. $read = $this->_readFds;
  242. $write = $this->_writeFds;
  243. $except = $this->_writeFds;
  244. // Waiting read/write/signal/timeout events.
  245. $ret = @stream_select($read, $write, $except, 0, $this->_selectTimeout);
  246. if (!$this->_scheduler->isEmpty()) {
  247. $this->tick();
  248. }
  249. if (!$ret) {
  250. continue;
  251. }
  252. if ($read) {
  253. foreach ($read as $fd) {
  254. $fd_key = (int)$fd;
  255. if (isset($this->_allEvents[$fd_key][self::EV_READ])) {
  256. call_user_func_array($this->_allEvents[$fd_key][self::EV_READ][0],
  257. array($this->_allEvents[$fd_key][self::EV_READ][1]));
  258. }
  259. }
  260. }
  261. if ($write) {
  262. foreach ($write as $fd) {
  263. $fd_key = (int)$fd;
  264. if (isset($this->_allEvents[$fd_key][self::EV_WRITE])) {
  265. call_user_func_array($this->_allEvents[$fd_key][self::EV_WRITE][0],
  266. array($this->_allEvents[$fd_key][self::EV_WRITE][1]));
  267. }
  268. }
  269. }
  270. if($except) {
  271. foreach($except as $fd) {
  272. $fd_key = (int) $fd;
  273. if(isset($this->_allEvents[$fd_key][self::EV_EXCEPT])) {
  274. call_user_func_array($this->_allEvents[$fd_key][self::EV_EXCEPT][0],
  275. array($this->_allEvents[$fd_key][self::EV_EXCEPT][1]));
  276. }
  277. }
  278. }
  279. }
  280. }
  281. /**
  282. * Destroy loop.
  283. *
  284. * @return void
  285. */
  286. public function destroy()
  287. {
  288. }
  289. }