Select.php 10 KB

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