Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed May 26, 2022
1 parent 7f22fd7 commit 8a329f6
Show file tree
Hide file tree
Showing 19 changed files with 1,754 additions and 155 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/.idea
/.vscode
/vendor
composer.lock
start.php
composer.lock
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ composer require workbunny/event-loop
```

- 定时器

```php
$loop = \EventLoop\Factory::create(\EventLoop\Drivers\NativeLoop::class);
$loop = \WorkBunny\EventLoop\Loop::create(\WorkBunny\EventLoop\Drivers\NativeLoop::class);
$id = $loop->addTimer(0.0, 1.0, function (){
# 业务
});
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
},
"require-dev": {
"paulxu-cn/event-ide-helper": "dev-master",
"symfony/var-dumper": "^5.4"
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^5.0"
},
"suggest": {
"ext-event": "For EventLoop. ",
Expand All @@ -38,12 +39,12 @@
},
"autoload": {
"psr-4": {
"EventLoop\\": "src"
"WorkBunny\\EventLoop\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Test\\": "test"
"WorkBunny\\Test\\": "test"
}
}
}
20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- PHPUnit configuration file with new format for PHPUnit 9.3+ -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResult="false"
colors="true"
convertDeprecationsToExceptions="true">
<testsuites>
<testsuite name="workbunny test suite">
<directory>./test/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory>./src/</directory>
</include>
</coverage>
</phpunit>
85 changes: 85 additions & 0 deletions src/Drivers/AbstractLoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);

namespace WorkBunny\EventLoop\Drivers;

use WorkBunny\EventLoop\Exception\LoopException;
use WorkBunny\EventLoop\Storage;

abstract class AbstractLoop implements LoopInterface
{
/** @var resource[] */
protected array $_readFds = [];

/** @var resource[] */
protected array $_writeFds = [];

/** @var array All listeners for read event. */
protected array $_reads = [];

/** @var array All listeners for write event. */
protected array $_writes = [];

/** @var array Event listeners of signal. */
protected array $_signals = [];

/** @var Storage 定时器容器 */
protected Storage $_storage;

/**
* 初始化
* @throws LoopException
*/
public function __construct()
{
$this->_storage = new Storage();
}

/**
* @return resource[]
*/
public function getReadFds(): array
{
return $this->_readFds;
}

/**
* @return resource[]
*/
public function getWriteFds(): array
{
return $this->_writeFds;
}

/**
* @return array
*/
public function getReads(): array
{
return $this->_reads;
}

/**
* @return array
*/
public function getWrites(): array
{
return $this->_writes;
}

/**
* @return array
*/
public function getSignals(): array
{
return $this->_signals;
}

/**
* @return Storage
*/
public function getStorage(): Storage
{
return $this->_storage;
}
}
110 changes: 67 additions & 43 deletions src/Drivers/EvLoop.php
Original file line number Diff line number Diff line change
@@ -1,95 +1,119 @@
<?php
declare(strict_types=1);

namespace EventLoop\Drivers;
namespace WorkBunny\EventLoop\Drivers;

use EventLoop\Exception\LoopException;
use EventLoop\Storage;
use Ev;
use EvIo;
use EvSignal;
use EvTimer;
use WorkBunny\EventLoop\Exception\LoopException;
use EvLoop as BaseEvLoop;
use Closure;

