added new socket manager, errorhandling and testing is not finished yet
This commit is contained in:
102
libs/React/EventLoop/Timer/Timer.php
Normal file
102
libs/React/EventLoop/Timer/Timer.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace React\EventLoop\Timer;
|
||||
|
||||
use React\EventLoop\LoopInterface;
|
||||
|
||||
class Timer implements TimerInterface
|
||||
{
|
||||
const MIN_INTERVAL = 0.000001;
|
||||
|
||||
protected $loop;
|
||||
protected $interval;
|
||||
protected $callback;
|
||||
protected $periodic;
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Constructor initializes the fields of the Timer
|
||||
*
|
||||
* @param LoopInterface $loop The loop with which this timer is associated
|
||||
* @param float $interval The interval after which this timer will execute, in seconds
|
||||
* @param callable $callback The callback that will be executed when this timer elapses
|
||||
* @param bool $periodic Whether the time is periodic
|
||||
* @param mixed $data Arbitrary data associated with timer
|
||||
*/
|
||||
public function __construct(LoopInterface $loop, $interval, callable $callback, $periodic = false, $data = null)
|
||||
{
|
||||
if ($interval < self::MIN_INTERVAL) {
|
||||
$interval = self::MIN_INTERVAL;
|
||||
}
|
||||
|
||||
$this->loop = $loop;
|
||||
$this->interval = (float) $interval;
|
||||
$this->callback = $callback;
|
||||
$this->periodic = (bool) $periodic;
|
||||
$this->data = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLoop()
|
||||
{
|
||||
return $this->loop;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getInterval()
|
||||
{
|
||||
return $this->interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCallback()
|
||||
{
|
||||
return $this->callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setData($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isPeriodic()
|
||||
{
|
||||
return $this->periodic;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isActive()
|
||||
{
|
||||
return $this->loop->isTimerActive($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function cancel()
|
||||
{
|
||||
$this->loop->cancelTimer($this);
|
||||
}
|
||||
}
|
62
libs/React/EventLoop/Timer/TimerInterface.php
Normal file
62
libs/React/EventLoop/Timer/TimerInterface.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace React\EventLoop\Timer;
|
||||
|
||||
use React\EventLoop\LoopInterface;
|
||||
|
||||
interface TimerInterface
|
||||
{
|
||||
/**
|
||||
* Get the loop with which this timer is associated
|
||||
*
|
||||
* @return LoopInterface
|
||||
*/
|
||||
public function getLoop();
|
||||
|
||||
/**
|
||||
* Get the interval after which this timer will execute, in seconds
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getInterval();
|
||||
|
||||
/**
|
||||
* Get the callback that will be executed when this timer elapses
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public function getCallback();
|
||||
|
||||
/**
|
||||
* Set arbitrary data associated with timer
|
||||
*
|
||||
* @param mixed $data
|
||||
*/
|
||||
public function setData($data);
|
||||
|
||||
/**
|
||||
* Get arbitrary data associated with timer
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData();
|
||||
|
||||
/**
|
||||
* Determine whether the time is periodic
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isPeriodic();
|
||||
|
||||
/**
|
||||
* Determine whether the time is active
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isActive();
|
||||
|
||||
/**
|
||||
* Cancel this timer
|
||||
*/
|
||||
public function cancel();
|
||||
}
|
100
libs/React/EventLoop/Timer/Timers.php
Normal file
100
libs/React/EventLoop/Timer/Timers.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace React\EventLoop\Timer;
|
||||
|
||||
use SplObjectStorage;
|
||||
use SplPriorityQueue;
|
||||
|
||||
class Timers
|
||||
{
|
||||
private $time;
|
||||
private $timers;
|
||||
private $scheduler;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->timers = new SplObjectStorage();
|
||||
$this->scheduler = new SplPriorityQueue();
|
||||
}
|
||||
|
||||
public function updateTime()
|
||||
{
|
||||
return $this->time = microtime(true);
|
||||
}
|
||||
|
||||
public function getTime()
|
||||
{
|
||||
return $this->time ?: $this->updateTime();
|
||||
}
|
||||
|
||||
public function add(TimerInterface $timer)
|
||||
{
|
||||
$interval = $timer->getInterval();
|
||||
$scheduledAt = $interval + $this->getTime();
|
||||
|
||||
$this->timers->attach($timer, $scheduledAt);
|
||||
$this->scheduler->insert($timer, -$scheduledAt);
|
||||
}
|
||||
|
||||
public function contains(TimerInterface $timer)
|
||||
{
|
||||
return $this->timers->contains($timer);
|
||||
}
|
||||
|
||||
public function cancel(TimerInterface $timer)
|
||||
{
|
||||
$this->timers->detach($timer);
|
||||
}
|
||||
|
||||
public function getFirst()
|
||||
{
|
||||
while ($this->scheduler->count()) {
|
||||
$timer = $this->scheduler->top();
|
||||
|
||||
if ($this->timers->contains($timer)) {
|
||||
return $this->timers[$timer];
|
||||
}
|
||||
|
||||
$this->scheduler->extract();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function isEmpty()
|
||||
{
|
||||
return count($this->timers) === 0;
|
||||
}
|
||||
|
||||
public function tick()
|
||||
{
|
||||
$time = $this->updateTime();
|
||||
$timers = $this->timers;
|
||||
$scheduler = $this->scheduler;
|
||||
|
||||
while (!$scheduler->isEmpty()) {
|
||||
$timer = $scheduler->top();
|
||||
|
||||
if (!isset($timers[$timer])) {
|
||||
$scheduler->extract();
|
||||
$timers->detach($timer);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($timers[$timer] >= $time) {
|
||||
break;
|
||||
}
|
||||
|
||||
$scheduler->extract();
|
||||
call_user_func($timer->getCallback(), $timer);
|
||||
|
||||
if ($timer->isPeriodic() && isset($timers[$timer])) {
|
||||
$timers[$timer] = $scheduledAt = $timer->getInterval() + $time;
|
||||
$scheduler->insert($timer, -$scheduledAt);
|
||||
} else {
|
||||
$timers->detach($timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user