Ev.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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::log($e);
  57. exit(250);
  58. } catch (\Error $e) {
  59. Worker::log($e);
  60. exit(250);
  61. }
  62. };
  63. switch ($flag) {
  64. case self::EV_SIGNAL:
  65. $event = new \EvSignal($fd, $callback);
  66. $this->_eventSignal[$fd] = $event;
  67. return true;
  68. case self::EV_TIMER:
  69. case self::EV_TIMER_ONCE:
  70. $repeat = $flag === self::EV_TIMER_ONCE ? 0 : $fd;
  71. $param = array($func, (array)$args, $flag, $fd, self::$_timerId);
  72. $event = new \EvTimer($fd, $repeat, array($this, 'timerCallback'), $param);
  73. $this->_eventTimer[self::$_timerId] = $event;
  74. return self::$_timerId++;
  75. default :
  76. $fd_key = (int)$fd;
  77. $real_flag = $flag === self::EV_READ ? \Ev::READ : \Ev::WRITE;
  78. $event = new \EvIo($fd, $real_flag, $callback);
  79. $this->_allEvents[$fd_key][$flag] = $event;
  80. return true;
  81. }
  82. }
  83. /**
  84. * Remove a timer.
  85. * {@inheritdoc}
  86. */
  87. public function del($fd, $flag)
  88. {
  89. switch ($flag) {
  90. case self::EV_READ:
  91. case self::EV_WRITE:
  92. $fd_key = (int)$fd;
  93. if (isset($this->_allEvents[$fd_key][$flag])) {
  94. $this->_allEvents[$fd_key][$flag]->stop();
  95. unset($this->_allEvents[$fd_key][$flag]);
  96. }
  97. if (empty($this->_allEvents[$fd_key])) {
  98. unset($this->_allEvents[$fd_key]);
  99. }
  100. break;
  101. case self::EV_SIGNAL:
  102. $fd_key = (int)$fd;
  103. if (isset($this->_eventSignal[$fd_key])) {
  104. $this->_eventSignal[$fd_key]->stop();
  105. unset($this->_eventSignal[$fd_key]);
  106. }
  107. break;
  108. case self::EV_TIMER:
  109. case self::EV_TIMER_ONCE:
  110. if (isset($this->_eventTimer[$fd])) {
  111. $this->_eventTimer[$fd]->stop();
  112. unset($this->_eventTimer[$fd]);
  113. }
  114. break;
  115. }
  116. return true;
  117. }
  118. /**
  119. * Timer callback.
  120. *
  121. * @param EvWatcher $event
  122. */
  123. public function timerCallback(EvWatcher $event)
  124. {
  125. $param = $event->data;
  126. $timer_id = $param[4];
  127. if ($param[2] === self::EV_TIMER_ONCE) {
  128. $this->_eventTimer[$timer_id]->stop();
  129. unset($this->_eventTimer[$timer_id]);
  130. }
  131. try {
  132. \call_user_func_array($param[0], $param[1]);
  133. } catch (\Exception $e) {
  134. Worker::log($e);
  135. exit(250);
  136. } catch (\Error $e) {
  137. Worker::log($e);
  138. exit(250);
  139. }
  140. }
  141. /**
  142. * Remove all timers.
  143. *
  144. * @return void
  145. */
  146. public function clearAllTimer()
  147. {
  148. foreach ($this->_eventTimer as $event) {
  149. $event->stop();
  150. }
  151. $this->_eventTimer = array();
  152. }
  153. /**
  154. * Main loop.
  155. *
  156. * @see EventInterface::loop()
  157. */
  158. public function loop()
  159. {
  160. \Ev::run();
  161. }
  162. /**
  163. * Destroy loop.
  164. *
  165. * @return void
  166. */
  167. public function destroy()
  168. {
  169. foreach ($this->_allEvents as $event) {
  170. $event->stop();
  171. }
  172. }
  173. /**
  174. * Get timer count.
  175. *
  176. * @return integer
  177. */
  178. public function getTimerCount()
  179. {
  180. return \count($this->_eventTimer);
  181. }
  182. }