Skip to content
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

Fix bug #12 #21

Merged
merged 2 commits into from
Oct 17, 2024
Merged
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
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 @@

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;

Check warning on line 92 in src/Events/SwooleEvent.php

View check run for this annotation

Codecov / codecov/patch

src/Events/SwooleEvent.php#L92

Added line #L92 was not covered by tests
}

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)

Check warning on line 96 in src/Events/SwooleEvent.php

View check run for this annotation

Codecov / codecov/patch

src/Events/SwooleEvent.php#L96

Added line #L96 was not covered by tests
: Event::add($fd, $func, null, SWOOLE_EVENT_READ);
case EventInterface::EV_WRITE:
if (!\is_resource($fd)) {
return false;

Check warning on line 100 in src/Events/SwooleEvent.php

View check run for this annotation

Codecov / codecov/patch

src/Events/SwooleEvent.php#L100

Added line #L100 was not covered by tests
}

return false;
return Event::isset($fd, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE)
? Event::set($fd, null, $func, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE)

Check warning on line 104 in src/Events/SwooleEvent.php

View check run for this annotation

Codecov / codecov/patch

src/Events/SwooleEvent.php#L104

Added line #L104 was not covered by tests
: Event::add($fd, null, $func, SWOOLE_EVENT_WRITE);
default:
return null;
}
Expand All @@ -134,7 +112,7 @@
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 @@
}

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 @@
}

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;

Check warning on line 138 in src/Events/SwooleEvent.php

View check run for this annotation

Codecov / codecov/patch

src/Events/SwooleEvent.php#L138

Added line #L138 was not covered by tests
}

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);

Check warning on line 144 in src/Events/SwooleEvent.php

View check run for this annotation

Codecov / codecov/patch

src/Events/SwooleEvent.php#L144

Added line #L144 was not covered by tests
case EventInterface::EV_WRITE:
if (!\is_resource($fd)) {
return false;

Check warning on line 147 in src/Events/SwooleEvent.php

View check run for this annotation

Codecov / codecov/patch

src/Events/SwooleEvent.php#L147

Added line #L147 was not covered by tests
}

return false;
if (!Event::isset($fd, SWOOLE_EVENT_READ)) {
return Event::del($fd);
}
return Event::set($fd, null, null, SWOOLE_EVENT_READ);

Check warning on line 153 in src/Events/SwooleEvent.php

View check run for this annotation

Codecov / codecov/patch

src/Events/SwooleEvent.php#L153

Added line #L153 was not covered by tests
default:
return null;
}
Expand Down Expand Up @@ -211,7 +179,6 @@
}
// 退出event loop
Event::exit();
$this->_reads = $this->_writes = [];
}

/** @inheritdoc */
Expand Down