Commit 262801ce authored by jan.koester's avatar jan.koester
Browse files

test

parent 58712fcf
Loading
Loading
Loading
Loading
+25 −32
Changes for src/event/kqueue.cpp: 25 added lines, 32 removed lines.
Original line number Diff line number Diff line
@@ -132,29 +132,37 @@ namespace netplus {
    };

    // RAII Rearm guard - handles event re-enabling for EV_ONESHOT
    // Modelled after EpollArmGuard: always re-arms READ on scope exit
    // and auto-detects whether WRITE is needed by inspecting the
    // connection state (SendData, hasPendingWrite, hasBufferedData).
    // Call disarm() to suppress re-arming (e.g. before CloseEventHandler).
    class KqueueArmGuard {
    public:
        KqueueArmGuard(int fd, std::shared_ptr<con> c, KeventBatch* batch)
            : _fd(fd), _con(std::move(c)), _batch(batch), _events_to_rearm(0) {}
            : _fd(fd), _con(std::move(c)), _batch(batch) {}

        ~KqueueArmGuard() {
            if (!_batch || _events_to_rearm == 0) return;
            // Re-enable filters after EV_ONESHOT fires
            if (_events_to_rearm & KQUEUE_EV_READ)
            if (!_armed) return;
            if (!_batch || !_con || !_con->csock) return;

            // Always re-arm READ (matches EpollArmGuard EPOLLIN)
            _batch->add_change(_fd, EVFILT_READ, EV_ADD | EV_ONESHOT);
            if (_events_to_rearm & KQUEUE_EV_WRITE)

            // Auto-detect if WRITE is needed
            if (_con->SendData.pos < _con->SendData.size()
                || _con->csock->hasPendingWrite()
                || _con->csock->hasBufferedData()) {
                _batch->add_change(_fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
            }
        }

        void rearm_read() { _events_to_rearm |= KQUEUE_EV_READ; }
        void rearm_write() { _events_to_rearm |= KQUEUE_EV_WRITE; }
        void disarm() { _events_to_rearm = 0; }
        void disarm() { _armed = false; }

    private:
        bool _armed = true;
        int _fd;
        std::shared_ptr<con> _con;
        KeventBatch* _batch;
        int _events_to_rearm = 0;
    };

    // ------------------------------------------------------------
@@ -392,7 +400,7 @@ namespace netplus {
                                    c->csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        _batch.add_change(fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                                        // Guard auto-detects hasPendingWrite and arms WRITE
                                        return;
                                    }
                                    throw;
@@ -410,13 +418,9 @@ namespace netplus {
                        }

                        // Re-arm if handshake still in progress
                        if (!c->csock->getHandshakeDone()) {
                            int ev = KQUEUE_EV_READ;
                            if (c->csock->hasPendingWrite()) ev |= KQUEUE_EV_WRITE;
                            if (ev & KQUEUE_EV_READ) rearm.rearm_read();
                            if (ev & KQUEUE_EV_WRITE) rearm.rearm_write();
                        // Guard auto-detects READ + WRITE needs
                        if (!c->csock->getHandshakeDone())
                            return;
                        }

                        // Post-handshake: if the socket has a framing
                        // callback, invoke it with stream_id=0 to produce
@@ -457,7 +461,6 @@ namespace netplus {
                                needClose = true;
                            } else if (rcv > 0) {
                                c->RecvData.append(buf.data.buf, rcv);
                                rearm.rearm_read();  // Will need READ again after this event
                                evconnection->RequestEvent(*c, tid, args);
                            }

@@ -483,13 +486,6 @@ namespace netplus {
                                break;
                        }

                        // EV_ONESHOT removes the filter after it fires.
                        // Always re-arm READ so we catch future data even
                        // if the loop above produced nothing (e.g. handshake
                        // just finished and no app data arrived yet).
                        if (!needClose)
                            rearm.rearm_read();

                        // Step 4: Flush and send
                        // Always attempt to send if there is data queued
                        if ((c->SendData.pos < c->SendData.size() || c->csock->hasPendingWrite()) && !needClose) {
@@ -498,7 +494,7 @@ namespace netplus {
                                    c->csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        rearm.rearm_write();
                                        // Guard auto-detects hasPendingWrite and arms WRITE
                                        return;
                                    }
                                    throw;
@@ -543,10 +539,9 @@ namespace netplus {
                                }
                            }

                            // Send blocked — arm WRITE to resume later
                            if (sendBlockedWouldBlock || c->csock->hasPendingWrite()) {
                                rearm.rearm_write();
                                if (c->SendData.pos < c->SendData.size())
                            // Send blocked — guard auto-detects and arms WRITE
                            if ((sendBlockedWouldBlock || c->csock->hasPendingWrite())
                                && c->SendData.pos < c->SendData.size()) {
                                return;
                            }

@@ -560,9 +555,7 @@ namespace netplus {

                } catch (NetException& e) {
                    if (e.getErrorType() == NetException::Note) {
                        rearm.rearm_read();
                        if (c->csock->hasPendingWrite() || c->SendData.pos < c->SendData.size())
                            rearm.rearm_write();
                        // Guard auto-detects READ + WRITE needs
                        return;
                    }
                    needClose = true;