Select.php 9.8 KB

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