class EvLoop implements LoopInterface
class EvLoop extends AbstractLoop
{
/** @var array All listeners for read event. */
protected array $_reads = [];

/** @var array All listeners for write event. */
protected array $_writes = [];

/** @var array Event listeners of signal. */
protected array $_signals = [];

/** @var BaseEvLoop loop */
protected BaseEvLoop $_loop;

/** @var Storage 计数器 */
protected Storage $_storage;

/**
* Ev constructor.
* @throws LoopException
*/
/** @inheritDoc */
public function __construct()
{
if(!extension_loaded('ev')){
throw new LoopException('ext-ev not support');
}
$this->_storage = new Storage();

parent::__construct();

$this->_loop = new BaseEvLoop();
}

/** @inheritDoc */
public function addReadStream($stream, Closure $handler): void
{
if(is_resource($stream)){
$event = new \EvIo($stream,\Ev::READ, $handler);
$this->_reads[(int)$stream] = $event;
if(is_resource($stream) and !isset($this->_reads[$key = (int)$stream])){
$event = $this->_loop->io($stream, Ev::READ, $handler);
$this->_reads[$key] = $event;
$this->_readFds[spl_object_hash($event)] = $stream;
}
}

/** @inheritDoc */
/**
* @param resource|EvIo $stream
* @return void
*/
public function delReadStream($stream): void
{
if(is_resource($stream) and isset($this->_reads[(int)$stream])){
/** @var \EvIo $event */
$event = $this->_reads[(int)$stream];
if(is_resource($stream) and isset($this->_reads[$key = (int)$stream])){
/** @var EvIo $event */
$event = $this->_reads[$key];
$event->stop();
unset($this->_reads[(int)$stream]);
unset(
$this->_reads[$key],
$this->_readFds[spl_object_hash($event)]
);
}

if($stream instanceof EvIo and isset($this->_readFds[spl_object_hash($stream)])){
$stream->stop();
$key = (int)($this->_readFds[spl_object_hash($stream)]);
unset(
$this->_reads[$key],
$this->_readFds[spl_object_hash($stream)]
);
}
}

/** @inheritDoc */
public function addWriteStream($stream, Closure $handler): void
{
if(is_resource($stream)){
$event = new \EvIo($stream, \Ev::WRITE, $handler);
$this->_writes[(int)$stream] = $event;
if(is_resource($stream) and !isset($this->_writes[$key = (int)$stream])){
$event = $this->_loop->io($stream, Ev::WRITE, $handler);
$this->_writes[$key] = $event;
$this->_writeFds[spl_object_hash($event)] = $stream;
}
}

/** @inheritDoc */
/**
* @param EvIo|resource $stream
* @return void
*/
public function delWriteStream($stream): void
{
if(is_resource($stream) and isset($this->_writes[(int)$stream])){
/** @var \EvIo $event */
$event = $this->_writes[(int)$stream];
if(is_resource($stream) and isset($this->_writes[$key = (int)$stream])){
/** @var EvIo $event */
$event = $this->_writes[$key];
$event->stop();
unset($this->_writes[(int)$stream]);
unset(
$this->_writes[$key],
$this->_writeFds[spl_object_hash($event)]
);
}

if($stream instanceof EvIo and isset($this->_writeFds[spl_object_hash($stream)])){
$stream->stop();
$key = (int)($this->_writeFds[spl_object_hash($stream)]);
unset(
$this->_writes[$key],
$this->_writeFds[spl_object_hash($stream)]
);
}
}

/** @inheritDoc */
public function addSignal(int $signal, Closure $handler): void
{
$event = new \EvSignal($signal, $handler);
$this->_signals[$signal] = $event;
if(!isset($this->_signals[$signal])){
$event = $this->_loop->signal($signal, $handler);
$this->_signals[$signal] = $event;
}
}

/** @inheritDoc */
public function delSignal(int $signal, Closure $handler): void
public function delSignal(int $signal): void
{
if(isset($this->_signals[$signal])){
/** @var \EvSignal $event */
/** @var EvSignal $event */
$event = $this->_signals[$signal];
$event->stop();
unset($this->_signals[$signal]);
Expand All @@ -99,14 +123,14 @@ public function delSignal(int $signal, Closure $handler): void
/** @inheritDoc */
public function addTimer(float $delay, float $repeat, Closure $callback): string
{
$event = new \EvTimer($delay, $repeat, $callback);
$event = $this->_loop->timer($delay, $repeat, $callback);
return $this->_storage->add(spl_object_hash($event), $event);
}

/** @inheritDoc */
public function delTimer(string $timerId): void
{
/** @var \EvTimer $event */
/** @var EvTimer $event */
if($event = $this->_storage->get($timerId)){
$event->stop();
$this->_storage->del($timerId);
Expand Down
Loading

0 comments on commit 8a329f6

Please sign in to comment.