Skip to content

Commit

Permalink
Merge pull request #21 from NathanFreeman/bug_12
Browse files Browse the repository at this point in the history
Fix bug #12
  • Loading branch information
chaz6chez authored Oct 17, 2024
2 parents 0d4c049 + f4685c0 commit 28187ae
Showing 1 changed file with 28 additions and 61 deletions.
89 changes: 28 additions & 61 deletions src/Events/SwooleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

class SwooleEvent implements EventInterface
{
/** @var int[] All listeners for read event. */
protected array $_reads = [];

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

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

Expand Down Expand Up @@ -94,37 +88,21 @@ public function add($fd, $flag, $func, $args = [])

return $timerId;
case EventInterface::EV_READ:
if (\is_resource($fd)) {
if (
($this->_reads[$key = (int) $fd] ?? null) or
Event::isset($fd, SWOOLE_EVENT_READ)
) {
$this->del($fd, EventInterface::EV_READ);
}
if ($res = Event::add($fd, $func, null, SWOOLE_EVENT_READ)) {
$this->_reads[$key] = 1;
}

return (bool) $res;
if (!\is_resource($fd)) {
return false;
}

return false;
case self::EV_WRITE:
if (\is_resource($fd)) {
if (
($this->_writes[$key = (int) $fd] ?? null) or
Event::isset($fd, SWOOLE_EVENT_WRITE)
) {
$this->del($fd, EventInterface::EV_WRITE);
}
if ($res = Event::add($fd, null, $func, SWOOLE_EVENT_WRITE)) {
$this->_writes[$key] = 1;
}

return (bool) $res;
return Event::isset($fd, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE)
? Event::set($fd, $func, null, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE)
: Event::add($fd, $func, null, SWOOLE_EVENT_READ);
case EventInterface::EV_WRITE:
if (!\is_resource($fd)) {
return false;
}

return false;
return Event::isset($fd, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE)
? Event::set($fd, null, $func, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE)
: Event::add($fd, null, $func, SWOOLE_EVENT_WRITE);
default:
return null;
}
Expand All @@ -134,7 +112,7 @@ public function add($fd, $flag, $func, $args = [])
public function del($fd, $flag)
{
switch ($flag) {
case self::EV_SIGNAL:
case EventInterface::EV_SIGNAL:
if ($this->_signals[$fd] ?? null) {
if (Process::signal($fd, null)) {
unset($this->_signals[$fd]);
Expand All @@ -144,8 +122,8 @@ public function del($fd, $flag)
}

return false;
case self::EV_TIMER:
case self::EV_TIMER_ONCE:
case EventInterface::EV_TIMER:
case EventInterface::EV_TIMER_ONCE:
if ($id = $this->_timer[$fd] ?? null) {
if ($id === true or Timer::clear($id)) {
unset($this->_timer[$fd]);
Expand All @@ -155,34 +133,24 @@ public function del($fd, $flag)
}

return false;
case self::EV_READ:
if (\is_resource($fd)) {
$key = (int) $fd;
if (Event::isset($fd, SWOOLE_EVENT_READ)) {
if (Event::del($fd)) {
return false;
}
}
unset($this->_reads[$key]);

return true;
case EventInterface::EV_READ:
if (!\is_resource($fd)) {
return false;
}

return false;
case self::EV_WRITE:
if (\is_resource($fd)) {
$key = (int) $fd;
if (Event::isset($fd, SWOOLE_EVENT_WRITE)) {
if (Event::del($fd)) {
return false;
}
}
unset($this->_writes[$key]);

return true;
if (!Event::isset($fd, SWOOLE_EVENT_WRITE)) {
return Event::del($fd);
}
return Event::set($fd, null, null, SWOOLE_EVENT_WRITE);
case EventInterface::EV_WRITE:
if (!\is_resource($fd)) {
return false;
}

return false;
if (!Event::isset($fd, SWOOLE_EVENT_READ)) {
return Event::del($fd);
}
return Event::set($fd, null, null, SWOOLE_EVENT_READ);
default:
return null;
}
Expand Down Expand Up @@ -211,7 +179,6 @@ public function destroy()
}
// 退出event loop
Event::exit();
$this->_reads = $this->_writes = [];
}

/** @inheritdoc */
Expand Down

0 comments on commit 28187ae

Please sign in to comment.