Loading src/crypto/tls.cpp +10 −0 Changes for src/crypto/tls.cpp: 10 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -111,6 +111,7 @@ namespace netplus { } void tls::handshake_after_accept(){ std::lock_guard<std::recursive_mutex> lk(mutex_); // Note: Caller (IOCP layer) must hold con::event_mutex to prevent concurrent access #if SSL_DEBUG Loading Loading @@ -1302,6 +1303,7 @@ namespace netplus { } void tls::handshake_after_connect(){ std::lock_guard<std::recursive_mutex> lk(mutex_); auto note = [&](const std::string& msg){ NetException n; n[NetException::Note] << msg; Loading Loading @@ -2090,6 +2092,7 @@ namespace netplus { size_t tls::recvData(buffer& data, int flags){ std::lock_guard<std::recursive_mutex> lk(mutex_); auto throwSSL = [&](int etype, const std::string& msg) -> void { NetException e; e[etype] << "ssl::recvData: " << msg; Loading Loading @@ -2244,6 +2247,7 @@ namespace netplus { } size_t tls::sendData(buffer& data, int flags) { std::lock_guard<std::recursive_mutex> lk(mutex_); auto throwSSL = [&](int etype, const std::string& msg) -> void { NetException e; e[etype] << "ssl::sendData: " << msg; Loading Loading @@ -2645,6 +2649,7 @@ namespace netplus { } void tls::resetTLS() { std::lock_guard<std::recursive_mutex> lk(mutex_); // MUST be false so next handshake is treated as fresh handshakeStarted = false; Loading Loading @@ -2746,21 +2751,25 @@ namespace netplus { // ================================================================= void tls::queueRaw(const std::vector<uint8_t>& v) { std::lock_guard<std::recursive_mutex> lk(mutex_); if (v.empty()) return; send_queue.emplace_back(v); } void tls::queueRaw(const uint8_t* data, size_t len) { std::lock_guard<std::recursive_mutex> lk(mutex_); if (!data || len == 0) return; send_queue.emplace_back(data, data + len); } void tls::queueRaw(std::vector<uint8_t>&& v) { std::lock_guard<std::recursive_mutex> lk(mutex_); if (v.empty()) return; send_queue.emplace_back(std::move(v)); } void tls::flush_out() { std::lock_guard<std::recursive_mutex> lk(mutex_); // Flush buffered TLS records to the underlying socket // This method writes queued data to the TCP socket // Throws Note (EAGAIN) if socket write would block Loading Loading @@ -2806,6 +2815,7 @@ namespace netplus { // readTlsRecordAsync - Read a complete TLS record from the buffer // ============================================================================ std::vector<uint8_t> tls::readTlsRecordAsync() { std::lock_guard<std::recursive_mutex> lk(mutex_); // Read raw TCP data into rx_tcp_buf for client-side connections. // Uses prime_recv() which calls tcp::recvData() (not ssl::recvData()) // so it works during handshake when handshakeDone is still false. Loading src/crypto/tls.h +12 −2 Changes for src/crypto/tls.h: 12 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ #include <atomic> #include <functional> #include <mutex> #include "config.h" #include "rsa.h" #include "aes.h" Loading Loading @@ -63,16 +64,24 @@ namespace netplus { }; // --- Buffer Management --- bool hasBufferedData() const { return !rx_tcp_buf.empty() || recv_off < rx_record_buf.size(); } bool hasBufferedData() const { std::lock_guard<std::recursive_mutex> lk(mutex_); return !rx_tcp_buf.empty() || recv_off < rx_record_buf.size(); } void pushReceivedData(const uint8_t* data, size_t len) { std::lock_guard<std::recursive_mutex> lk(mutex_); rx_tcp_buf.insert(rx_tcp_buf.end(), data, data + len); } bool hasPendingWrite() const { std::lock_guard<std::recursive_mutex> lk(mutex_); return (!send_queue.empty()) || (!send_record.empty()) || (send_off > 0); } // Getter methods bool getHandshakeDone() const { return handshakeDone; } bool getHandshakeDone() const { std::lock_guard<std::recursive_mutex> lk(mutex_); return handshakeDone; } const std::string& getRequestedHostname() const { return requested_hostname; } // ================================================================= Loading Loading @@ -406,6 +415,7 @@ namespace netplus { netplus::rsa peer_rsa; // --- Hostname (deprecated/legacy) --- mutable std::recursive_mutex mutex_; std::string hostname; // --- Socket reference for sending data --- Loading src/event/epoll.cpp +19 −3 Changes for src/event/epoll.cpp: 19 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -243,6 +243,10 @@ namespace netplus { CONNECTIONS.emplace(fd, ccon); } // Serialize connection setup with I/O for this fd { std::lock_guard<std::mutex> lk(ccon->event_mutex); epoll_event setevent{}; setevent.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT; setevent.data.fd = fd; Loading @@ -261,6 +265,10 @@ namespace netplus { epoll_ctl(_pollFD, EPOLL_CTL_MOD, fd, &ev); } // Drop local reference under the same lock to serialize refcount updates ccon.reset(); } } catch (NetException& e) { if (e.getErrorType() == NetException::Note) return; Loading @@ -277,16 +285,18 @@ namespace netplus { return; } 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); bool doClose = false; { std::lock_guard<std::mutex> lk(c->event_mutex); if (!c->csock) return; EpollArmGuard rearm(fd, c, this); try { // Peer closed? if (events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) { Loading Loading @@ -490,10 +500,16 @@ namespace netplus { } catch (...) { needClose = true; } } // unlock event_mutex if (needClose) { rearm.disarm(); doClose = true; } // Drop local reference under the same lock to serialize refcount updates c.reset(); } // unlock event_mutex if (doClose) { CloseEventHandler(fd, tid, args); } } Loading src/quic.cpp +30 −30 Changes for src/quic.cpp: 30 added lines, 30 removed lines. Original line number Diff line number Diff line Loading @@ -50,7 +50,7 @@ namespace netplus { quic::quic() : udp() { _Socket = -1; _Type = sockettype::QUIC; // QUIC runs over UDP but has its own type _conn_state = ConnectionState::Idle; _conn_state.store(ConnectionState::Idle); _is_server = false; _handshake_complete = false; _version = QUIC_VERSION_1; Loading @@ -61,7 +61,7 @@ quic::quic() : udp() { quic::quic(SOCKET sock) : udp(sock) { _Type = sockettype::QUIC; _conn_state = ConnectionState::Idle; _conn_state.store(ConnectionState::Idle); _is_server = false; _handshake_complete = false; _version = QUIC_VERSION_1; Loading @@ -74,7 +74,7 @@ quic::quic(const std::string& addr, int port, int maxconnections, int sockopts) : udp(addr, port, maxconnections, sockopts) { _Type = sockettype::QUIC; _is_server = true; _conn_state = ConnectionState::Idle; _conn_state.store(ConnectionState::Idle); _handshake_complete = false; _version = QUIC_VERSION_1; Loading @@ -93,7 +93,7 @@ quic::quic(const std::map<std::string, ssl::CertificateBundle>& certs, : udp(addr, port, maxconnections, sockopts) { _Type = sockettype::QUIC; _is_server = true; _conn_state = ConnectionState::Idle; _conn_state.store(ConnectionState::Idle); _handshake_complete = false; _version = QUIC_VERSION_1; _cert_map = certs; Loading Loading @@ -162,7 +162,7 @@ quic::~quic() { std::memset(_app_iv_server, 0, sizeof(_app_iv_server)); std::memset(_app_hp_server, 0, sizeof(_app_hp_server)); _conn_state = ConnectionState::Closed; _conn_state.store(ConnectionState::Closed); } // ============================================================================ Loading Loading @@ -881,7 +881,7 @@ void quic::handshake_after_accept() { if (elapsed_ms > 2000) { // 2 second timeout for fragmented messages _handshake_timeout_triggered = true; _conn_state = ConnectionState::Closed; _conn_state.store(ConnectionState::Closed); } } Loading Loading @@ -927,7 +927,7 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { _child_owned_connections.erase( std::remove_if(_child_owned_connections.begin(), _child_owned_connections.end(), [this](const std::unique_ptr<quic>& p) { if (p && p->_conn_state == ConnectionState::Closed) { if (p && p->_conn_state.load() == ConnectionState::Closed) { _child_connections.erase(p->_original_dcid); _child_connections.erase(p->_local_cid); return true; Loading Loading @@ -1017,8 +1017,8 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { quic* existing = it->second; // Skip stale (Closed/Draining) connections if (existing->_conn_state == ConnectionState::Closed || existing->_conn_state == ConnectionState::Draining) { if (existing->_conn_state.load() == ConnectionState::Closed || existing->_conn_state.load() == ConnectionState::Draining) { _child_connections.erase(it); } else { Loading Loading @@ -1110,8 +1110,8 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { // AND this is a new Initial packet - client is reconnecting uint8_t pkt_type = (dgram[0] & 0x30) >> 4; if (pkt_type == 0x00 && (existing->_conn_state == ConnectionState::Closed || existing->_conn_state == ConnectionState::Draining)) { (existing->_conn_state.load() == ConnectionState::Closed || existing->_conn_state.load() == ConnectionState::Draining)) { // Remove from routing maps _child_connections.erase(existing->_local_cid); Loading Loading @@ -1193,7 +1193,7 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { // Configure the new connection new_quic->_is_server = true; new_quic->_conn_state = ConnectionState::Handshaking; new_quic->_conn_state.store(ConnectionState::Handshaking); new_quic->_version = version; // RFC 9000: Server sends packets TO the client using the client's SCID as DCID new_quic->_remote_cid = scid; // Client's SCID becomes our DCID for responses Loading Loading @@ -1556,13 +1556,13 @@ void quic::close() { std::lock_guard<std::mutex> lock(_quic_mutex); // Don't close if already closed if (_conn_state == ConnectionState::Closed) { if (_conn_state.load() == ConnectionState::Closed) { return; } // If connection is established, send CONNECTION_CLOSE frame if (_conn_state == ConnectionState::Connected || _conn_state == ConnectionState::Handshaking) { if (_conn_state.load() == ConnectionState::Connected || _conn_state.load() == ConnectionState::Handshaking) { // Build CONNECTION_CLOSE frame (error code 0 = NO_ERROR) std::vector<uint8_t> close_frame = buildConnectionCloseFrame(0x00, ""); Loading Loading @@ -1594,7 +1594,7 @@ void quic::close() { } // Enter draining state _conn_state = ConnectionState::Draining; _conn_state.store(ConnectionState::Draining); } // Close all streams Loading Loading @@ -1662,7 +1662,7 @@ void quic::close() { _Socket = -1; } _conn_state = ConnectionState::Closed; _conn_state.store(ConnectionState::Closed); _handshake_complete = false; } Loading Loading @@ -1842,7 +1842,7 @@ void quic::connect(const std::string& addr, int port, bool nonblock) { std::lock_guard<std::mutex> lock(_quic_mutex); // Ensure we're in idle state if (_conn_state != ConnectionState::Idle) { if (_conn_state.load() != ConnectionState::Idle) { NetException e; e[NetException::Error] << "quic::connect: connection already in progress"; throw e; Loading Loading @@ -1918,7 +1918,7 @@ void quic::connect(const std::string& addr, int port, bool nonblock) { generateECDHEKeyPair(); // Build and send ClientHello in Initial packet _conn_state = ConnectionState::Handshaking; _conn_state.store(ConnectionState::Handshaking); // Build CRYPTO frame containing ClientHello std::vector<uint8_t> client_hello = buildClientHello(); Loading Loading @@ -2218,7 +2218,7 @@ void quic::completeHandshake() { std::this_thread::sleep_for(std::chrono::milliseconds(5)); } _conn_state = ConnectionState::Connected; _conn_state.store(ConnectionState::Connected); } // ============================================================================ Loading @@ -2233,13 +2233,13 @@ size_t quic::recvData(buffer& data, int flags) { std::lock_guard<std::mutex> lock(_quic_mutex); // Check connection state if (_conn_state == ConnectionState::Closed) { if (_conn_state.load() == ConnectionState::Closed) { NetException e; e[NetException::Error] << "quic::recvData: connection closed"; throw e; } if (_conn_state != ConnectionState::Connected && !_handshake_complete) { if (_conn_state.load() != ConnectionState::Connected && !_handshake_complete) { NetException e; e[NetException::Error] << "quic::recvData: connection not established"; throw e; Loading Loading @@ -2521,14 +2521,14 @@ void quic::processFrame(const uint8_t* data, size_t len, size_t& offset) { offset += reason_len; // Connection is closing _conn_state = ConnectionState::Draining; _conn_state.store(ConnectionState::Draining); (void)error_code; // Could log this } break; case 0x1e: // HANDSHAKE_DONE _handshake_complete = true; _conn_state = ConnectionState::Connected; _conn_state.store(ConnectionState::Connected); break; default: Loading Loading @@ -3093,7 +3093,7 @@ void quic::processTLSMessages() { if (elapsed_ms > 2000 && !_handshake_timeout_triggered) { // 2 second timeout for fragments _handshake_timeout_triggered = true; _conn_state = ConnectionState::Closed; _conn_state.store(ConnectionState::Closed); return; // Exit without processing } Loading Loading @@ -3428,11 +3428,11 @@ void quic::processFinished(const std::vector<uint8_t>& msg) { sendClientFinished(); _handshake_complete = true; _conn_state = ConnectionState::Connected; _conn_state.store(ConnectionState::Connected); } else { // Server received client's Finished _handshake_complete = true; _conn_state = ConnectionState::Connected; _conn_state.store(ConnectionState::Connected); // Send HANDSHAKE_DONE frame in 1-RTT packet sendHandshakeDone(); Loading Loading @@ -3522,13 +3522,13 @@ size_t quic::sendData(buffer& data, int flags) { std::lock_guard<std::mutex> lock(_quic_mutex); // Check connection state if (_conn_state == ConnectionState::Closed) { if (_conn_state.load() == ConnectionState::Closed) { NetException e; e[NetException::Error] << "quic::sendData: connection closed"; throw e; } if (_conn_state != ConnectionState::Connected && !_handshake_complete) { if (_conn_state.load() != ConnectionState::Connected && !_handshake_complete) { NetException e; e[NetException::Error] << "quic::sendData: connection not established"; throw e; Loading Loading @@ -3611,7 +3611,7 @@ size_t quic::sendData(buffer& data, int flags) { size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len, bool fin) { std::lock_guard<std::mutex> lock(_quic_mutex); if (_conn_state != ConnectionState::Connected && !_handshake_complete) { if (_conn_state.load() != ConnectionState::Connected && !_handshake_complete) { return 0; } Loading src/socket.h +2 −2 Changes for src/socket.h: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -564,7 +564,7 @@ namespace netplus { void setVersion(uint32_t version) { _version = version; } uint32_t getVersion() const { return _version; } ConnectionState getConnectionState() const { return _conn_state; } ConnectionState getConnectionState() const { return _conn_state.load(); } // Stream management uint64_t openStream(bool bidirectional = true); Loading Loading @@ -683,7 +683,7 @@ namespace netplus { uint64_t decodePacketNumber(uint32_t truncated_pn, size_t pn_len, uint64_t largest_pn); // State ConnectionState _conn_state = ConnectionState::Idle; std::atomic<ConnectionState> _conn_state{ConnectionState::Idle}; bool _is_server = false; bool _handshake_complete = false; bool _serverhello_sent = false; // Track if ServerHello has been sent Loading Loading
src/crypto/tls.cpp +10 −0 Changes for src/crypto/tls.cpp: 10 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -111,6 +111,7 @@ namespace netplus { } void tls::handshake_after_accept(){ std::lock_guard<std::recursive_mutex> lk(mutex_); // Note: Caller (IOCP layer) must hold con::event_mutex to prevent concurrent access #if SSL_DEBUG Loading Loading @@ -1302,6 +1303,7 @@ namespace netplus { } void tls::handshake_after_connect(){ std::lock_guard<std::recursive_mutex> lk(mutex_); auto note = [&](const std::string& msg){ NetException n; n[NetException::Note] << msg; Loading Loading @@ -2090,6 +2092,7 @@ namespace netplus { size_t tls::recvData(buffer& data, int flags){ std::lock_guard<std::recursive_mutex> lk(mutex_); auto throwSSL = [&](int etype, const std::string& msg) -> void { NetException e; e[etype] << "ssl::recvData: " << msg; Loading Loading @@ -2244,6 +2247,7 @@ namespace netplus { } size_t tls::sendData(buffer& data, int flags) { std::lock_guard<std::recursive_mutex> lk(mutex_); auto throwSSL = [&](int etype, const std::string& msg) -> void { NetException e; e[etype] << "ssl::sendData: " << msg; Loading Loading @@ -2645,6 +2649,7 @@ namespace netplus { } void tls::resetTLS() { std::lock_guard<std::recursive_mutex> lk(mutex_); // MUST be false so next handshake is treated as fresh handshakeStarted = false; Loading Loading @@ -2746,21 +2751,25 @@ namespace netplus { // ================================================================= void tls::queueRaw(const std::vector<uint8_t>& v) { std::lock_guard<std::recursive_mutex> lk(mutex_); if (v.empty()) return; send_queue.emplace_back(v); } void tls::queueRaw(const uint8_t* data, size_t len) { std::lock_guard<std::recursive_mutex> lk(mutex_); if (!data || len == 0) return; send_queue.emplace_back(data, data + len); } void tls::queueRaw(std::vector<uint8_t>&& v) { std::lock_guard<std::recursive_mutex> lk(mutex_); if (v.empty()) return; send_queue.emplace_back(std::move(v)); } void tls::flush_out() { std::lock_guard<std::recursive_mutex> lk(mutex_); // Flush buffered TLS records to the underlying socket // This method writes queued data to the TCP socket // Throws Note (EAGAIN) if socket write would block Loading Loading @@ -2806,6 +2815,7 @@ namespace netplus { // readTlsRecordAsync - Read a complete TLS record from the buffer // ============================================================================ std::vector<uint8_t> tls::readTlsRecordAsync() { std::lock_guard<std::recursive_mutex> lk(mutex_); // Read raw TCP data into rx_tcp_buf for client-side connections. // Uses prime_recv() which calls tcp::recvData() (not ssl::recvData()) // so it works during handshake when handshakeDone is still false. Loading
src/crypto/tls.h +12 −2 Changes for src/crypto/tls.h: 12 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ #include <atomic> #include <functional> #include <mutex> #include "config.h" #include "rsa.h" #include "aes.h" Loading Loading @@ -63,16 +64,24 @@ namespace netplus { }; // --- Buffer Management --- bool hasBufferedData() const { return !rx_tcp_buf.empty() || recv_off < rx_record_buf.size(); } bool hasBufferedData() const { std::lock_guard<std::recursive_mutex> lk(mutex_); return !rx_tcp_buf.empty() || recv_off < rx_record_buf.size(); } void pushReceivedData(const uint8_t* data, size_t len) { std::lock_guard<std::recursive_mutex> lk(mutex_); rx_tcp_buf.insert(rx_tcp_buf.end(), data, data + len); } bool hasPendingWrite() const { std::lock_guard<std::recursive_mutex> lk(mutex_); return (!send_queue.empty()) || (!send_record.empty()) || (send_off > 0); } // Getter methods bool getHandshakeDone() const { return handshakeDone; } bool getHandshakeDone() const { std::lock_guard<std::recursive_mutex> lk(mutex_); return handshakeDone; } const std::string& getRequestedHostname() const { return requested_hostname; } // ================================================================= Loading Loading @@ -406,6 +415,7 @@ namespace netplus { netplus::rsa peer_rsa; // --- Hostname (deprecated/legacy) --- mutable std::recursive_mutex mutex_; std::string hostname; // --- Socket reference for sending data --- Loading
src/event/epoll.cpp +19 −3 Changes for src/event/epoll.cpp: 19 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -243,6 +243,10 @@ namespace netplus { CONNECTIONS.emplace(fd, ccon); } // Serialize connection setup with I/O for this fd { std::lock_guard<std::mutex> lk(ccon->event_mutex); epoll_event setevent{}; setevent.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT; setevent.data.fd = fd; Loading @@ -261,6 +265,10 @@ namespace netplus { epoll_ctl(_pollFD, EPOLL_CTL_MOD, fd, &ev); } // Drop local reference under the same lock to serialize refcount updates ccon.reset(); } } catch (NetException& e) { if (e.getErrorType() == NetException::Note) return; Loading @@ -277,16 +285,18 @@ namespace netplus { return; } 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); bool doClose = false; { std::lock_guard<std::mutex> lk(c->event_mutex); if (!c->csock) return; EpollArmGuard rearm(fd, c, this); try { // Peer closed? if (events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) { Loading Loading @@ -490,10 +500,16 @@ namespace netplus { } catch (...) { needClose = true; } } // unlock event_mutex if (needClose) { rearm.disarm(); doClose = true; } // Drop local reference under the same lock to serialize refcount updates c.reset(); } // unlock event_mutex if (doClose) { CloseEventHandler(fd, tid, args); } } Loading
src/quic.cpp +30 −30 Changes for src/quic.cpp: 30 added lines, 30 removed lines. Original line number Diff line number Diff line Loading @@ -50,7 +50,7 @@ namespace netplus { quic::quic() : udp() { _Socket = -1; _Type = sockettype::QUIC; // QUIC runs over UDP but has its own type _conn_state = ConnectionState::Idle; _conn_state.store(ConnectionState::Idle); _is_server = false; _handshake_complete = false; _version = QUIC_VERSION_1; Loading @@ -61,7 +61,7 @@ quic::quic() : udp() { quic::quic(SOCKET sock) : udp(sock) { _Type = sockettype::QUIC; _conn_state = ConnectionState::Idle; _conn_state.store(ConnectionState::Idle); _is_server = false; _handshake_complete = false; _version = QUIC_VERSION_1; Loading @@ -74,7 +74,7 @@ quic::quic(const std::string& addr, int port, int maxconnections, int sockopts) : udp(addr, port, maxconnections, sockopts) { _Type = sockettype::QUIC; _is_server = true; _conn_state = ConnectionState::Idle; _conn_state.store(ConnectionState::Idle); _handshake_complete = false; _version = QUIC_VERSION_1; Loading @@ -93,7 +93,7 @@ quic::quic(const std::map<std::string, ssl::CertificateBundle>& certs, : udp(addr, port, maxconnections, sockopts) { _Type = sockettype::QUIC; _is_server = true; _conn_state = ConnectionState::Idle; _conn_state.store(ConnectionState::Idle); _handshake_complete = false; _version = QUIC_VERSION_1; _cert_map = certs; Loading Loading @@ -162,7 +162,7 @@ quic::~quic() { std::memset(_app_iv_server, 0, sizeof(_app_iv_server)); std::memset(_app_hp_server, 0, sizeof(_app_hp_server)); _conn_state = ConnectionState::Closed; _conn_state.store(ConnectionState::Closed); } // ============================================================================ Loading Loading @@ -881,7 +881,7 @@ void quic::handshake_after_accept() { if (elapsed_ms > 2000) { // 2 second timeout for fragmented messages _handshake_timeout_triggered = true; _conn_state = ConnectionState::Closed; _conn_state.store(ConnectionState::Closed); } } Loading Loading @@ -927,7 +927,7 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { _child_owned_connections.erase( std::remove_if(_child_owned_connections.begin(), _child_owned_connections.end(), [this](const std::unique_ptr<quic>& p) { if (p && p->_conn_state == ConnectionState::Closed) { if (p && p->_conn_state.load() == ConnectionState::Closed) { _child_connections.erase(p->_original_dcid); _child_connections.erase(p->_local_cid); return true; Loading Loading @@ -1017,8 +1017,8 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { quic* existing = it->second; // Skip stale (Closed/Draining) connections if (existing->_conn_state == ConnectionState::Closed || existing->_conn_state == ConnectionState::Draining) { if (existing->_conn_state.load() == ConnectionState::Closed || existing->_conn_state.load() == ConnectionState::Draining) { _child_connections.erase(it); } else { Loading Loading @@ -1110,8 +1110,8 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { // AND this is a new Initial packet - client is reconnecting uint8_t pkt_type = (dgram[0] & 0x30) >> 4; if (pkt_type == 0x00 && (existing->_conn_state == ConnectionState::Closed || existing->_conn_state == ConnectionState::Draining)) { (existing->_conn_state.load() == ConnectionState::Closed || existing->_conn_state.load() == ConnectionState::Draining)) { // Remove from routing maps _child_connections.erase(existing->_local_cid); Loading Loading @@ -1193,7 +1193,7 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { // Configure the new connection new_quic->_is_server = true; new_quic->_conn_state = ConnectionState::Handshaking; new_quic->_conn_state.store(ConnectionState::Handshaking); new_quic->_version = version; // RFC 9000: Server sends packets TO the client using the client's SCID as DCID new_quic->_remote_cid = scid; // Client's SCID becomes our DCID for responses Loading Loading @@ -1556,13 +1556,13 @@ void quic::close() { std::lock_guard<std::mutex> lock(_quic_mutex); // Don't close if already closed if (_conn_state == ConnectionState::Closed) { if (_conn_state.load() == ConnectionState::Closed) { return; } // If connection is established, send CONNECTION_CLOSE frame if (_conn_state == ConnectionState::Connected || _conn_state == ConnectionState::Handshaking) { if (_conn_state.load() == ConnectionState::Connected || _conn_state.load() == ConnectionState::Handshaking) { // Build CONNECTION_CLOSE frame (error code 0 = NO_ERROR) std::vector<uint8_t> close_frame = buildConnectionCloseFrame(0x00, ""); Loading Loading @@ -1594,7 +1594,7 @@ void quic::close() { } // Enter draining state _conn_state = ConnectionState::Draining; _conn_state.store(ConnectionState::Draining); } // Close all streams Loading Loading @@ -1662,7 +1662,7 @@ void quic::close() { _Socket = -1; } _conn_state = ConnectionState::Closed; _conn_state.store(ConnectionState::Closed); _handshake_complete = false; } Loading Loading @@ -1842,7 +1842,7 @@ void quic::connect(const std::string& addr, int port, bool nonblock) { std::lock_guard<std::mutex> lock(_quic_mutex); // Ensure we're in idle state if (_conn_state != ConnectionState::Idle) { if (_conn_state.load() != ConnectionState::Idle) { NetException e; e[NetException::Error] << "quic::connect: connection already in progress"; throw e; Loading Loading @@ -1918,7 +1918,7 @@ void quic::connect(const std::string& addr, int port, bool nonblock) { generateECDHEKeyPair(); // Build and send ClientHello in Initial packet _conn_state = ConnectionState::Handshaking; _conn_state.store(ConnectionState::Handshaking); // Build CRYPTO frame containing ClientHello std::vector<uint8_t> client_hello = buildClientHello(); Loading Loading @@ -2218,7 +2218,7 @@ void quic::completeHandshake() { std::this_thread::sleep_for(std::chrono::milliseconds(5)); } _conn_state = ConnectionState::Connected; _conn_state.store(ConnectionState::Connected); } // ============================================================================ Loading @@ -2233,13 +2233,13 @@ size_t quic::recvData(buffer& data, int flags) { std::lock_guard<std::mutex> lock(_quic_mutex); // Check connection state if (_conn_state == ConnectionState::Closed) { if (_conn_state.load() == ConnectionState::Closed) { NetException e; e[NetException::Error] << "quic::recvData: connection closed"; throw e; } if (_conn_state != ConnectionState::Connected && !_handshake_complete) { if (_conn_state.load() != ConnectionState::Connected && !_handshake_complete) { NetException e; e[NetException::Error] << "quic::recvData: connection not established"; throw e; Loading Loading @@ -2521,14 +2521,14 @@ void quic::processFrame(const uint8_t* data, size_t len, size_t& offset) { offset += reason_len; // Connection is closing _conn_state = ConnectionState::Draining; _conn_state.store(ConnectionState::Draining); (void)error_code; // Could log this } break; case 0x1e: // HANDSHAKE_DONE _handshake_complete = true; _conn_state = ConnectionState::Connected; _conn_state.store(ConnectionState::Connected); break; default: Loading Loading @@ -3093,7 +3093,7 @@ void quic::processTLSMessages() { if (elapsed_ms > 2000 && !_handshake_timeout_triggered) { // 2 second timeout for fragments _handshake_timeout_triggered = true; _conn_state = ConnectionState::Closed; _conn_state.store(ConnectionState::Closed); return; // Exit without processing } Loading Loading @@ -3428,11 +3428,11 @@ void quic::processFinished(const std::vector<uint8_t>& msg) { sendClientFinished(); _handshake_complete = true; _conn_state = ConnectionState::Connected; _conn_state.store(ConnectionState::Connected); } else { // Server received client's Finished _handshake_complete = true; _conn_state = ConnectionState::Connected; _conn_state.store(ConnectionState::Connected); // Send HANDSHAKE_DONE frame in 1-RTT packet sendHandshakeDone(); Loading Loading @@ -3522,13 +3522,13 @@ size_t quic::sendData(buffer& data, int flags) { std::lock_guard<std::mutex> lock(_quic_mutex); // Check connection state if (_conn_state == ConnectionState::Closed) { if (_conn_state.load() == ConnectionState::Closed) { NetException e; e[NetException::Error] << "quic::sendData: connection closed"; throw e; } if (_conn_state != ConnectionState::Connected && !_handshake_complete) { if (_conn_state.load() != ConnectionState::Connected && !_handshake_complete) { NetException e; e[NetException::Error] << "quic::sendData: connection not established"; throw e; Loading Loading @@ -3611,7 +3611,7 @@ size_t quic::sendData(buffer& data, int flags) { size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len, bool fin) { std::lock_guard<std::mutex> lock(_quic_mutex); if (_conn_state != ConnectionState::Connected && !_handshake_complete) { if (_conn_state.load() != ConnectionState::Connected && !_handshake_complete) { return 0; } Loading
src/socket.h +2 −2 Changes for src/socket.h: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -564,7 +564,7 @@ namespace netplus { void setVersion(uint32_t version) { _version = version; } uint32_t getVersion() const { return _version; } ConnectionState getConnectionState() const { return _conn_state; } ConnectionState getConnectionState() const { return _conn_state.load(); } // Stream management uint64_t openStream(bool bidirectional = true); Loading Loading @@ -683,7 +683,7 @@ namespace netplus { uint64_t decodePacketNumber(uint32_t truncated_pn, size_t pn_len, uint64_t largest_pn); // State ConnectionState _conn_state = ConnectionState::Idle; std::atomic<ConnectionState> _conn_state{ConnectionState::Idle}; bool _is_server = false; bool _handshake_complete = false; bool _serverhello_sent = false; // Track if ServerHello has been sent Loading