Select.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. $timer_id = $this->_timerId++;
  131. $run_time = microtime(true) + $fd;
  132. $this->_scheduler->insert($timer_id, -$run_time);
  133. $this->_eventTimer[$timer_id] = array($func, (array)$args, $flag, $fd);
  134. $this->tick();
  135. return $timer_id;
  136. }
  137. return true;
  138. }
  139. /**
  140. * Signal handler.
  141. *
  142. * @param int $signal
  143. */
  144. public function signalHandler($signal)
  145. {
  146. call_user_func_array($this->_signalEvents[$signal][self::EV_SIGNAL][0], array($signal));
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function del($fd, $flag)
  152. {
  153. $fd_key = (int)$fd;
  154. switch ($flag) {
  155. case self::EV_READ:
  156. unset($this->_allEvents[$fd_key][$flag], $this->_readFds[$fd_key]);
  157. if (empty($this->_allEvents[$fd_key])) {
  158. unset($this->_allEvents[$fd_key]);
  159. }
  160. return true;
  161. case self::EV_WRITE:
  162. unset($this->_allEvents[$fd_key][$flag], $this->_writeFds[$fd_key]);
  163. if (empty($this->_allEvents[$fd_key])) {
  164. unset($this->_allEvents[$fd_key]);
  165. }
  166. return true;
  167. case self::EV_EXCEPT:
  168. unset($this->_allEvents[$fd_key][$flag], $this->_exceptFds[$fd_key]);
  169. if(empty($this->_allEvents[$fd_key]))
  170. {
  171. unset($this->_allEvents[$fd_key]);
  172. }
  173. return true;
  174. case self::EV_SIGNAL:
  175. if(DIRECTORY_SEPARATOR !== '/') {
  176. return false;
  177. }
  178. unset($this->_signalEvents[$fd_key]);
  179. pcntl_signal($fd, SIG_IGN);
  180. break;
  181. case self::EV_TIMER:
  182. case self::EV_TIMER_ONCE;
  183. unset($this->_eventTimer[$fd_key]);
  184. return true;
  185. }
  186. return false;
  187. }
  188. /**
  189. * Tick for timer.
  190. *
  191. * @return void
  192. */
  193. protected function tick()
  194. {
  195. while (!$this->_scheduler->isEmpty()) {
  196. $scheduler_data = $this->_scheduler->top();
  197. $timer_id = $scheduler_data['data'];
  198. $next_run_time = -$scheduler_data['priority'];
  199. $time_now = microtime(true);
  200. $this->_selectTimeout = ($next_run_time - $time_now) * 1000000;
  201. if ($this->_selectTimeout <= 0) {
  202. $this->_scheduler->extract();
  203. if (!isset($this->_eventTimer[$timer_id])) {
  204. continue;
  205. }
  206. // [func, args, flag, timer_interval]
  207. $task_data = $this->_eventTimer[$timer_id];
  208. if ($task_data[2] === self::EV_TIMER) {
  209. $next_run_time = $time_now + $task_data[3];
  210. $this->_scheduler->insert($timer_id, -$next_run_time);
  211. }
  212. call_user_func_array($task_data[0], $task_data[1]);
  213. if (isset($this->_eventTimer[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE) {
  214. $this->del($timer_id, self::EV_TIMER_ONCE);
  215. }
  216. continue;
  217. }
  218. return;
  219. }
  220. $this->_selectTimeout = 100000000;
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function clearAllTimer()
  226. {
  227. $this->_scheduler = new \SplPriorityQueue();
  228. $this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
  229. $this->_eventTimer = array();
  230. }
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function loop()
  235. {
  236. $e = null;
  237. while (1) {
  238. if(DIRECTORY_SEPARATOR === '/') {
  239. // Calls signal handlers for pending signals
  240. pcntl_signal_dispatch();
  241. }
  242. $read = $this->_readFds;
  243. $write = $this->_writeFds;
  244. $except = $this->_writeFds;
  245. // Waiting read/write/signal/timeout events.
  246. $ret = @stream_select($read, $write, $except, 0, $this->_selectTimeout);
  247. if (!$this->_scheduler->isEmpty()) {
  248. $this->tick();
  249. }
  250. if (!$ret) {
  251. continue;
  252. }
  253. if ($read) {
  254. foreach ($read as $fd) {
  255. $fd_key = (int)$fd;
  256. if (isset($this->_allEvents[$fd_key][self::EV_READ])) {
  257. call_user_func_array($this->_allEvents[$fd_key][self::EV_READ][0],
  258. array($this->_allEvents[$fd_key][self::EV_READ][1]));
  259. }
  260. }
  261. }
  262. if ($write) {
  263. foreach ($write as $fd) {
  264. $fd_key = (int)$fd;
  265. if (isset($this->_allEvents[$fd_key][self::EV_WRITE])) {
  266. call_user_func_array($this->_allEvents[$fd_key][self::EV_WRITE][0],
  267. array($this->_allEvents[$fd_key][self::EV_WRITE][1]));
  268. }
  269. }
  270. }
  271. if($except) {
  272. foreach($except as $fd) {
  273. $fd_key = (int) $fd;
  274. if(isset($this->_allEvents[$fd_key][self::EV_EXCEPT])) {
  275. call_user_func_array($this->_allEvents[$fd_key][self::EV_EXCEPT][0],
  276. array($this->_allEvents[$fd_key][self::EV_EXCEPT][1]));
  277. }
  278. }
  279. }
  280. }
  281. }
  282. /**
  283. * Destroy loop.
  284. *
  285. * @return void
  286. */
  287. public function destroy()
  288. {
  289. }
  290. }