Commit 82df2152 authored by jan.koester's avatar jan.koester
Browse files

test

parent 83b4d9ba
Loading
Loading
Loading
Loading
+2 −22
Changes for src/connection.cpp: 2 added lines, 22 removed lines.
Original line number Diff line number Diff line
@@ -97,33 +97,13 @@ size_t netplus::condata<char>::search(const char* word){
}

netplus::con::con()
    : slots(1),
      csock(slots[0].csock),
      RecvData(slots[0].RecvData),
      SendData(slots[0].SendData),
      SendOff(slots[0].SendOff),
      WritePending(slots[0].WritePending),
      ReadPending(slots[0].ReadPending),
      PeerClosed(slots[0].PeerClosed),
      Closing(slots[0].Closing),
      ResponsePending(slots[0].ResponsePending),
      PendingOps(slots[0].PendingOps)
    : slots(1)
{
    lasteventime = time(nullptr);
}

netplus::con::con(eventapi *event)
    : slots(1),
      csock(slots[0].csock),
      RecvData(slots[0].RecvData),
      SendData(slots[0].SendData),
      SendOff(slots[0].SendOff),
      WritePending(slots[0].WritePending),
      ReadPending(slots[0].ReadPending),
      PeerClosed(slots[0].PeerClosed),
      Closing(slots[0].Closing),
      ResponsePending(slots[0].ResponsePending),
      PendingOps(slots[0].PendingOps)
    : slots(1)
{
    lasteventime = time(nullptr);
}
+0 −12
Changes for src/connection.h: 0 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -181,18 +181,6 @@ namespace netplus {
            // --- Sub-connection slots ---
            std::vector<ConSlot> slots;   // slot[0] = primary / listening socket

            // Backward-compatible accessors (delegate to slot 0)
            std::unique_ptr<socket>&       csock;
            condata<char>&                 RecvData;
            condata<char>&                 SendData;
            size_t&                        SendOff;
            std::atomic_bool&              WritePending;
            std::atomic_bool&              ReadPending;
            std::atomic_bool&              PeerClosed;
            std::atomic_bool&              Closing;
            std::atomic_bool&              ResponsePending;
            std::atomic_int&               PendingOps;

            int DebugId=0;

            // Application-defined extension data.
+49 −49
Changes for src/event/epoll.cpp: 49 added lines, 49 removed lines.
Original line number Diff line number Diff line
@@ -61,10 +61,10 @@ 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 (...) {}
    }

