Event.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. if (\class_exists('\\\\EventBase', false)) {
  54. $class_name = '\\\\EventBase';
  55. } else {
  56. $class_name = '\EventBase';
  57. }
  58. $this->_eventBase = new $class_name();
  59. }
  60. /**
  61. * @see EventInterface::add()
  62. */
  63. public function add($fd, $flag, $func, $args=array())
  64. {
  65. if (\class_exists('\\\\Event', false)) {
  66. $class_name = '\\\\Event';
  67. } else {
  68. $class_name = '\Event';
  69. }
  70. switch ($flag) {
  71. case self::EV_SIGNAL:
  72. $fd_key = (int)$fd;
  73. $event = $class_name::signal($this->_eventBase, $fd, $func);
  74. if (!$event||!$event->add()) {
  75. return false;
  76. }
  77. $this->_eventSignal[$fd_key] = $event;
  78. return true;
  79. case self::EV_TIMER:
  80. case self::EV_TIMER_ONCE:
  81. $param = array($func, (array)$args, $flag, $fd, self::$_timerId);
  82. $event = new $class_name($this->_eventBase, -1, $class_name::TIMEOUT|$class_name::PERSIST, array($this, "timerCallback"), $param);
  83. if (!$event||!$event->addTimer($fd)) {
  84. return false;
  85. }
  86. $this->_eventTimer[self::$_timerId] = $event;
  87. return self::$_timerId++;
  88. default :
  89. $fd_key = (int)$fd;
  90. $real_flag = $flag === self::EV_READ ? $class_name::READ | $class_name::PERSIST : $class_name::WRITE | $class_name::PERSIST;
  91. $event = new $class_name($this->_eventBase, $fd, $real_flag, $func, $fd);
  92. if (!$event||!$event->add()) {
  93. return false;
  94. }
  95. $this->_allEvents[$fd_key][$flag] = $event;
  96. return true;
  97. }
  98. }
  99. /**
  100. * @see Events\EventInterface::del()
  101. */
  102. public function del($fd, $flag)
  103. {
  104. switch ($flag) {
  105. case self::EV_READ:
  106. case self::EV_WRITE:
  107. $fd_key = (int)$fd;
  108. if (isset($this->_allEvents[$fd_key][$flag])) {
  109. $this->_allEvents[$fd_key][$flag]->del();
  110. unset($this->_allEvents[$fd_key][$flag]);
  111. }
  112. if (empty($this->_allEvents[$fd_key])) {
  113. unset($this->_allEvents[$fd_key]);
  114. }
  115. break;
  116. case self::EV_SIGNAL:
  117. $fd_key = (int)$fd;
  118. if (isset($this->_eventSignal[$fd_key])) {
  119. $this->_eventSignal[$fd_key]->del();
  120. unset($this->_eventSignal[$fd_key]);
  121. }
  122. break;
  123. case self::EV_TIMER:
  124. case self::EV_TIMER_ONCE:
  125. if (isset($this->_eventTimer[$fd])) {
  126. $this->_eventTimer[$fd]->del();
  127. unset($this->_eventTimer[$fd]);
  128. }
  129. break;
  130. }
  131. return true;
  132. }
  133. /**
  134. * Timer callback.
  135. * @param int|null $fd
  136. * @param int $what
  137. * @param int $timer_id
  138. */
  139. public function timerCallback($fd, $what, $param)
  140. {
  141. $timer_id = $param[4];
  142. if ($param[2] === self::EV_TIMER_ONCE) {
  143. $this->_eventTimer[$timer_id]->del();
  144. unset($this->_eventTimer[$timer_id]);
  145. }
  146. try {
  147. \call_user_func_array($param[0], $param[1]);
  148. } catch (\Exception $e) {
  149. Worker::stopAll(250, $e);
  150. } catch (\Error $e) {
  151. Worker::stopAll(250, $e);
  152. }
  153. }
  154. /**
  155. * @see Events\EventInterface::clearAllTimer()
  156. * @return void
  157. */
  158. public function clearAllTimer()
  159. {
  160. foreach ($this->_eventTimer as $event) {
  161. $event->del();
  162. }
  163. $this->_eventTimer = array();
  164. }
  165. /**
  166. * @see EventInterface::loop()
  167. */
  168. public function loop()
  169. {
  170. $this->_eventBase->loop();
  171. }
  172. /**
  173. * Destroy loop.
  174. *
  175. * @return void
  176. */
  177. public function destroy()
  178. {
  179. $this->_eventBase->exit();
  180. }
  181. /**
  182. * Get timer count.
  183. *
  184. * @return integer
  185. */
  186. public function getTimerCount()
  187. {
  188. return \count($this->_eventTimer);
  189. }
  190. }