Created
May 5, 2012 16:53
-
-
Save m4rw3r/2604007 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
namespace React\EventLoop; | |
use libev\EventLoop; | |
use libev\IOEvent; | |
class LibevLoop | |
{ | |
protected $loop = null; | |
protected $read_events = array(); | |
protected $write_events = array(); | |
public function __construct() | |
{ | |
$this->loop = new EventLoop(); | |
} | |
public function addReadStream($stream, $listener) | |
{ | |
$event = new libev\IOEvent($listener, $stream, IOEvent::READ); | |
$this->loop->add($event); | |
$this->read_events[(int) $stream] = $event; | |
} | |
public function addWriteStream($stream, $listener) | |
{ | |
$event = new libev\IOEvent($listener, $stream, IOEvent::WRITE); | |
$this->loop->add($event); | |
$this->write_events[(int) $stream] = $event; | |
} | |
public function removeReadStream($stream) | |
{ | |
if(isset($this->read_events[(int) $stream])) | |
{ | |
$this->read_events[(int) $stream]->stop(); | |
unset($this->read_events[(int) $stream]); | |
} | |
} | |
public function removeWriteStream($stream) | |
{ | |
if(isset($this->write_events[(int) $stream])) | |
{ | |
$this->write_events[(int) $stream]->stop(); | |
unset($this->write_events[(int) $stream]); | |
} | |
} | |
public function tick() | |
{ | |
$this->loop->run(EventLoop::RUN_NOWAIT); | |
} | |
public function run() | |
{ | |
$this->loop->run(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment