Commit 0bf04f04 authored by jan.koester's avatar jan.koester
Browse files

test

parent 557d1a70
Loading
Loading
Loading
Loading
+38 −38
Changes for src/event/kqueue.cpp: 38 added lines, 38 removed lines.
Original line number Diff line number Diff line
@@ -64,14 +64,14 @@ namespace netplus {
    // ------------------------------------------------------------
    static void safeCloseSocket(std::shared_ptr<con>& c) {
        if (!c) return;
        if (!c->csock) return;
        if (!c->slots[0].csock) return;

        try {
            c->csock->close();
            c->slots[0].csock->close();
        } catch (...) {}
        // Reset to nullptr so concurrent guards (KqueueArmGuard)
        // detect the closed state and skip re-arming.
        c->csock.reset();
        c->slots[0].csock.reset();
    }

    // ------------------------------------------------------------
@@ -176,11 +176,11 @@ namespace netplus {
            if (!_armed) return;
            if (!_batch || !_con) return;
            try {
                if (!_con->csock) return;
                if (!_con->slots[0].csock) return;

                bool needWrite = _con->SendData.pos < _con->SendData.size()
                              || _con->csock->hasPendingWrite()
                              || _con->ResponsePending.exchange(false, std::memory_order_relaxed);
                              || _con->slots[0].csock->hasPendingWrite()
                              || _con->slots[0].ResponsePending.exchange(false, std::memory_order_relaxed);

                if (needWrite) {
                    // Data pending → arm WRITE only.
@@ -344,11 +344,11 @@ namespace netplus {
                if (!ccon) return;

                if (_ServerSocket->_Type == sockettype::TCP)
                    ccon->csock = std::make_unique<tcp>(-1);
                    ccon->slots[0].csock = std::make_unique<tcp>(-1);
                else if (_ServerSocket->_Type == sockettype::SSL) {
                    ssl* srv = static_cast<ssl*>(_ServerSocket);
                    // Pass the certificate bundle map (SNI will select appropriate cert per connection)
                    ccon->csock = std::make_unique<ssl>(srv->_tls.cert_map, -1);
                    ccon->slots[0].csock = std::make_unique<ssl>(*srv->_tls.cert_map, -1);
                } else if (_ServerSocket->_Type == sockettype::QUIC) {
                    // QUIC: All child connections share the parent's UDP socket.
                    // accept() receives packets and either routes to existing children
@@ -368,11 +368,11 @@ namespace netplus {
                    return;
                }

                _ServerSocket->accept(ccon->csock, true);
                if (!ccon || !ccon->csock) return;
                _ServerSocket->accept(ccon->slots[0].csock, true);
                if (!ccon || !ccon->slots[0].csock) return;

                ccon->csock->setNonBlock();
                int fd = ccon->csock->fd();
                ccon->slots[0].csock->setNonBlock();
                int fd = ccon->slots[0].csock->fd();

                // insert into connection map
                {
@@ -387,7 +387,7 @@ namespace netplus {

                // If ConnectEvent queued data (e.g. HTTP/2 SETTINGS),
                // enable WRITE to flush immediately
                if (!ccon->SendData.empty() || ccon->csock->hasPendingWrite()) {
                if (!ccon->SendData.empty() || ccon->slots[0].csock->hasPendingWrite()) {
                    _batch.add_change(fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                }

@@ -408,7 +408,7 @@ namespace netplus {
        // ------------------------------------------------------------
        void IoEventHandler(int fd, int events, const int tid, ULONG_PTR args) override {
            std::shared_ptr<con> c = getConByFd(fd);
            if (!c || !c->csock) {
            if (!c || !c->slots[0].csock) {
                return;
            }

@@ -419,8 +419,8 @@ namespace netplus {
            buffer recvBuf(BLOCKSIZE);

            {
                std::lock_guard<std::mutex> lk(c->event_mutex);
                if (!c->csock) return;
                std::lock_guard<std::recursive_mutex> lk(c->event_mutex);
                if (!c->slots[0].csock) return;

                try {
                    // Peer closed or error?
@@ -445,7 +445,7 @@ namespace netplus {
                        {
                            recvBuf.size = BLOCKSIZE;
                            try {
                                size_t n = c->csock->prime_recv(recvBuf, 0);
                                size_t n = c->slots[0].csock->prime_recv(recvBuf, 0);
                                if (n > 0) gotRawData = true;
                            } catch (NetException& e) {
                                if (e.getErrorType() != NetException::Note) throw;
@@ -456,11 +456,11 @@ namespace netplus {
                        // Keep driving the handshake state machine until:
                        // - Handshake completes, or
                        // - We need to wait for more data (Note exception)
                        while (!c->csock->getHandshakeDone() && !needClose) {
                        while (!c->slots[0].csock->getHandshakeDone() && !needClose) {
                            // First flush any pending writes
                            if (c->csock->hasPendingWrite()) {
                            if (c->slots[0].csock->hasPendingWrite()) {
                                try {
                                    c->csock->flush_out();
                                    c->slots[0].csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        // Guard auto-detects hasPendingWrite and arms WRITE
@@ -472,7 +472,7 @@ namespace netplus {

                            // Drive the handshake state machine
                            try {
                                c->csock->handshake_after_accept();
                                c->slots[0].csock->handshake_after_accept();
                            } catch (NetException& e) {
                                if (e.getErrorType() != NetException::Note) throw;
                                // Note means need more data or write blocked - exit loop
@@ -482,13 +482,13 @@ namespace netplus {

                        // Re-arm if handshake still in progress
                        // Guard auto-detects READ + WRITE needs
                        if (!c->csock->getHandshakeDone())
                        if (!c->slots[0].csock->getHandshakeDone())
                            return;

                        // Post-handshake: if the socket has a framing
                        // callback, invoke it with stream_id=0 to produce
                        // initial connection data (e.g. HTTP/2 SETTINGS).
                        if (auto *sslsock = dynamic_cast<ssl*>(c->csock.get())) {
                        if (auto *sslsock = dynamic_cast<ssl*>(c->slots[0].csock.get())) {
                            const auto &fcb = sslsock->getFramingCallback();
                            if (fcb && c->SendData.empty()) {
                                std::string init = fcb(sslsock->getSelectedAlpn(), 0, "", "");
@@ -503,14 +503,14 @@ namespace netplus {
                        // when multiple records arrive in a single TCP segment.
                        // Edge-triggered: drain completely before returning
                        bool hasAppData = gotRawData ||
                                          c->csock->hasBufferedData();
                        while (hasAppData && c->csock->getHandshakeDone() && !needClose) {
                                          c->slots[0].csock->hasBufferedData();
                        while (hasAppData && c->slots[0].csock->getHandshakeDone() && !needClose) {
                            recvBuf.size = BLOCKSIZE;
                            size_t rcv = 0;
                            bool wouldBlock = false;

                            try {
                                rcv = c->csock->recvData(recvBuf, 0);
                                rcv = c->slots[0].csock->recvData(recvBuf, 0);
                            } catch (NetException& e) {
                                if (e.getErrorType() == NetException::Note) {
                                    wouldBlock = true;
@@ -535,7 +535,7 @@ namespace netplus {
                                bool gotMore = false;
                                try {
                                    recvBuf.size = BLOCKSIZE;
                                    size_t pr = c->csock->prime_recv(recvBuf, 0);
                                    size_t pr = c->slots[0].csock->prime_recv(recvBuf, 0);
                                    if (pr > 0) gotMore = true;
                                } catch (NetException&) {
                                    // EAGAIN / would-block — no more kernel data
@@ -545,16 +545,16 @@ namespace netplus {
                            }

                            // Continue only if there are more buffered records
                            if (rcv == 0 || !c->csock->hasBufferedData())
                            if (rcv == 0 || !c->slots[0].csock->hasBufferedData())
                                break;
                        }

                        // 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) {
                            if (c->csock->hasPendingWrite()) {
                        if ((c->SendData.pos < c->SendData.size() || c->slots[0].csock->hasPendingWrite()) && !needClose) {
                            if (c->slots[0].csock->hasPendingWrite()) {
                                try {
                                    c->csock->flush_out();
                                    c->slots[0].csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        // Guard auto-detects hasPendingWrite and arms WRITE
@@ -574,7 +574,7 @@ namespace netplus {

                                size_t consumed = 0;
                                try {
                                    consumed = c->csock->sendData(out, 0);
                                    consumed = c->slots[0].csock->sendData(out, 0);
                                    // Advance pos immediately after sendData —
                                    // the plaintext is already encrypted and
                                    // queued in the TLS send_queue.  If the
@@ -593,8 +593,8 @@ namespace netplus {
                                        sendBlockedWouldBlock = true;
                                        break;
                                    }
                                    if (c->csock->hasPendingWrite())
                                        c->csock->flush_out();
                                    if (c->slots[0].csock->hasPendingWrite())
                                        c->slots[0].csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        sendBlockedWouldBlock = true;
@@ -605,12 +605,12 @@ namespace netplus {
                            }

                            // Send blocked — guard auto-detects and arms WRITE
                            if ((sendBlockedWouldBlock || c->csock->hasPendingWrite())
                            if ((sendBlockedWouldBlock || c->slots[0].csock->hasPendingWrite())
                                && c->SendData.pos < c->SendData.size()) {
                                return;
                            }

                            if (c->SendData.pos >= c->SendData.size() && !c->csock->hasPendingWrite()) {
                            if (c->SendData.pos >= c->SendData.size() && !c->slots[0].csock->hasPendingWrite()) {
                                c->SendData.clear();
                                c->SendData.pos = 0;
                                evconnection->ResponseEvent(*c, tid, args);
@@ -655,7 +655,7 @@ namespace netplus {

            // now lock per connection
            {
                std::lock_guard<std::mutex> lk(ccon->event_mutex);
                std::lock_guard<std::recursive_mutex> lk(ccon->event_mutex);

                // close() automatically removes all kqueue filters for
                // this fd.  Do NOT add EV_DELETE to the batch — those
@@ -810,7 +810,7 @@ namespace netplus {

        for (auto& c : conns) {
            if (!c) continue;
            std::lock_guard<std::mutex> lk(c->event_mutex);
            std::lock_guard<std::recursive_mutex> lk(c->event_mutex);
            safeCloseSocket(c);
        }
    }
+974 −0

File added.

Preview size limit exceeded, changes collapsed.