Event.php 4.8 KB

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