Ev.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 有个鬼<42765633@qq.com>
  10. * @link http://www.workerman.net/
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Workerman\Events;
  14. use Workerman\Worker;
  15. use \EvWatcher;
  16. /**
  17. * ev eventloop
  18. */
  19. class Ev implements EventInterface
  20. {
  21. /**
  22. * All listeners for read/write event.
  23. *
  24. * @var array
  25. */
  26. protected $_allEvents = array();
  27. /**
  28. * Event listeners of signal.
  29. *
  30. * @var array
  31. */
  32. protected $_eventSignal = array();
  33. /**
  34. * All timer event listeners.
  35. * [func, args, event, flag, time_interval]
  36. *
  37. * @var array
  38. */
  39. protected $_eventTimer = array();
  40. /**
  41. * Timer id.
  42. *
  43. * @var int
  44. */
  45. protected static $_timerId = 1;
  46. /**
  47. * Add a timer.
  48. * {@inheritdoc}
  49. */
  50. public function add($fd, $flag, $func, $args = null)
  51. {
  52. $callback = function ($event, $socket) use ($fd, $func) {
  53. try {
  54. \call_user_func($func, $fd);
  55. } catch (\Exception $e) {
  56. Worker::stopAll(250, $e);
  57. } catch (\Error $e) {
  58. Worker::stopAll(250, $e);
  59. }
  60. };
  61. switch ($flag) {
  62. case self::EV_SIGNAL:
  63. $event = new \EvSignal($fd, $callback);
  64. $this->_eventSignal[$fd] = $event;
  65. return true;
  66. case self::EV_TIMER:
  67. case self::EV_TIMER_ONCE:
  68. $repeat = $flag === self::EV_TIMER_ONCE ? 0 : $fd;
  69. $param = array($func, (array)$args, $flag, $fd, self::$_timerId);
  70. $event = new \EvTimer($fd, $repeat, array($this, 'timerCallback'), $param);
  71. $this->_eventTimer[self::$_timerId] = $event;
  72. return self::$_timerId++;
  73. default :
  74. $fd_key = (int)$fd;
  75. $real_flag = $flag === self::EV_READ ? \Ev::READ : \Ev::WRITE;
  76. $event = new \EvIo($fd, $real_flag, $callback);
  77. $this->_allEvents[$fd_key][$flag] = $event;
  78. return true;
  79. }
  80. }
  81. /**
  82. * Remove a timer.
  83. * {@inheritdoc}
  84. */
  85. public function del($fd, $flag)
  86. {
  87. switch ($flag) {
  88. case self::EV_READ:
  89. case self::EV_WRITE:
  90. $fd_key = (int)$fd;
  91. if (isset($this->_allEvents[$fd_key][$flag])) {
  92. $this->_allEvents[$fd_key][$flag]->stop();
  93. unset($this->_allEvents[$fd_key][$flag]);
  94. }
  95. if (empty($this->_allEvents[$fd_key])) {
  96. unset($this->_allEvents[$fd_key]);
  97. }
  98. break;
  99. case self::EV_SIGNAL:
  100. $fd_key = (int)$fd;
  101. if (isset($this->_eventSignal[$fd_key])) {
  102. $this->_eventSignal[$fd_key]->stop();
  103. unset($this->_eventSignal[$fd_key]);
  104. }
  105. break;
  106. case self::EV_TIMER:
  107. case self::EV_TIMER_ONCE:
  108. if (isset($this->_eventTimer[$fd])) {
  109. $this->_eventTimer[$fd]->stop();
  110. unset($this->_eventTimer[$fd]);
  111. }
  112. break;
  113. }
  114. return true;
  115. }
  116. /**
  117. * Timer callback.
  118. *
  119. * @param EvWatcher $event
  120. */
  121. public function timerCallback(EvWatcher $event)
  122. {
  123. $param = $event->data;
  124. $timer_id = $param[4];
  125. if ($param[2] === self::EV_TIMER_ONCE) {
  126. $this->_eventTimer[$timer_id]->stop();
  127. unset($this->_eventTimer[$timer_id]);
  128. }
  129. try {
  130. \call_user_func_array($param[0], $param[1]);
  131. } catch (\Exception $e) {
  132. Worker::stopAll(250, $e);
  133. } catch (\Error $e) {
  134. Worker::stopAll(250, $e);
  135. }
  136. }
  137. /**
  138. * Remove all timers.
  139. *
  140. * @return void
  141. */
  142. public function clearAllTimer()
  143. {
  144. foreach ($this->_eventTimer as $event) {
  145. $event->stop();
  146. }
  147. $this->_eventTimer = array();
  148. }
  149. /**
  150. * Main loop.
  151. *
  152. * @see EventInterface::loop()
  153. */
  154. public function loop()
  155. {
  156. \Ev::run();
  157. }
  158. /**
  159. * Destroy loop.
  160. *
  161. * @return void
  162. */
  163. public function destroy()
  164. {
  165. foreach ($this->_allEvents as $event) {
  166. $event->stop();
  167. }
  168. }
  169. /**
  170. * Get timer count.
  171. *
  172. * @return integer
  173. */
  174. public function getTimerCount()
  175. {
  176. return \count($this->_eventTimer);
  177. }
  178. }