Skip to content

Instantly share code, notes, and snippets.

@m4rw3r
Created May 5, 2012 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m4rw3r/2604007 to your computer and use it in GitHub Desktop.
Save m4rw3r/2604007 to your computer and use it in GitHub Desktop.
<?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