Skip to content

Commit

Permalink
Fix swoole
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Nov 7, 2024
1 parent e0ae67a commit bb9e4b0
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions Events/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Workerman\Worker;
use Swoole\Event;
use Swoole\Timer;
use Swoole\Coroutine;

class Swoole implements EventInterface
{
Expand All @@ -33,6 +34,10 @@ class Swoole implements EventInterface

protected $_hasSignal = false;

protected $_readEvents = array();

protected $_writeEvents = array();

/**
*
* {@inheritdoc}
Expand Down Expand Up @@ -61,7 +66,7 @@ function () {
$mapId = $this->mapId++;
$t = (int)($fd * 1000);
if ($t < 1) {
$t = 1;
$t = 1;
}
$timer_id = Timer::$method($t,
function ($timer_id = null) use ($func, $args, $mapId) {
Expand Down Expand Up @@ -92,9 +97,14 @@ function ($timer_id = null) use ($func, $args, $mapId) {
case self::EV_READ:
case self::EV_WRITE:
$fd_key = (int) $fd;
if (! isset($this->_fd[$fd_key])) {
if ($flag === self::EV_READ) {
$this->_readEvents[$fd_key] = $func;
} else {
$this->_writeEvents[$fd_key] = $func;
}
if (!isset($this->_fd[$fd_key])) {
if ($flag === self::EV_READ) {
$res = Event::add($fd, $func, null, SWOOLE_EVENT_READ);
$res = Event::add($fd, [$this, 'callRead'], null, SWOOLE_EVENT_READ);
$fd_type = SWOOLE_EVENT_READ;
} else {
$res = Event::add($fd, null, $func, SWOOLE_EVENT_WRITE);
Expand Down Expand Up @@ -124,6 +134,42 @@ function ($timer_id = null) use ($func, $args, $mapId) {
}
}

/**
* @param $fd
* @return void
*/
protected function callRead($stream)
{
$fd = (int) $stream;
if (isset($this->_readEvents[$fd])) {
try {
\call_user_func($this->_readEvents[$fd], $stream);
} catch (\Exception $e) {
Worker::stopAll(250, $e);
} catch (\Error $e) {
Worker::stopAll(250, $e);
}
}
}

/**
* @param $fd
* @return void
*/
protected function callWrite($stream)
{
$fd = (int) $stream;
if (isset($this->_writeEvents[$fd])) {
try {
\call_user_func($this->_writeEvents[$fd], $stream);
} catch (\Exception $e) {
Worker::stopAll(250, $e);
} catch (\Error $e) {
Worker::stopAll(250, $e);
}
}
}

/**
*
* {@inheritdoc}
Expand Down Expand Up @@ -153,6 +199,11 @@ public function del($fd, $flag)
case self::EV_READ:
case self::EV_WRITE:
$fd_key = (int) $fd;
if ($flag === self::EV_READ) {
unset($this->_readEvents[$fd_key]);
} elseif ($flag === self::EV_WRITE) {
unset($this->_writeEvents[$fd_key]);
}
if (isset($this->_fd[$fd_key])) {
$fd_val = $this->_fd[$fd_key];
if ($flag === self::EV_READ) {
Expand Down Expand Up @@ -213,8 +264,10 @@ public function loop()
*/
public function destroy()
{
foreach (Coroutine::listCoroutines() as $coroutine) {
Coroutine::cancel($coroutine);
}
Event::exit();
posix_kill(posix_getpid(), SIGINT);
}

/**
Expand Down

0 comments on commit bb9e4b0

Please sign in to comment.