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

small fixes

parent 9e395bdc
Loading
Loading
Loading
Loading
+16 −7
Changes for src/quic.cpp: 16 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -2187,28 +2187,37 @@ std::vector<uint8_t> quic::buildCryptoFrame(const std::vector<uint8_t>& data, ui
// ============================================================================

void quic::completeHandshake() {
    // Receive and process packets until handshake is complete
    // Receive and process packets until handshake is complete or timeout
    const auto start = std::chrono::steady_clock::now();
    const auto timeout = std::chrono::milliseconds(1500);

    int max_attempts = 100;  // Prevent infinite loop
    while (!_handshake_complete && max_attempts-- > 0) {
    while (!_handshake_complete) {
        try {
            buffer recv_buf_wrapper(65535);
            size_t recv_len = udp::recvData(recv_buf_wrapper, 0);
            size_t recv_len = udp::recvData(recv_buf_wrapper, MSG_DONTWAIT);
            if (recv_len > 0) {
                processIncomingPacket(reinterpret_cast<const uint8_t*>(recv_buf_wrapper.data.buf),
                                     recv_len);
            }
        } catch (NetException&) {
            continue;  // EAGAIN or other transient error, retry
        } catch (NetException& e) {
            if (e.getErrorType() != NetException::Note) {
                throw;
            }
        }

    if (!_handshake_complete) {
        if (_handshake_complete) {
            break;
        }

        if (std::chrono::steady_clock::now() - start > timeout) {
            NetException e;
            e[NetException::Error] << "quic::completeHandshake: handshake timed out";
            throw e;
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(5));
    }

    _conn_state = ConnectionState::Connected;
}