Ev.php 4.4 KB

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