-
-
Notifications
You must be signed in to change notification settings - Fork 726
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { | ||
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.