| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 | 
							- <?php
 
- /**
 
-  * This file is part of workerman.
 
-  *
 
-  * Licensed under The MIT License
 
-  * For full copyright and license information, please see the MIT-LICENSE.txt
 
-  * Redistributions of files must retain the above copyright notice.
 
-  *
 
-  * @author    walkor<walkor@workerman.net>
 
-  * @copyright walkor<walkor@workerman.net>
 
-  * @link      http://www.workerman.net/
 
-  * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 
-  */
 
- namespace Workerman\Events\React;
 
- use Workerman\Events\EventInterface;
 
- use React\EventLoop\TimerInterface;
 
- /**
 
-  * Class StreamSelectLoop
 
-  * @package Workerman\Events\React
 
-  */
 
- class Base implements \React\EventLoop\LoopInterface
 
- {
 
-     /**
 
-      * @var array
 
-      */
 
-     protected $_timerIdMap = array();
 
-     /**
 
-      * @var int
 
-      */
 
-     protected $_timerIdIndex = 0;
 
-     /**
 
-      * @var array
 
-      */
 
-     protected $_signalHandlerMap = array();
 
-     /**
 
-      * @var \React\EventLoop\LoopInterface
 
-      */
 
-     protected $_eventLoop = null;
 
-     /**
 
-      * Base constructor.
 
-      */
 
-     public function __construct()
 
-     {
 
-         $this->_eventLoop = new \React\EventLoop\StreamSelectLoop();
 
-     }
 
-     /**
 
-      * Add event listener to event loop.
 
-      *
 
-      * @param $fd
 
-      * @param $flag
 
-      * @param $func
 
-      * @param array $args
 
-      * @return bool
 
-      */
 
-     public function add($fd, $flag, $func, $args = array())
 
-     {
 
-         $args = (array)$args;
 
-         switch ($flag) {
 
-             case EventInterface::EV_READ:
 
-                 return $this->addReadStream($fd, $func);
 
-             case EventInterface::EV_WRITE:
 
-                 return $this->addWriteStream($fd, $func);
 
-             case EventInterface::EV_SIGNAL:
 
-                 if (isset($this->_signalHandlerMap[$fd])) {
 
-                     $this->removeSignal($fd, $this->_signalHandlerMap[$fd]);
 
-                 }
 
-                 $this->_signalHandlerMap[$fd] = $func;
 
-                 return $this->addSignal($fd, $func);
 
-             case EventInterface::EV_TIMER:
 
-                 $timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
 
-                     call_user_func_array($func, $args);
 
-                 });
 
-                 $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
 
-                 return $this->_timerIdIndex;
 
-             case EventInterface::EV_TIMER_ONCE:
 
-                 $index = ++$this->_timerIdIndex;
 
-                 $timer_obj = $this->addTimer($fd, function() use ($func, $args, $index) {
 
-                     $this->del($index,EventInterface::EV_TIMER_ONCE);
 
-                     call_user_func_array($func, $args);
 
-                 });
 
-                 $this->_timerIdMap[$index] = $timer_obj;
 
-                 return $this->_timerIdIndex;
 
-         }
 
-         return false;
 
-     }
 
-     /**
 
-      * Remove event listener from event loop.
 
-      *
 
-      * @param mixed $fd
 
-      * @param int   $flag
 
-      * @return bool
 
-      */
 
-     public function del($fd, $flag)
 
-     {
 
-         switch ($flag) {
 
-             case EventInterface::EV_READ:
 
-                 return $this->removeReadStream($fd);
 
-             case EventInterface::EV_WRITE:
 
-                 return $this->removeWriteStream($fd);
 
-             case EventInterface::EV_SIGNAL:
 
-                 if (!isset($this->_eventLoop[$fd])) {
 
-                     return false;
 
-                 }
 
-                 $func = $this->_eventLoop[$fd];
 
-                 unset($this->_eventLoop[$fd]);
 
-                 return $this->removeSignal($fd, $func);
 
-             case EventInterface::EV_TIMER:
 
-             case EventInterface::EV_TIMER_ONCE:
 
-                 if (isset($this->_timerIdMap[$fd])){
 
-                     $timer_obj = $this->_timerIdMap[$fd];
 
-                     unset($this->_timerIdMap[$fd]);
 
-                     $this->cancelTimer($timer_obj);
 
-                     return true;
 
-                 }
 
-         }
 
-         return false;
 
-     }
 
