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

test

parent 31f28523
Loading
Loading
Loading
Loading
+45 −29
Changes for src/event/epoll.cpp: 45 added lines, 29 removed lines.
Original line number Diff line number Diff line
@@ -31,15 +31,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <memory>
#include <vector>
#include <atomic>
#include <mutex>
#include <map>
#include <thread>
#include <shared_mutex>
#include <unordered_map>

#include <unistd.h>
#include <signal.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <cstring>

#include "config.h"
@@ -52,9 +52,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace netplus {

    static std::vector<socket*> SSOCKETS;
    static std::map<int, std::shared_ptr<con>> CONNECTIONS;
    static std::unordered_map<int, std::shared_ptr<con>> CONNECTIONS;

    static std::mutex POLL_HANDLER_MUTEX;
    static std::shared_mutex POLL_HANDLER_MUTEX;

    // ------------------------------------------------------------
    // Utility: safe close (no deadlock, no double close)
@@ -114,7 +114,7 @@ namespace netplus {
                    next |= EPOLLOUT;
            } else {
                next |= EPOLLIN;
                if (!_con->SendData.empty() || _con->csock->hasPendingWrite())
                if (_con->SendData.pos < _con->SendData.size() || _con->csock->hasPendingWrite())
                    next |= EPOLLOUT;
                // If the TLS layer still has buffered records from a previous
                // prime_recv, the kernel socket may be empty so EPOLLIN alone
@@ -153,7 +153,7 @@ namespace netplus {
              _ServerSocket(serversocket)
        {
            {
                std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX);
                std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX);
                SSOCKETS.emplace_back(_ServerSocket);
            }

@@ -170,7 +170,7 @@ namespace netplus {
        int getEventFd(int pos) { return _Events[pos].data.fd; }

        std::shared_ptr<con> getConByFd(int fd) {
            std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX);
            std::shared_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX);
            auto it = CONNECTIONS.find(fd);
            if (it == CONNECTIONS.end()) return nullptr;
            return it->second;
@@ -258,7 +258,7 @@ namespace netplus {

                // insert into connection map
                {
                    std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX);
                    std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX);
                    CONNECTIONS.emplace(fd, ccon);
                }

@@ -272,7 +272,7 @@ namespace netplus {

                // If ConnectEvent queued data (e.g. HTTP/2 SETTINGS),
                // re-arm with EPOLLOUT so it gets flushed immediately.
                if (!ccon->SendData.empty() || ccon->csock->hasPendingWrite()) {
                if (ccon->SendData.pos < ccon->SendData.size() || ccon->csock->hasPendingWrite()) {
                    epoll_event ev{};
                    ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT;
                    ev.data.fd = fd;
@@ -297,6 +297,9 @@ namespace netplus {

            EpollArmGuard rearm(fd, c, this);
            bool needClose = false;
            // Pre-allocate buffer outside the inner loops to avoid
            // repeated heap allocation on every recv iteration.
            buffer recvBuf(BLOCKSIZE);

            {
                std::lock_guard<std::mutex> lk(c->event_mutex);
@@ -310,9 +313,8 @@ namespace netplus {
                    else {
                        // Step 1: EPOLLIN - buffer transport data
                        if ((events & EPOLLIN) && !needClose) {
                            buffer buf(BLOCKSIZE);
                            try {
                                c->csock->prime_recv(buf, 0);
                                c->csock->prime_recv(recvBuf, 0);
                            } catch (NetException& e) {
                                if (e.getErrorType() != NetException::Note) throw;
                            }
@@ -365,7 +367,7 @@ namespace netplus {
                        // for SETTINGS and the server waits for the browser.
                        if (auto *sslsock = dynamic_cast<ssl*>(c->csock.get())) {
                            const auto &fcb = sslsock->getFramingCallback();
                            if (fcb && c->SendData.empty()) {
                            if (fcb && c->SendData.pos >= c->SendData.size()) {
                                std::string init = fcb(sslsock->getSelectedAlpn(), 0, "", "");
                                if (!init.empty()) {
                                    c->SendData.append(init.data(), init.size());
@@ -379,12 +381,11 @@ namespace netplus {
                        bool hasAppData = (events & EPOLLIN) ||
                                          c->csock->hasBufferedData();
                        while (hasAppData && c->csock->getHandshakeDone() && !needClose) {
                            buffer buf(BLOCKSIZE);
                            size_t rcv = 0;
                            bool wouldBlock = false;

                            try {
                                rcv = c->csock->recvData(buf, 0);
                                rcv = c->csock->recvData(recvBuf, 0);
                            } catch (NetException& e) {
                                if (e.getErrorType() == NetException::Note) {
                                    wouldBlock = true;
@@ -397,7 +398,7 @@ namespace netplus {
                                // True EOF — peer closed the connection
                                needClose = true;
                            } else if (rcv > 0) {
                                c->RecvData.append(buf.data.buf, rcv);
                                c->RecvData.append(recvBuf.data.buf, rcv);
                                evconnection->RequestEvent(*c, tid, args);
                            }

@@ -410,7 +411,7 @@ 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.empty() || c->csock->hasPendingWrite()) && !needClose) {
                        if ((c->SendData.pos < c->SendData.size() || c->csock->hasPendingWrite()) && !needClose) {
                            if (c->csock->hasPendingWrite()) {
                                try {
                                    c->csock->flush_out();
@@ -424,9 +425,10 @@ namespace netplus {
                                }
                            }

                            if (!c->SendData.empty()) {
                                size_t sendlen = std::min((size_t)BLOCKSIZE, c->SendData.size());
                                buffer out(c->SendData.data(), sendlen);
                            if (c->SendData.pos < c->SendData.size()) {
                                size_t remaining = c->SendData.size() - c->SendData.pos;
                                size_t sendlen = std::min((size_t)BLOCKSIZE, remaining);
                                buffer out(c->SendData.data() + c->SendData.pos, sendlen);

                                size_t consumed = 0;
                                try {
@@ -443,11 +445,24 @@ namespace netplus {
                                }

                                if (consumed > 0) {
                                    c->SendData.erase(c->SendData.begin(), c->SendData.begin() + consumed);
                                    // 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()) {
                                        // All data consumed — clear and reset
                                        c->SendData.clear();
                                        c->SendData.pos = 0;
                                    } else if (c->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;
                                    }
                                }
                            }

                            if (c->SendData.empty() && !c->csock->hasPendingWrite()) {
                            if (c->SendData.pos >= c->SendData.size() && !c->csock->hasPendingWrite()) {
                                c->SendData.clear();
                                c->SendData.pos = 0;
                                evconnection->ResponseEvent(*c, tid, args);
                            }
                        }
@@ -456,7 +471,7 @@ namespace netplus {
                } catch (NetException& e) {
                    if (e.getErrorType() == NetException::Note) {
                        int ev = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT;
                        if (c->csock->hasPendingWrite() || !c->SendData.empty())
                        if (c->csock->hasPendingWrite() || c->SendData.pos < c->SendData.size())
                            ev |= EPOLLOUT;
                        rearm.disarm();
                        setpollEventsFd(fd, ev);
@@ -482,11 +497,11 @@ namespace netplus {

            // take out of map first
            {
                std::lock_guard<std::mutex> lock(POLL_HANDLER_MUTEX);
                std::unique_lock<std::shared_mutex> lock(POLL_HANDLER_MUTEX);
                auto it = CONNECTIONS.find(fd);
                if (it == CONNECTIONS.end())
                    return;
                ccon = it->second;
                ccon = std::move(it->second);
                CONNECTIONS.erase(it);
            }

@@ -541,8 +556,8 @@ namespace netplus {
                                int wait = pollptr.waitEventHandler(eargs->timeout);

                                if (wait == 0) {
                                    // No events, brief sleep to reduce CPU usage
                                    usleep(1000);  // 1ms
                                    // epoll_wait already blocked for the timeout
                                    // duration — no need for additional sleep.
                                    continue;
                                }

@@ -561,7 +576,8 @@ namespace netplus {
                                }
                            } catch (NetException& e) {
                                std::cerr << "Poll Error: " << e.what() << std::endl;
                                usleep(10000);  // 10ms sleep on error to reduce CPU thrashing
                                // brief pause after error to avoid busy-loop
                                std::this_thread::sleep_for(std::chrono::milliseconds(10));
                            }
                        }
                    } catch (NetException& e) {
@@ -617,7 +633,7 @@ namespace netplus {
        std::vector<std::shared_ptr<con>> conns;

        {
            std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX);
            std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX);

            servers = SSOCKETS;
            for (auto& kv : CONNECTIONS)