Select.php 9.5 KB

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