Event.php 5.2 KB

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