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

test

parent 179a0456
Loading
Loading
Loading
Loading
+16 −1
Changes for src/event/epoll.cpp: 16 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -627,6 +627,20 @@ namespace netplus {
        // across listeners -- that would starve whichever listener's poll instance doesn't win
        // the race every interval.
        // ------------------------------------------------------------
        // Protocol timers (QUIC PTO) must run even when nothing arrives;
        // see socket::serviceTimers().
        void serviceTimers() {
            try { _ServerSocket->serviceTimers(); } catch (...) {}
        }

        // QUIC listeners need wakeups at PTO granularity, not the loop's
        // default (1s), or a tail-lost packet waits that long for its probe.
        int waitTimeout(int timeout) const {
            if (_ServerSocket->_Type == sockettype::QUIC && (timeout < 0 || timeout > 25))
                return 25;
            return timeout;
        }

        void reapIdleConnections(int idleTimeoutSeconds, int tid, ULONG_PTR args) {
            if (idleTimeoutSeconds <= 0) return;

@@ -706,7 +720,8 @@ namespace netplus {

                        while (event::Running) {
                            try {
                                int wait = pollptr.waitEventHandler(eargs->timeout);
                                int wait = pollptr.waitEventHandler(pollptr.waitTimeout(eargs->timeout));
                                pollptr.serviceTimers();

                                if (wait == 0) {
                                    // epoll_wait already blocked for the timeout
+16 −1
Changes for src/event/kqueue.cpp: 16 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -737,6 +737,20 @@ namespace netplus {
        // reasoning on why per-instance (not shared/atomic) _lastSweepAttempt is safe here
        // despite multiple poll instances (one per worker thread) sharing this listener's kqfd.
        // ------------------------------------------------------------
        // Protocol timers (QUIC PTO) must run even when nothing arrives;
        // see socket::serviceTimers().
        void serviceTimers() {
            try { _ServerSocket->serviceTimers(); } catch (...) {}
        }

        // QUIC listeners need wakeups at PTO granularity, not the loop's
        // default (1s), or a tail-lost packet waits that long for its probe.
        int waitTimeout(int timeout) const {
            if (_ServerSocket->_Type == sockettype::QUIC && (timeout < 0 || timeout > 25))
                return 25;
            return timeout;
        }

        void reapIdleConnections(int idleTimeoutSeconds, int tid, ULONG_PTR args) {
            if (idleTimeoutSeconds <= 0) return;

@@ -811,7 +825,8 @@ namespace netplus {

                        while (event::Running) {
                            try {
                                int wait = pollptr.waitEventHandler(eargs->timeout);
                                int wait = pollptr.waitEventHandler(pollptr.waitTimeout(eargs->timeout));
                                pollptr.serviceTimers();

                                if (wait == 0) {
                                    // kevent() already blocked for the full
+16 −1
Changes for src/event/poll.cpp: 16 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -177,6 +177,20 @@ namespace netplus {
    // ------------------------------------------------------------
    class poll : public pollapi {
    public:
        // Protocol timers (QUIC PTO) must run even when nothing arrives;
        // see socket::serviceTimers().
        void serviceTimers() {
            try { _ServerSocket->serviceTimers(); } catch (...) {}
        }

        // QUIC listeners need wakeups at PTO granularity, not the loop's
        // default (1s), or a tail-lost packet waits that long for its probe.
        int waitTimeout(int timeout) const {
            if (_ServerSocket->_Type == sockettype::QUIC && (timeout < 0 || timeout > 25))
                return 25;
            return timeout;
        }

        poll(socket* serversocket, eventapi* evcon, int /*pollfd*/, int timeout)
            : pollapi(evcon, timeout),
              _maxFD(0),
@@ -620,7 +634,8 @@ namespace netplus {

                        while (event::Running) {
                            try {
                                int wait = pollptr.waitEventHandler(eargs->timeout);
                                int wait = pollptr.waitEventHandler(pollptr.waitTimeout(eargs->timeout));
                                pollptr.serviceTimers();

                                if (wait == 0) {
                                    #ifdef _WIN32
+47 −0
Changes for src/quic.cpp: 47 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2493,6 +2493,53 @@ void quic::sendVersionNegotiation(const std::vector<uint8_t>& client_dcid,
             client_dcid.size(), client_scid.size(), packet.size(), sent, sent < 0 ? errno : 0);
}

// ============================================================================
// Event-loop timer tick (listener only)
// ============================================================================

void quic::serviceTimers() {
    if (_parent || _Socket < 0) return;

    const auto now = std::chrono::steady_clock::now();
    const auto now_rep = now.time_since_epoch().count();
    auto last = _last_timer_sweep.load(std::memory_order_relaxed);
    if (now_rep - last < std::chrono::duration_cast<std::chrono::steady_clock::duration>(
                             _timer_sweep_interval).count())
        return;
    // Only one thread per interval does the sweep.
    if (!_last_timer_sweep.compare_exchange_strong(last, now_rep, std::memory_order_relaxed))
        return;

    // Snapshot under the shared lock; a connection is registered under
    // several CIDs, so dedupe by pointer.
    std::vector<std::shared_ptr<quic>> conns;
    {
        std::shared_lock<netplus::WriterPreferringSharedMutex> reg_lock(_registry_mutex);
        conns.reserve(_child_connections.size());
        for (auto& kv : _child_connections) {
            if (kv.second) conns.push_back(kv.second);
        }
    }
    std::sort(conns.begin(), conns.end());
    conns.erase(std::unique(conns.begin(), conns.end()), conns.end());

    for (auto& c : conns) {
        // try_lock for the same reason as accept()'s flush_pending_app: a
        // connection busy on another thread is serviced there anyway.
        std::unique_lock<std::recursive_mutex> lock(c->quic_mtx(), std::try_to_lock);
        if (!lock.owns_lock()) continue;
        if (!c->_handshake_complete ||
            c->_conn_state.load() != ConnectionState::Connected)
            continue;
        try {
            c->checkLossAndRetransmit();
            c->flushBatch();
        } catch (...) {
            // A send failure here is retried on the next sweep.
        }
    }
}

// ============================================================================
// Socket Interface: accept (Server-side connection acceptance)
// ============================================================================
+15 −0
Changes for src/socket.h: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -209,6 +209,10 @@ namespace netplus {
		virtual void accept(std::unique_ptr<socket>& csock,bool nonblock) = 0;
		virtual void handshake_after_accept(){};
		virtual void  handshake_after_connect(){};
		// Called by the event loop on every wakeup, including wait timeouts,
		// for each listening socket. Drives protocol timers that must fire
		// even when no datagram arrives (QUIC PTO/loss detection).
		virtual void serviceTimers(){};

#ifdef Windows
		virtual void accept(LPFN_ACCEPTEX lpfnAcceptEx, std::unique_ptr<socket>& csock) = 0;
@@ -819,6 +823,11 @@ namespace netplus {
		
		// Handshake handling (for event loop integration)
		void handshake_after_accept() override;
		// Listener only: runs loss detection / PTO probes for every child
		// connection. Child connections are serviced only when a datagram
		// for them arrives, so without this a lost final packet of a
		// response (tail loss, peer idle) was never retransmitted.
		void serviceTimers() override;
		void pushReceivedData(const uint8_t* data, size_t len) override;
		bool hasBufferedData() const override { return !_recv_buffer.empty(); }

@@ -2207,6 +2216,12 @@ namespace netplus {
		std::chrono::steady_clock::time_point _last_registry_purge{};
		static constexpr std::chrono::milliseconds _registry_purge_interval{100};

		// Throttle for serviceTimers(): every event-loop thread calls it on
		// every wakeup; one sweep per interval is enough for PTO-scale
		// (tens of ms) timers. Atomic because several threads race on it.
		std::atomic<std::chrono::steady_clock::rep> _last_timer_sweep{0};
		static constexpr std::chrono::milliseconds _timer_sweep_interval{10};

		// Flow control batching: avoid sending duplicate MAX_DATA/MAX_STREAM_DATA
		bool _pending_max_data = false;
		uint64_t _pending_max_data_value = 0;