-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Non-blocking IO (Generators approach)
- Loading branch information
1 parent
7013693
commit 46c8809
Showing
5 changed files
with
477 additions
and
84 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
<?php namespace Ollyxar\WebSockets; | ||
|
||
use Generator; | ||
use SplQueue; | ||
|
||
/** | ||
* Class Dispatcher | ||
* @package Ollyxar\WebSockets | ||
*/ | ||
final class Dispatcher | ||
{ | ||
/** | ||
* @var SplQueue | ||
*/ | ||
private $queue; | ||
|
||
/** | ||
* Sockets ready to read | ||
* | ||
* @var array | ||
*/ | ||
private $read = []; | ||
|
||
/** | ||
* Sockets ready to write | ||
* | ||
* @var array | ||
*/ | ||
private $write = []; | ||
|
||
/** | ||
* Dispatcher constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->queue = new SplQueue(); | ||
} | ||
|
||
/** | ||
* Poll connections | ||
* | ||
* @param $timeout | ||
* @return void | ||
*/ | ||
private function poll($timeout): void | ||
{ | ||
$read = $write = []; | ||
|
||
foreach ($this->read as [$socket]) { | ||
$read[] = $socket; | ||
} | ||
|
||
foreach ($this->write as [$socket]) { | ||
$write[] = $socket; | ||
} | ||
|
||
if (!@stream_select($read, $write, $except, $timeout)) { | ||
return; | ||
} | ||
|
||
foreach ($read as $socket) { | ||
list(, $jobs) = $this->read[(int)$socket]; | ||
unset($this->read[(int)$socket]); | ||
|
||
foreach ($jobs as $job) { | ||
$this->enqueue($job); | ||
} | ||
} | ||
|
||
foreach ($write as $socket) { | ||
list(, $jobs) = $this->write[(int)$socket]; | ||
unset($this->write[(int)$socket]); | ||
|
||
foreach ($jobs as $job) { | ||
$this->enqueue($job); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return Generator | ||
*/ | ||
private function pollProcess(): Generator | ||
{ | ||
while (true) { | ||
if ($this->queue->isEmpty()) { | ||
$this->poll(null); | ||
} else { | ||
$this->poll(0); | ||
} | ||
yield; | ||
} | ||
} | ||
|
||
/** | ||
* @param Job $job | ||
* @return void | ||
*/ | ||
public function enqueue(Job $job): void | ||
{ | ||
$this->queue->enqueue($job); | ||
} | ||
|
||
/** | ||
* @param $socket | ||
* @param Job $job | ||
* @return void | ||
*/ | ||
public function appendRead($socket, Job $job): void | ||
{ | ||
if (isset($this->read[(int)$socket])) { | ||
$this->read[(int)$socket][1][] = $job; | ||
} else { | ||
$this->read[(int)$socket] = [$socket, [$job]]; | ||
} | ||
} | ||
|
||
/** | ||
* @param $socket | ||
* @param Job $job | ||
* @return void | ||
*/ | ||
public function appendWrite($socket, Job $job): void | ||
{ | ||
if (isset($this->write[(int)$socket])) { | ||
$this->write[(int)$socket][1][] = $job; | ||
} else { | ||
$this->write[(int)$socket] = [$socket, [$job]]; | ||
} | ||
} | ||
|
||
/** | ||
* @param Generator $process | ||
* @return Dispatcher | ||
*/ | ||
public function add(Generator $process): self | ||
{ | ||
$this->enqueue(new Job($process)); | ||
|
||
return $this; | ||
} | ||
|
||
public static function make(Generator $process) { | ||
return new SysCall( | ||
function(Job $job, Dispatcher $dispatcher) use ($process) { | ||
$job->value($dispatcher->add($process)); | ||
$dispatcher->enqueue($job); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param $socket | ||
* @return SysCall | ||
*/ | ||
public static function listenRead($socket) | ||
{ | ||
return new SysCall( | ||
function(Job $job, Dispatcher $dispatcher) use ($socket) { | ||
$dispatcher->appendRead($socket, $job); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param $socket | ||
* @return SysCall | ||
*/ | ||
public static function listenWrite($socket) | ||
{ | ||
return new SysCall( | ||
function(Job $job, Dispatcher $dispatcher) use ($socket) { | ||
$dispatcher->appendWrite($socket, $job); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function dispatch(): void | ||
{ | ||
$this->add($this->pollProcess()); | ||
|
||
while (!$this->queue->isEmpty()) { | ||
$job = $this->queue->dequeue(); | ||
$result = $job->run(); | ||
|
||
if ($result instanceof SysCall) { | ||
$result($job, $this); | ||
continue; | ||
} | ||
|
||
if (!$job->finished()) { | ||
$this->enqueue($job); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php namespace Ollyxar\WebSockets; | ||
|
||
use Generator; | ||
|
||
/** | ||
* Class Job | ||
* @package Ollyxar\WebSockets | ||
*/ | ||
final class Job | ||
{ | ||
/** | ||
* @var Generator | ||
*/ | ||
protected $process; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
protected $sendValue = null; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $init = true; | ||
|
||
/** | ||
* Job constructor. | ||
* @param Generator $process | ||
*/ | ||
public function __construct(Generator $process) | ||
{ | ||
$this->process = $process; | ||
} | ||
|
||
/** | ||
* @param mixed $sendValue | ||
*/ | ||
public function value($sendValue) | ||
{ | ||
$this->sendValue = $sendValue; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function run() | ||
{ | ||
if ($this->init) { | ||
$this->init = false; | ||
|
||
return $this->process->current(); | ||
} else { | ||
$result = $this->process->send($this->sendValue); | ||
$this->sendValue = null; | ||
|
||
return $result; | ||
} | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function finished(): bool | ||
{ | ||
return !$this->process->valid(); | ||
} | ||
} |
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
Oops, something went wrong.