ExtLibevLoop.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace React\EventLoop;
  3. use BadMethodCallException;
  4. use libev\EventLoop;
  5. use libev\IOEvent;
  6. use libev\SignalEvent;
  7. use libev\TimerEvent;
  8. use React\EventLoop\Tick\FutureTickQueue;
  9. use React\EventLoop\Timer\Timer;
  10. use SplObjectStorage;
  11. /**
  12. * [Deprecated] An `ext-libev` based event loop.
  13. *
  14. * This uses an [unofficial `libev` extension](https://github.com/m4rw3r/php-libev),
  15. * that provides an interface to `libev` library.
  16. * `libev` itself supports a number of system-specific backends (epoll, kqueue).
  17. *
  18. * This loop does only work with PHP 5.
  19. * An update for PHP 7 is [unlikely](https://github.com/m4rw3r/php-libev/issues/8)
  20. * to happen any time soon.
  21. *
  22. * @see https://github.com/m4rw3r/php-libev
  23. * @see https://gist.github.com/1688204
  24. * @deprecated 1.2.0, use [`ExtEvLoop`](#extevloop) instead.
  25. */
  26. final class ExtLibevLoop implements LoopInterface
  27. {
  28. private $loop;
  29. private $futureTickQueue;
  30. private $timerEvents;
  31. private $readEvents = array();
  32. private $writeEvents = array();
  33. private $running;
  34. private $signals;
  35. private $signalEvents = array();
  36. public function __construct()
  37. {
  38. if (!\class_exists('libev\EventLoop', false)) {
  39. throw new BadMethodCallException('Cannot create ExtLibevLoop, ext-libev extension missing');
  40. }
  41. $this->loop = new EventLoop();
  42. $this->futureTickQueue = new FutureTickQueue();
  43. $this->timerEvents = new SplObjectStorage();
  44. $this->signals = new SignalsHandler();
  45. }
  46. public function addReadStream($stream, $listener)
  47. {
  48. if (isset($this->readEvents[(int) $stream])) {
  49. return;
  50. }
  51. $callback = function () use ($stream, $listener) {
  52. \call_user_func($listener, $stream);
  53. };
  54. $event = new IOEvent($callback, $stream, IOEvent::READ);
  55. $this->loop->add($event);
  56. $this->readEvents[(int) $stream] = $event;
  57. }
  58. public function addWriteStream($stream, $listener)
  59. {
  60. if (isset($this->writeEvents[(int) $stream])) {
  61. return;
  62. }
  63. $callback = function () use ($stream, $listener) {
  64. \call_user_func($listener, $stream);
  65. };
  66. $event = new IOEvent($callback, $stream, IOEvent::WRITE);
  67. $this->loop->add($event);
  68. $this->writeEvents[(int) $stream] = $event;
  69. }
  70. public function removeReadStream($stream)
  71. {
  72. $key = (int) $stream;
  73. if (isset($this->readEvents[$key])) {
  74. $this->readEvents[$key]->stop();
  75. $this->loop->remove($this->readEvents[$key]);
  76. unset($this->readEvents[$key]);
  77. }
  78. }
  79. public function removeWriteStream($stream)
  80. {
  81. $key = (int) $stream;
  82. if (isset($this->writeEvents[$key])) {
  83. $this->writeEvents[$key]->stop();
  84. $this->loop->remove($this->writeEvents[$key]);
  85. unset($this->writeEvents[$key]);
  86. }
  87. }
  88. public function addTimer($interval, $callback)
  89. {
  90. $timer = new Timer( $interval, $callback, false);
  91. $that = $this;
  92. $timers = $this->timerEvents;
  93. $callback = function () use ($timer, $timers, $that) {
  94. \call_user_func($timer->getCallback(), $timer);
  95. if ($timers->contains($timer)) {
  96. $that->cancelTimer($timer);
  97. }
  98. };
  99. $event = new TimerEvent($callback, $timer->getInterval());
  100. $this->timerEvents->attach($timer, $event);
  101. $this->loop->add($event);
  102. return $timer;
  103. }
  104. public function addPeriodicTimer($interval, $callback)
  105. {
  106. $timer = new Timer($interval, $callback, true);
  107. $callback = function () use ($timer) {
  108. \call_user_func($timer->getCallback(), $timer);
  109. };
  110. $event = new TimerEvent($callback, $timer->getInterval(), $timer->getInterval());
  111. $this->timerEvents->attach($timer, $event);
  112. $this->loop->add($event);
  113. return $timer;
  114. }
  115. public function cancelTimer(TimerInterface $timer)
  116. {
  117. if (isset($this->timerEvents[$timer])) {
  118. $this->loop->remove($this->timerEvents[$timer]);
  119. $this->timerEvents->detach($timer);
  120. }
  121. }
  122. public function futureTick($listener)
  123. {
  124. $this->futureTickQueue->add($listener);
  125. }
  126. public function addSignal($signal, $listener)
  127. {
  128. $this->signals->add($signal, $listener);
  129. if (!isset($this->signalEvents[$signal])) {
  130. $signals = $this->signals;
  131. $this->signalEvents[$signal] = new SignalEvent(function () use ($signals, $signal) {
  132. $signals->call($signal);
  133. }, $signal);
  134. $this->loop->add($this->signalEvents[$signal]);
  135. }
  136. }
  137. public function removeSignal($signal, $listener)
  138. {
  139. $this->signals->remove($signal, $listener);
  140. if (isset($this->signalEvents[$signal]) && $this->signals->count($signal) === 0) {
  141. $this->signalEvents[$signal]->stop();
  142. $this->loop->remove($this->signalEvents[$signal]);
  143. unset($this->signalEvents[$signal]);
  144. }
  145. }
  146. public function run()
  147. {
  148. $this->running = true;
  149. while ($this->running) {
  150. $this->futureTickQueue->tick();
  151. $flags = EventLoop::RUN_ONCE;
  152. if (!$this->running || !$this->futureTickQueue->isEmpty()) {
  153. $flags |= EventLoop::RUN_NOWAIT;
  154. } elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count() && $this->signals->isEmpty()) {
  155. break;
  156. }
  157. $this->loop->run($flags);
  158. }
  159. }
  160. public function stop()
  161. {
  162. $this->running = false;
  163. }
  164. }