@@ -101,13 +101,13 @@ namespace netplus {

        ~EpollArmGuard() {
            if (!_armed) return;
            if (!_con || !_con->csock) return;
            if (!_con || !_con->slots[0].csock) return;

            int next = EPOLLRDHUP | EPOLLIN | EPOLLONESHOT;

            // Check if we have pending writes
            if (_con->SendData.pos < _con->SendData.size() || _con->csock->hasPendingWrite()
                || _con->ResponsePending.exchange(false, std::memory_order_relaxed))
            if (_con->slots[0].SendData.pos < _con->slots[0].SendData.size() || _con->slots[0].csock->hasPendingWrite()
                || _con->slots[0].ResponsePending.exchange(false, std::memory_order_relaxed))
                next |= EPOLLOUT;

            try {
@@ -210,10 +210,10 @@ 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);
                    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: Process ONE UDP datagram per epoll event.
                    // The server socket uses level-triggered EPOLLIN,
@@ -228,12 +228,12 @@ 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;

            register_connection:
                // accept4(SOCK_NONBLOCK) already sets non-blocking in tcp::accept
                int fd = ccon->csock->fd();
                int fd = ccon->slots[0].csock->fd();

                // insert into connection map
                {
@@ -256,7 +256,7 @@ namespace netplus {
                    // With EPOLLONESHOT, socket must be re-armed after each event.
                    // The batched send loop reduces re-arm syscalls by draining data
                    // in a single epoll event rather than one chunk per event.
                    if (ccon->SendData.pos < ccon->SendData.size() || ccon->csock->hasPendingWrite()) {
                    if (ccon->slots[0].SendData.pos < ccon->slots[0].SendData.size() || ccon->slots[0].csock->hasPendingWrite()) {
                        epoll_event ev{};
                        ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT;
                        ev.data.fd = fd;
@@ -279,7 +279,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;
            }

@@ -291,7 +291,7 @@ namespace netplus {
            bool doClose = false;
            {
                std::lock_guard<std::recursive_mutex> lk(c->event_mutex);
                if (!c->csock) return;
                if (!c->slots[0].csock) return;

                EpollArmGuard rearm(fd, c, this);

@@ -307,7 +307,7 @@ namespace netplus {
                        if ((events & EPOLLIN) && !needClose) {
                            try {
                                buffer primeBuf(BLOCKSIZE);
                                c->csock->prime_recv(primeBuf, 0);
                                c->slots[0].csock->prime_recv(primeBuf, 0);
                            } catch (NetException& e) {
                                if (e.getErrorType() != NetException::Note) throw;
                            }
@@ -317,11 +317,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) {
                                        rearm.disarm();
@@ -336,7 +336,7 @@ namespace netplus {
                            // Use handshake_after_connect() for client sockets,
                            // handshake_after_accept() for server sockets
                            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
@@ -356,14 +356,14 @@ namespace netplus {
                        // that can't be sent yet (sendData throws Note),
                        // which causes the EpollArmGuard to add EPOLLOUT,
                        // creating a busy-loop that starves all workers.
                        if (c->csock->getHandshakeDone()) {
                            if (c->csock->getSocketType() == sockettype::SSL) {
                                auto *sslsock = static_cast<ssl*>(c->csock.get());
                        if (c->slots[0].csock->getHandshakeDone()) {
                            if (c->slots[0].csock->getSocketType() == sockettype::SSL) {
                                auto *sslsock = static_cast<ssl*>(c->slots[0].csock.get());
                                const auto &fcb = sslsock->getFramingCallback();
                                if (fcb && c->SendData.pos >= c->SendData.size()) {
                                if (fcb && c->slots[0].SendData.pos >= c->slots[0].SendData.size()) {
                                    std::string init = fcb(sslsock->getSelectedAlpn(), 0, "", "");
                                    if (!init.empty()) {
                                        c->SendData.append(init.data(), init.size());
                                        c->slots[0].SendData.append(init.data(), init.size());
                                    }
                                }
                            }
@@ -373,8 +373,8 @@ namespace netplus {
                        // Drain all available TLS records to avoid stalling
                        // when multiple records arrive in a single TCP segment.
                        bool hasAppData = (events & EPOLLIN) ||
                                          c->csock->hasBufferedData();
                        while (hasAppData && c->csock->getHandshakeDone() && !needClose) {
                                          c->slots[0].csock->hasBufferedData();
                        while (hasAppData && c->slots[0].csock->getHandshakeDone() && !needClose) {
                            size_t rcv = 0;
                            bool wouldBlock = false;

@@ -384,7 +384,7 @@ namespace netplus {
                            recvBuf.size = BLOCKSIZE;

                            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;
@@ -397,7 +397,7 @@ namespace netplus {
                                // True EOF — peer closed the connection
                                needClose = true;
                            } else if (rcv > 0) {
                                c->RecvData.append(recvBuf.data.buf, rcv);
                                c->slots[0].RecvData.append(recvBuf.data.buf, rcv);
                                evconnection->RequestEvent(*c, tid, args);
                            }

@@ -411,7 +411,7 @@ namespace netplus {
                                bool gotMore = false;
                                try {
                                    buffer prBuf(BLOCKSIZE);
                                    size_t pr = c->csock->prime_recv(prBuf, 0);
                                    size_t pr = c->slots[0].csock->prime_recv(prBuf, 0);
                                    if (pr > 0) gotMore = true;
                                } catch (NetException&) {
                                    // EAGAIN / would-block — no more kernel data
@@ -421,7 +421,7 @@ 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;
                        }

@@ -429,10 +429,10 @@ namespace netplus {
                        // Always attempt to send if there is data queued
                        // (browsers require the server's HTTP/2 SETTINGS before
                        //  sending their request, so we must not wait for EPOLLOUT)
                        if ((c->SendData.pos < c->SendData.size() || c->csock->hasPendingWrite()) && !needClose) {
                            if (c->csock->hasPendingWrite()) {
                        if ((c->slots[0].SendData.pos < c->slots[0].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) {
                                        rearm.disarm();
@@ -447,16 +447,16 @@ namespace netplus {
                            // This significantly improves throughput for large files by
                            // reducing epoll_ctl system call overhead
                            bool sendBlockedWouldBlock = false;
                            while (c->SendData.pos < c->SendData.size() && !sendBlockedWouldBlock && !needClose) {
                                size_t remaining = c->SendData.size() - c->SendData.pos;
                            while (c->slots[0].SendData.pos < c->slots[0].SendData.size() && !sendBlockedWouldBlock && !needClose) {
                                size_t remaining = c->slots[0].SendData.size() - c->slots[0].SendData.pos;
                                size_t sendlen = std::min((size_t)BLOCKSIZE, remaining);
                                buffer out(c->SendData.data() + c->SendData.pos, sendlen);
                                buffer out(c->slots[0].SendData.data() + c->slots[0].SendData.pos, sendlen);

                                size_t consumed = 0;
                                try {
                                    consumed = c->csock->sendData(out, 0);
                                    if (c->csock->hasPendingWrite())
                                        c->csock->flush_out();
                                    consumed = c->slots[0].csock->sendData(out, 0);
                                    if (c->slots[0].csock->hasPendingWrite())
                                        c->slots[0].csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        sendBlockedWouldBlock = true;
@@ -468,15 +468,15 @@ namespace netplus {
                                if (consumed > 0) {
                                    // Advance position instead of erasing from the front
                                    // (erase from vector front is O(n) due to element shift).
                                    c->SendData.pos += consumed;
                                    if (c->SendData.pos >= c->SendData.size()) {
                                    c->slots[0].SendData.pos += consumed;
                                    if (c->slots[0].SendData.pos >= c->slots[0].SendData.size()) {
                                        // All data consumed — clear and reset
                                        c->SendData.clear();
                                        c->SendData.pos = 0;
                                    } else if (c->SendData.pos > BLOCKSIZE * 4) {
                                        c->slots[0].SendData.clear();
                                        c->slots[0].SendData.pos = 0;
                                    } else if (c->slots[0].SendData.pos > BLOCKSIZE * 4) {
                                        // Compact only when waste exceeds threshold
                                        c->SendData.erase(c->SendData.begin(), c->SendData.begin() + c->SendData.pos);
                                        c->SendData.pos = 0;
                                        c->slots[0].SendData.erase(c->slots[0].SendData.begin(), c->slots[0].SendData.begin() + c->slots[0].SendData.pos);
                                        c->slots[0].SendData.pos = 0;
                                    }
                                } else {
                                    // No progress made, socket likely full
@@ -485,13 +485,13 @@ namespace netplus {
                            }

                            // Send blocked, guard will re-arm with EPOLLOUT
                            if (sendBlockedWouldBlock && c->SendData.pos < c->SendData.size()) {
                            if (sendBlockedWouldBlock && c->slots[0].SendData.pos < c->slots[0].SendData.size()) {
                                return;
                            }

                            if (c->SendData.pos >= c->SendData.size() && !c->csock->hasPendingWrite()) {
                                c->SendData.clear();
                                c->SendData.pos = 0;
                            if (c->slots[0].SendData.pos >= c->slots[0].SendData.size() && !c->slots[0].csock->hasPendingWrite()) {
                                c->slots[0].SendData.clear();
                                c->slots[0].SendData.pos = 0;
                                evconnection->ResponseEvent(*c, tid, args);
                            }
                        }
+71 −71

File changed.

Preview size limit exceeded, changes collapsed.

+44 −44

File changed.

Preview size limit exceeded, changes collapsed.

Loading