Commit a73d79ed authored by jan.koester's avatar jan.koester
Browse files

test

parent ca75a19c
Loading
Loading
Loading
Loading
+13 −63
Changes for src/event/kqueue.cpp: 13 added lines, 63 removed lines.
Original line number Diff line number Diff line
@@ -100,53 +100,8 @@ namespace netplus {
    #define KQUEUE_EV_ERROR  4
    #define KQUEUE_EV_EOF    8

    // ------------------------------------------------------------
    // RAII Rearm (kqueue EV_ONESHOT)
    // ------------------------------------------------------------
    class KqueueArmGuard {
    public:
        KqueueArmGuard(int fd, std::shared_ptr<con> c, pollapi* pptr)
            : _fd(fd), _con(std::move(c)), _pptr(pptr) {}

        ~KqueueArmGuard() {
            if (!_armed) return;
            if (!_con || !_con->csock) return;

            int next = 0;

            // during handshake => need READ, and WRITE only if pending writes
            if (!_con->csock->getHandshakeDone()) {
                next |= KQUEUE_EV_READ;
                if (_con->csock->hasPendingWrite())
                    next |= KQUEUE_EV_WRITE;
            } else {
                next |= KQUEUE_EV_READ;
                if (!_con->SendData.empty() || _con->csock->hasPendingWrite())
                    next |= KQUEUE_EV_WRITE;
                // If the TLS layer still has buffered records from a previous
                // prime_recv, the kernel socket may be empty so EVFILT_READ alone
                // won't fire.  Adding EVFILT_WRITE (almost always ready) forces
                // kevent to return, giving us another chance to process
                // the buffered data.
                if (_con->csock->hasBufferedData())
                    next |= KQUEUE_EV_WRITE;
            }

            try {
                _pptr->setpollEventsFd(_fd, next);
            } catch (...) {
                // ignore
            }
        }

        void disarm() { _armed = false; }

    private:
        bool _armed = true;
        int _fd;
        std::shared_ptr<con> _con;
        pollapi* _pptr;
    };
    // Level-triggered kqueue no longer requires re-arming
    // (EV_ONESHOT eliminated for better performance)

    // ------------------------------------------------------------
    // poll implementation (kqueue)
@@ -217,10 +172,10 @@ namespace netplus {
                return;
            }

            // Set up read event (oneshot)
            // Set up read event (level-triggered)
            if (events & KQUEUE_EV_READ) {
                EV_SET(&changes[nchanges], fd, EVFILT_READ,
                       EV_ADD | EV_ONESHOT, 0, 0, nullptr);
                       EV_ADD, 0, 0, nullptr);
                nchanges++;
            } else {
                struct kevent delev;
@@ -228,10 +183,10 @@ namespace netplus {
                kevent(_kqfd, &delev, 1, nullptr, 0, nullptr);
            }

            // Set up write event (oneshot)
            // Set up write event (level-triggered)
            if (events & KQUEUE_EV_WRITE) {
                EV_SET(&changes[nchanges], fd, EVFILT_WRITE,
                       EV_ADD | EV_ONESHOT, 0, 0, nullptr);
                       EV_ADD, 0, 0, nullptr);
                nchanges++;
            } else {
                struct kevent delev;
@@ -327,10 +282,10 @@ namespace netplus {
                    CONNECTIONS.emplace(fd, ccon);
                }

                // Add to kqueue (EVFILT_READ + EV_ONESHOT)
                // Add to kqueue (level-triggered)
                struct kevent setevent;
                EV_SET(&setevent, fd, EVFILT_READ,
                       EV_ADD | EV_ONESHOT, 0, 0, nullptr);
                       EV_ADD, 0, 0, nullptr);

                if (kevent(_kqfd, &setevent, 1, nullptr, 0, nullptr) < 0) {
                    std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX);
@@ -364,7 +319,6 @@ namespace netplus {
                return;
            }

            KqueueArmGuard rearm(fd, c, this);
            bool needClose = false;

            {
@@ -406,7 +360,7 @@ namespace netplus {
                                    c->csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        rearm.disarm();
                                        // Level-triggered: socket will re-fire when ready
                                        setpollEventsFd(fd, KQUEUE_EV_WRITE);
                                        return;
                                    }
@@ -424,11 +378,11 @@ namespace netplus {
                            }
                        }

                        // Re-arm if handshake still in progress
                        // No need to re-arm with level-triggered kqueue
                        // Socket will automatically re-fire when ready
                        if (!c->csock->getHandshakeDone()) {
                            int ev = KQUEUE_EV_READ;
                            if (c->csock->hasPendingWrite()) ev |= KQUEUE_EV_WRITE;
                            rearm.disarm();
                            setpollEventsFd(fd, ev);
                            return;
                        }
@@ -487,7 +441,7 @@ namespace netplus {
                                    c->csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        rearm.disarm();
                                        // Level-triggered: socket will re-fire when ready
                                        setpollEventsFd(fd, KQUEUE_EV_WRITE);
                                        return;
                                    }
@@ -506,9 +460,7 @@ namespace netplus {
                                        c->csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        rearm.disarm();
                                        setpollEventsFd(fd, KQUEUE_EV_WRITE);
                                        return;
                                            // Level-triggered: socket will re-fire when ready
                                    }
                                    throw;
                                }
@@ -529,7 +481,6 @@ namespace netplus {
                        int ev = KQUEUE_EV_READ;
                        if (c->csock->hasPendingWrite() || !c->SendData.empty())
                            ev |= KQUEUE_EV_WRITE;
                        rearm.disarm();
                        setpollEventsFd(fd, ev);
                        return;
                    }
@@ -540,7 +491,6 @@ namespace netplus {
            } // unlock event_mutex

            if (needClose) {
                rearm.disarm();
                CloseEventHandler(fd, tid, args);
            }
        }