Ev.php 5.0 KB

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