Skip to content

WIP: [EventLoop] Add SocketSelectLoop #191

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
181 changes: 181 additions & 0 deletions src/React/EventLoop/AbstractSelectLoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

namespace React\EventLoop;

use React\EventLoop\Timer\Timer;
use React\EventLoop\Timer\TimerInterface;
use React\EventLoop\Timer\Timers;

abstract class AbstractSelectLoop implements LoopInterface
{
const QUANTUM_INTERVAL = 1000000;

private $timers;
private $running = false;
private $readStreams = array();
private $readListeners = array();
private $writeStreams = array();
private $writeListeners = array();

public function __construct()
{
$this->timers = new Timers();
}

public function addReadStream($stream, $listener)
{
$id = (int) $stream;

if (!isset($this->readStreams[$id])) {
$this->readStreams[$id] = $stream;
$this->readListeners[$id] = $listener;
}
}

public function addWriteStream($stream, $listener)
{
$id = (int) $stream;

if (!isset($this->writeStreams[$id])) {
$this->writeStreams[$id] = $stream;
$this->writeListeners[$id] = $listener;
}
}

public function removeReadStream($stream)
{
$id = (int) $stream;

unset(
$this->readStreams[$id],
$this->readListeners[$id]
);
}

public function removeWriteStream($stream)
{
$id = (int) $stream;

unset(
$this->writeStreams[$id],
$this->writeListeners[$id]
);
}

public function removeStream($stream)
{
$this->removeReadStream($stream);
$this->removeWriteStream($stream);
}

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

return $timer;
}

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

return $timer;
}

public function cancelTimer(TimerInterface $timer)
{
$this->timers->cancel($timer);
}

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

protected function getNextEventTimeInMicroSeconds()
{
$nextEvent = $this->timers->getFirst();

if (null === $nextEvent) {
return self::QUANTUM_INTERVAL;
}

$currentTime = microtime(true);
if ($nextEvent > $currentTime) {
return ($nextEvent - $currentTime) * 1000000;
}

return 0;
}

protected function sleepOnPendingTimers()
{
if ($this->timers->isEmpty()) {
$this->running = false;
} else {
// We use usleep() instead of stream_select() to emulate timeouts
// since the latter fails when there are no streams registered for
// read / write events. Blame PHP for us needing this hack.
usleep($this->getNextEventTimeInMicroSeconds());
}
}

protected function runStreamSelect()
{
$read = $this->readStreams ?: null;
$write = $this->writeStreams ?: null;
$except = null;

if (!$read && !$write) {
$this->sleepOnPendingTimers();

return;
}

if ($this->select($read, $write, $except, $this->getNextEventTimeInMicroSeconds()) > 0) {
if ($read) {
foreach ($read as $stream) {
$listener = $this->readListeners[(int) $stream];
call_user_func($listener, $stream, $this);
}
}

if ($write) {
foreach ($write as $stream) {
if (!isset($this->writeListeners[(int) $stream])) {
continue;
}

$listener = $this->writeListeners[(int) $stream];
call_user_func($listener, $stream, $this);
}
}
}
}

public function tick()
{
$this->timers->tick();
$this->runStreamSelect();

return $this->running;
}

public function run()
{
$this->running = true;

while ($this->tick()) {
// NOOP
}
}

public function stop()
{
$this->running = false;
}

abstract protected function select(&$read, &$write, &$except, $utime);
}
30 changes: 30 additions & 0 deletions src/React/EventLoop/SocketSelectLoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace React\EventLoop;

use InvalidArgumentException;

class SocketSelectLoop extends AbstractSelectLoop
{

public function addReadStream($stream, $listener)
{
if (get_resource_type($stream) !== 'socket') {
throw new InvalidArgumentException('Socket loop only accepts resources of type "socket"');
}
return parent::addReadStream($stream, $listener);
}

public function addWriteStream($stream, $listener)
{
if (get_resource_type($stream) !== 'socket') {
throw new InvalidArgumentException('Socket loop only accepts resources of type "socket"');
}
return parent::addWriteStream($stream, $listener);
}

protected function select(&$read, &$write, &$except, $utime)
{
return socket_select($read, $write, $except, 0, $utime);
}
}
173 changes: 3 additions & 170 deletions src/React/EventLoop/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,178 +2,11 @@

namespace React\EventLoop;

use React\EventLoop\Timer\Timer;
use React\EventLoop\Timer\TimerInterface;
use React\EventLoop\Timer\Timers;

class StreamSelectLoop implements LoopInterface
class StreamSelectLoop extends AbstractSelectLoop
{
const QUANTUM_INTERVAL = 1000000;

private $timers;
private $running = false;
private $readStreams = array();
private $readListeners = array();
private $writeStreams = array();
private $writeListeners = array();

public function __construct()
{
$this->timers = new Timers();
}

public function addReadStream($stream, $listener)
{
$id = (int) $stream;

if (!isset($this->readStreams[$id])) {
$this->readStreams[$id] = $stream;
$this->readListeners[$id] = $listener;
}
}

public function addWriteStream($stream, $listener)
{
$id = (int) $stream;

if (!isset($this->writeStreams[$id])) {
$this->writeStreams[$id] = $stream;
$this->writeListeners[$id] = $listener;
}
}

public function removeReadStream($stream)
{
$id = (int) $stream;

unset(
$this->readStreams[$id],
$this->readListeners[$id]
);
}

public function removeWriteStream($stream)
{
$id = (int) $stream;

unset(
$this->writeStreams[$id],
$this->writeListeners[$id]
);
}

public function removeStream($stream)
{
$this->removeReadStream($stream);
$this->removeWriteStream($stream);
}

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

return $timer;
}

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

return $timer;
}

public function cancelTimer(TimerInterface $timer)
{
$this->timers->cancel($timer);
}

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

protected function getNextEventTimeInMicroSeconds()
{
$nextEvent = $this->timers->getFirst();

if (null === $nextEvent) {
return self::QUANTUM_INTERVAL;
}

$currentTime = microtime(true);
if ($nextEvent > $currentTime) {
return ($nextEvent - $currentTime) * 1000000;
}

return 0;
}

protected function sleepOnPendingTimers()
{
if ($this->timers->isEmpty()) {
$this->running = false;
} else {
// We use usleep() instead of stream_select() to emulate timeouts
// since the latter fails when there are no streams registered for
// read / write events. Blame PHP for us needing this hack.
usleep($this->getNextEventTimeInMicroSeconds());
}
}

protected function runStreamSelect()
{
$read = $this->readStreams ?: null;
$write = $this->writeStreams ?: null;
$except = null;

if (!$read && !$write) {
$this->sleepOnPendingTimers();

return;
}

if (stream_select($read, $write, $except, 0, $this->getNextEventTimeInMicroSeconds()) > 0) {
if ($read) {
foreach ($read as $stream) {
$listener = $this->readListeners[(int) $stream];
call_user_func($listener, $stream, $this);
}
}

if ($write) {
foreach ($write as $stream) {
if (!isset($this->writeListeners[(int) $stream])) {
continue;
}

$listener = $this->writeListeners[(int) $stream];
call_user_func($listener, $stream, $this);
}
}
}
}

public function tick()
{
$this->timers->tick();
$this->runStreamSelect();

return $this->running;
}

public function run()
{
$this->running = true;

while ($this->tick()) {
// NOOP
}
}

public function stop()
protected function select(&$read, &$write, &$except, $utime)
{
$this->running = false;
return stream_select($read, $write, $except, 0, $utime);
}
}
Loading
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