-     /**
 
-      * Main loop.
 
-      *
 
-      * @return void
 
-      */
 
-     public function loop()
 
-     {
 
-         $this->run();
 
-     }
 
-     /**
 
-      * Destroy loop.
 
-      *
 
-      * @return void
 
-      */
 
-     public function destroy()
 
-     {
 
-     }
 
-     /**
 
-      * Get timer count.
 
-      *
 
-      * @return integer
 
-      */
 
-     public function getTimerCount()
 
-     {
 
-         return count($this->_timerIdMap);
 
-     }
 
-     /**
 
-      * @param resource $stream
 
-      * @param callable $listener
 
-      */
 
-     public function addReadStream($stream, $listener)
 
-     {
 
-         return $this->_eventLoop->addReadStream($stream, $listener);
 
-     }
 
-     /**
 
-      * @param resource $stream
 
-      * @param callable $listener
 
-      */
 
-     public function addWriteStream($stream, $listener)
 
-     {
 
-         return $this->_eventLoop->addWriteStream($stream, $listener);
 
-     }
 
-     /**
 
-      * @param resource $stream
 
-      */
 
-     public function removeReadStream($stream)
 
-     {
 
-         return $this->_eventLoop->removeReadStream($stream);
 
-     }
 
-     /**
 
-      * @param resource $stream
 
-      */
 
-     public function removeWriteStream($stream)
 
-     {
 
-         return $this->_eventLoop->removeWriteStream($stream);
 
-     }
 
-     /**
 
-      * @param float|int $interval
 
-      * @param callable $callback
 
-      * @return \React\EventLoop\Timer\Timer|TimerInterface
 
-      */
 
-     public function addTimer($interval, $callback)
 
-     {
 
-         return $this->_eventLoop->addTimer($interval, $callback);
 
-     }
 
-     /**
 
-      * @param float|int $interval
 
-      * @param callable $callback
 
-      * @return \React\EventLoop\Timer\Timer|TimerInterface
 
-      */
 
-     public function addPeriodicTimer($interval, $callback)
 
-     {
 
-         return $this->_eventLoop->addPeriodicTimer($interval, $callback);
 
-     }
 
-     /**
 
-      * @param TimerInterface $timer
 
-      */
 
-     public function cancelTimer(TimerInterface $timer)
 
-     {
 
-         return $this->_eventLoop->cancelTimer($timer);
 
-     }
 
-     /**
 
-      * @param callable $listener
 
-      */
 
-     public function futureTick($listener)
 
-     {
 
-         return $this->_eventLoop->futureTick($listener);
 
-     }
 
-     /**
 
-      * @param int $signal
 
-      * @param callable $listener
 
-      */
 
-     public function addSignal($signal, $listener)
 
-     {
 
-         return $this->_eventLoop->addSignal($signal, $listener);
 
-     }
 
-     /**
 
-      * @param int $signal
 
-      * @param callable $listener
 
-      */
 
-     public function removeSignal($signal, $listener)
 
-     {
 
-         return $this->_eventLoop->removeSignal($signal, $listener);
 
-     }
 
-     /**
 
-      * Run.
 
-      */
 
-     public function run()
 
-     {
 
-         return $this->_eventLoop->run();
 
-     }
 
-     /**
 
-      * Stop.
 
-      */
 
-     public function stop()
 
-     {
 
-         return $this->_eventLoop->stop();
 
-     }
 
- }
 
 
  |