From ded275df06fe839f16fcb48cc4c7b3d2b7ebe9ba Mon Sep 17 00:00:00 2001 From: MARiA so cute <33935209+NathanFreeman@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:44:26 +0800 Subject: [PATCH 1/2] Fix bug #12 --- src/Events/SwooleEvent.php | 82 +++++++++++++------------------------- 1 file changed, 28 insertions(+), 54 deletions(-) diff --git a/src/Events/SwooleEvent.php b/src/Events/SwooleEvent.php index 7ea93f1..048c034 100644 --- a/src/Events/SwooleEvent.php +++ b/src/Events/SwooleEvent.php @@ -94,37 +94,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; } @@ -134,7 +118,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]); @@ -144,8 +128,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]); @@ -155,34 +139,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; } From f4685c0cd02b4643994493bba51a060fd9c44dc1 Mon Sep 17 00:00:00 2001 From: MARiA so cute <33935209+NathanFreeman@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:45:02 +0800 Subject: [PATCH 2/2] Fix bug #12 --- src/Events/SwooleEvent.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/Events/SwooleEvent.php b/src/Events/SwooleEvent.php index 048c034..247091c 100644 --- a/src/Events/SwooleEvent.php +++ b/src/Events/SwooleEvent.php @@ -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 = []; @@ -185,7 +179,6 @@ public function destroy() } // 退出event loop Event::exit(); - $this->_reads = $this->_writes = []; } /** @inheritdoc */