Skip to content

Implement Eventloop based on pecl/ev #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ before_script:
cd libevent-0.0.5 && phpize && ./configure && make && sudo make install;
echo "extension=libevent.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`;
fi"
- sh -c " if [ \"\$(php --re ev | grep 'does not exist')\" != '' ]; then
git clone https://bitbucket.org/osmanov/pecl-ev.git;
cd pecl-ev;
phpize && ./configure --enable-ev && make && make install;
echo "extension=ev.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`;
fi"
- composer self-update
- composer install --dev --prefer-source

Expand Down
153 changes: 153 additions & 0 deletions src/React/EventLoop/EvLoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace React\EventLoop;

use SplObjectStorage;
use React\EventLoop\Timer\Timer;
use React\EventLoop\Timer\TimerInterface;

class EvLoop implements LoopInterface
{
private $loop;
private $timers;
private $readEvents = array();
private $writeEvents = array();

public function __construct()
{
$this->loop = new \EvLoop();
$this->timers = new SplObjectStorage();
}

public function addReadStream($stream, $listener)
{
$this->addStream($stream, $listener, \Ev::READ);
}

public function addWriteStream($stream, $listener)
{
$this->addStream($stream, $listener, \Ev::WRITE);
}

public function removeReadStream($stream)
{
$this->readEvents[(int)$stream]->stop();
unset($this->readEvents[(int)$stream]);
}

public function removeWriteStream($stream)
{
$this->writeEvents[(int)$stream]->stop();
unset($this->writeEvents[(int)$stream]);
}

public function removeStream($stream)
{
if (isset($this->readEvents[(int)$stream])) {
$this->removeReadStream($stream);
}

if (isset($this->writeEvents[(int)$stream])) {
$this->removeWriteStream($stream);
}
}

private function addStream($stream, $listener, $flags)
{
$listener = $this->wrapStreamListener($stream, $listener, $flags);
$event = $this->loop->io($stream, $flags, $listener);

if (($flags & \Ev::READ) === $flags) {
$this->readEvents[(int)$stream] = $event;
} elseif (($flags & \Ev::WRITE) === $flags) {
$this->writeEvents[(int)$stream] = $event;
}
}

private function wrapStreamListener($stream, $listener, $flags)
{
if (($flags & \Ev::READ) === $flags) {
$removeCallback = array($this, 'removeReadStream');
} elseif (($flags & \Ev::WRITE) === $flags) {
$removeCallback = array($this, 'removeWriteStream');
}

return function ($event) use ($stream, $listener, $removeCallback) {
if (feof($stream)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement should be removed. The stream handler should be removing this from the event loop, not the event loop itself. This statement currently removes the stream from the loop without notifying the listeners.

call_user_func($removeCallback, $stream);

return;
}

call_user_func($listener, $stream);
};
}

public function addTimer($interval, $callback)
{
$timer = new Timer($this, $interval, $callback, false);
$this->setupTimer($timer);

return $timer;
}

public function addPeriodicTimer($interval, $callback)
{
$timer = new Timer($this, $interval, $callback, true);
$this->setupTimer($timer);

return $timer;
}

public function cancelTimer(TimerInterface $timer)
{
if (isset($this->timers[$timer])) {
$this->timers[$timer]->stop();
$this->timers->detach($timer);
}
}

private function setupTimer(TimerInterface $timer)
{
$dummyCallback = function () {};
$interval = $timer->getInterval();

if ($timer->isPeriodic()) {
$libevTimer = $this->loop->timer($interval, $interval, $dummyCallback);
} else {
$libevTimer = $this->loop->timer($interval, $dummyCallback);
}

$libevTimer->setCallback(function () use ($timer) {
call_user_func($timer->getCallback(), $timer);

if (!$timer->isPeriodic()) {
$timer->cancel();
}
});

$this->timers->attach($timer, $libevTimer);

return $timer;
}

public function isTimerActive(TimerInterface $timer)
{
return $this->timers->contains($timer);
}

public function tick()
{
$this->loop->run(\Ev::RUN_ONCE);
}

public function run()
{
$this->loop->run();
}

public function stop()
{
$this->loop->stop();
}
}
22 changes: 22 additions & 0 deletions tests/React/Tests/EventLoop/EvLoopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace React\Tests\EventLoop;

use React\EventLoop\EvLoop;

class EvLoopTest extends AbstractLoopTest
{
public function createLoop()
{
if (!class_exists('\EvLoop')) {
$this->markTestSkipped('ev tests skipped because pecl/ev is not installed.');
}

return new EvLoop();
}

public function testEvConstructor()
{
$loop = new EvLoop();
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy