Ev.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. * @var array
  22. */
  23. protected $_allEvents = array();
  24. /**
  25. * Event listeners of signal.
  26. * @var array
  27. */
  28. protected $_eventSignal = array();
  29. /**
  30. * All timer event listeners.
  31. * [func, args, event, flag, time_interval]
  32. * @var array
  33. */
  34. protected $_eventTimer = array();
  35. /**
  36. * Timer id.
  37. * @var int
  38. */
  39. protected static $_timerId = 1;
  40. /**
  41. * Add a timer.
  42. * {@inheritdoc}
  43. */
  44. public function add($fd, $flag, $func, $args=null)
  45. {
  46. $callback = function($event,$socket)use($fd,$func)
  47. {
  48. try
  49. {
  50. call_user_func($func,$fd);
  51. }
  52. catch(\Exception $e)
  53. {
  54. echo $e;
  55. exit(250);
  56. }
  57. };
  58. switch($flag)
  59. {
  60. case self::EV_SIGNAL:
  61. $event = new \EvSignal($fd, $callback);
  62. $this->_eventSignal[$fd] = $event;
  63. return true;
  64. case self::EV_TIMER:
  65. case self::EV_TIMER_ONCE:
  66. $repeat = $flag==self::EV_TIMER_ONCE ? 0 : $fd;
  67. $param = array($func, (array)$args, $flag, $fd, self::$_timerId);
  68. $event = new \EvTimer($fd, $repeat, array($this, 'timerCallback'),$param);
  69. $this->_eventTimer[self::$_timerId] = $event;
  70. return self::$_timerId++;
  71. default :
  72. $fd_key = (int)$fd;
  73. $real_flag = $flag === self::EV_READ ? \Ev::READ : \Ev::WRITE;
  74. $event = new \EvIo($fd, $real_flag, $callback);
  75. $this->_allEvents[$fd_key][$flag] = $event;
  76. return true;
  77. }
  78. }
  79. /**
  80. * Remove a timer.
  81. * {@inheritdoc}
  82. */
  83. public function del($fd ,$flag)
  84. {
  85. switch($flag)
  86. {
  87. case self::EV_READ:
  88. case self::EV_WRITE:
  89. $fd_key = (int)$fd;
  90. if(isset($this->_allEvents[$fd_key][$flag]))
  91. {
  92. $this->_allEvents[$fd_key][$flag]->stop();
  93. unset($this->_allEvents[$fd_key][$flag]);
  94. }
  95. if(empty($this->_allEvents[$fd_key]))
  96. {
  97. unset($this->_allEvents[$fd_key]);
  98. }
  99. break;
  100. case self::EV_SIGNAL:
  101. $fd_key = (int)$fd;
  102. if(isset($this->_eventSignal[$fd_key]))
  103. {
  104. $this->_allEvents[$fd_key][$flag]->stop();
  105. unset($this->_eventSignal[$fd_key]);
  106. }
  107. break;
  108. case self::EV_TIMER:
  109. case self::EV_TIMER_ONCE:
  110. if(isset($this->_eventTimer[$fd]))
  111. {
  112. $this->_eventTimer[$fd]->stop();
  113. unset($this->_eventTimer[$fd]);
  114. }
  115. break;
  116. }
  117. return true;
  118. }
  119. /**
  120. * Timer callback.
  121. * @param \EvWatcher $event
  122. */
  123. public function timerCallback($event)
  124. {
  125. $param = $event->data;
  126. $timer_id = $param[4];
  127. if($param[2] === self::EV_TIMER_ONCE)
  128. {
  129. $this->_eventTimer[$timer_id]->stop();
  130. unset($this->_eventTimer[$timer_id]);
  131. }
  132. try
  133. {
  134. call_user_func_array($param[0],$param[1]);
  135. }
  136. catch(\Exception $e)
  137. {
  138. echo $e;
  139. exit(250);
  140. }
  141. }
  142. /**
  143. * Remove all timers.
  144. * @return void
  145. */
  146. public function clearAllTimer()
  147. {
  148. foreach($this->_eventTimer as $event)
  149. {
  150. $event->stop();
  151. }
  152. $this->_eventTimer = array();
  153. }
  154. /**
  155. * Main loop.
  156. * @see EventInterface::loop()
  157. */
  158. public function loop()
  159. {
  160. \Ev::run();
  161. }
  162. }