Ev.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  15. * ev eventloop
  16. */
  17. class Ev implements EventInterface
  18. {
  19. /**
  20. * All listeners for read/write event.
  21. *
  22. * @var array
  23. */
  24. protected $_allEvents = array();
  25. /**
  26. * Event listeners of signal.
  27. *
  28. * @var array
  29. */
  30. protected $_eventSignal = array();
  31. /**
  32. * All timer event listeners.
  33. * [func, args, event, flag, time_interval]
  34. *
  35. * @var array
  36. */
  37. protected $_eventTimer = array();
  38. /**
  39. * Timer id.
  40. *
  41. * @var int
  42. */
  43. protected static $_timerId = 1;
  44. /**
  45. * Add a timer.
  46. * {@inheritdoc}
  47. */
  48. public function add($fd, $flag, $func, $args = null)
  49. {
  50. $callback = function ($event, $socket) use ($fd, $func) {
  51. try {
  52. call_user_func($func, $fd);
  53. } catch (\Exception $e) {
  54. echo $e;
  55. exit(250);
  56. } catch (\Error $e) {
  57. echo $e;
  58. exit(250);
  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->_allEvents[$fd_key][$flag]->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($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. echo $e;
  133. exit(250);
  134. } catch (\Error $e) {
  135. echo $e;
  136. exit(250);
  137. }
  138. }
  139. /**
  140. * Remove all timers.
  141. *
  142. * @return void
  143. */
  144. public function clearAllTimer()
  145. {
  146. foreach ($this->_eventTimer as $event) {
  147. $event->stop();
  148. }
  149. $this->_eventTimer = array();
  150. }
  151. /**
  152. * Main loop.
  153. *
  154. * @see EventInterface::loop()
  155. */
  156. public function loop()
  157. {
  158. \Ev::run();
  159. }
  160. }