|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
A QUIC socket that wraps UDP and integrates QUIC packet protection. More...
#include <quic_socket.h>


Public Types | |
| using | stream_data_callback |
| Callback for receiving stream data. | |
| using | connected_callback = std::function<void()> |
| Callback when connection is established. | |
| using | error_callback = std::function<void(std::error_code)> |
| Callback for error handling. | |
| using | close_callback = std::function<void(uint64_t error_code, const std::string& reason)> |
| Callback when connection is closed. | |
Public Member Functions | |
| quic_socket (asio::ip::udp::socket socket, quic_role role) | |
| Constructs a QUIC socket. | |
| ~quic_socket () | |
| Destructor. | |
| quic_socket (const quic_socket &)=delete | |
| quic_socket & | operator= (const quic_socket &)=delete |
| quic_socket (quic_socket &&other) noexcept | |
| quic_socket & | operator= (quic_socket &&other) noexcept |
| auto | set_stream_data_callback (stream_data_callback cb) -> void |
| Set callback for stream data reception. | |
| auto | set_connected_callback (connected_callback cb) -> void |
| Set callback for connection establishment. | |
| auto | set_error_callback (error_callback cb) -> void |
| Set callback for errors. | |
| auto | set_close_callback (close_callback cb) -> void |
| Set callback for connection close. | |
| auto | connect (const asio::ip::udp::endpoint &endpoint, const std::string &server_name="") -> VoidResult |
| Connect to a remote server (client only) | |
| auto | accept (const std::string &cert_file, const std::string &key_file) -> VoidResult |
| Accept an incoming connection (server only) | |
| auto | close (uint64_t error_code=0, const std::string &reason="") -> VoidResult |
| Close the connection gracefully. | |
| auto | start_receive () -> void |
| Start the receive loop. | |
| auto | stop_receive () -> void |
| Stop the receive loop. | |
| auto | send_stream_data (uint64_t stream_id, std::vector< uint8_t > &&data, bool fin=false) -> VoidResult |
| Send data on a stream. | |
| auto | create_stream (bool unidirectional=false) -> Result< uint64_t > |
| Create a new stream. | |
| auto | close_stream (uint64_t stream_id) -> VoidResult |
| Close a stream. | |
| auto | is_connected () const noexcept -> bool |
| Check if the connection is established. | |
| auto | is_handshake_complete () const noexcept -> bool |
| Check if the TLS handshake is complete. | |
| auto | state () const noexcept -> quic_connection_state |
| Get the current connection state. | |
| auto | role () const noexcept -> quic_role |
| Get the role (client or server) | |
| auto | remote_endpoint () const -> asio::ip::udp::endpoint |
| Get the remote endpoint. | |
| auto | local_connection_id () const -> const protocols::quic::connection_id & |
| Get the local connection ID. | |
| auto | remote_connection_id () const -> const protocols::quic::connection_id & |
| Get the remote connection ID. | |
| auto | socket () -> asio::ip::udp::socket & |
| Access the underlying UDP socket. | |
| auto | socket () const -> const asio::ip::udp::socket & |
| Access the underlying UDP socket (const) | |
Private Member Functions | |
| auto | do_receive () -> void |
| Internal receive loop implementation. | |
| auto | handle_packet (std::span< const uint8_t > data) -> void |
| Handle received packet data. | |
| auto | process_frame (const protocols::quic::frame &f) -> void |
| Process a parsed frame. | |
| auto | process_crypto_frame (const protocols::quic::crypto_frame &f) -> void |
| Process CRYPTO frame data. | |
| auto | process_stream_frame (const protocols::quic::stream_frame &f) -> void |
| Process STREAM frame data. | |
| auto | process_ack_frame (const protocols::quic::ack_frame &f) -> void |
| Process ACK frame. | |
| auto | process_connection_close_frame (const protocols::quic::connection_close_frame &f) -> void |
| Process CONNECTION_CLOSE frame. | |
| auto | process_handshake_done_frame () -> void |
| Process HANDSHAKE_DONE frame. | |
| auto | send_pending_packets () -> void |
| Send pending outgoing packets. | |
| auto | send_packet (protocols::quic::encryption_level level, std::vector< protocols::quic::frame > &&frames) -> VoidResult |
| Build and send a packet with frames. | |
| auto | queue_crypto_data (std::vector< uint8_t > &&data) -> void |
| Queue crypto data for sending. | |
| auto | determine_encryption_level (const protocols::quic::packet_header &header) const noexcept -> protocols::quic::encryption_level |
| Determine encryption level from packet header. | |
| auto | generate_connection_id () -> protocols::quic::connection_id |
| Generate a new connection ID. | |
| auto | on_retransmit_timeout () -> void |
| Retransmission timeout handler. | |
| auto | transition_state (quic_connection_state new_state) -> void |
| Transition to a new connection state. | |
Private Attributes | |
| asio::ip::udp::socket | udp_socket_ |
| Underlying UDP socket. | |
| asio::ip::udp::endpoint | remote_endpoint_ |
| Remote endpoint. | |
| std::array< uint8_t, 65536 > | recv_buffer_ |
| Receive buffer (max UDP datagram size) | |
| quic_role | role_ |
| Socket role (client/server) | |
| std::atomic< quic_connection_state > | state_ {quic_connection_state::idle} |
| Connection state. | |
| protocols::quic::quic_crypto | crypto_ |
| QUIC crypto handler. | |
| protocols::quic::connection_id | local_conn_id_ |
| Local connection ID. | |
| protocols::quic::connection_id | remote_conn_id_ |
| Remote connection ID. | |
| std::array< uint64_t, 4 > | next_packet_number_ {0, 0, 0, 0} |
| Packet number for each encryption level. | |
| std::array< uint64_t, 4 > | largest_received_pn_ {0, 0, 0, 0} |
| Largest received packet number for each level. | |
| uint64_t | next_stream_id_ {0} |
| Next stream ID to allocate. | |
| std::array< std::deque< std::vector< uint8_t > >, 4 > | pending_crypto_data_ |
| Pending crypto data to send per encryption level. | |
| std::map< uint64_t, std::deque< std::pair< std::vector< uint8_t >, bool > > > | pending_stream_data_ |
| Pending stream data to send (stream_id -> data queue) | |
| std::mutex | callback_mutex_ |
| Mutex for callback protection. | |
| stream_data_callback | stream_data_cb_ |
| Stream data callback. | |
| connected_callback | connected_cb_ |
| Connected callback. | |
| error_callback | error_cb_ |
| Error callback. | |
| close_callback | close_cb_ |
| Close callback. | |
| std::atomic< bool > | is_receiving_ {false} |
| Is receive loop running. | |
| std::atomic< bool > | handshake_complete_ {false} |
| Is handshake complete. | |
| asio::steady_timer | retransmit_timer_ |
| Retransmission timer. | |
| asio::steady_timer | idle_timer_ |
| Idle timeout timer. | |
| std::mutex | state_mutex_ |
| Mutex for state protection. | |
A QUIC socket that wraps UDP and integrates QUIC packet protection.
asio::ip::udp::socket for UDP I/ODefinition at line 72 of file quic_socket.h.
| using kcenon::network::internal::quic_socket::close_callback = std::function<void(uint64_t error_code, const std::string& reason)> |
Callback when connection is closed.
| error_code | The QUIC error code |
| reason | Human-readable reason string |
Definition at line 102 of file quic_socket.h.
| using kcenon::network::internal::quic_socket::connected_callback = std::function<void()> |
Callback when connection is established.
Definition at line 89 of file quic_socket.h.
| using kcenon::network::internal::quic_socket::error_callback = std::function<void(std::error_code)> |
Callback for error handling.
| ec | The error code describing the error |
Definition at line 95 of file quic_socket.h.
Callback for receiving stream data.
| stream_id | The stream identifier |
| data | The received data |
| fin | True if this is the final data on the stream |
Definition at line 81 of file quic_socket.h.
| kcenon::network::internal::quic_socket::quic_socket | ( | asio::ip::udp::socket | socket, |
| quic_role | role ) |
Constructs a QUIC socket.
| socket | An open UDP socket |
| role | Client or server role |
Definition at line 20 of file quic_socket.cpp.
References kcenon::network::internal::client, generate_connection_id(), local_conn_id_, next_stream_id_, and role_.

| kcenon::network::internal::quic_socket::~quic_socket | ( | ) |
Destructor.
Definition at line 35 of file quic_socket.cpp.
References idle_timer_, retransmit_timer_, and stop_receive().

|
delete |
|
noexcept |
Definition at line 44 of file quic_socket.cpp.
|
nodiscard |
Accept an incoming connection (server only)
| cert_file | Path to certificate file (PEM format) |
| key_file | Path to private key file (PEM format) |
Definition at line 213 of file quic_socket.cpp.
References kcenon::network::error_codes::network_system::connection_failed, kcenon::network::error_void(), kcenon::network::internal::handshake_start, kcenon::network::internal::idle, kcenon::network::error_codes::common_errors::invalid_argument, kcenon::network::ok(), and kcenon::network::internal::server.

|
nodiscard |
Close the connection gracefully.
| error_code | Application error code (0 for no error) |
| reason | Human-readable reason string |
Definition at line 251 of file quic_socket.cpp.
References kcenon::network::internal::closed, kcenon::network::internal::closing, kcenon::network::internal::draining, kcenon::network::protocols::quic::connection_close_frame::error_code, kcenon::network::protocols::quic::connection_close_frame::is_application_error, kcenon::network::ok(), and kcenon::network::protocols::quic::connection_close_frame::reason_phrase.

|
nodiscard |
Close a stream.
| stream_id | Stream to close |
Definition at line 377 of file quic_socket.cpp.
References kcenon::network::error_void(), kcenon::network::error_codes::common_errors::not_found, and kcenon::network::ok().

|
nodiscard |
Connect to a remote server (client only)
| endpoint | Remote endpoint to connect to |
| server_name | Server name for SNI (TLS) |
Definition at line 135 of file quic_socket.cpp.
References kcenon::network::internal::client, kcenon::network::internal::closed, kcenon::network::error_codes::network_system::connection_failed, kcenon::network::error_void(), kcenon::network::internal::handshake, kcenon::network::internal::handshake_start, kcenon::network::internal::idle, kcenon::network::error_codes::common_errors::invalid_argument, and kcenon::network::ok().

|
nodiscard |
Create a new stream.
| unidirectional | True for unidirectional stream |
Definition at line 341 of file quic_socket.cpp.
References kcenon::network::error_codes::network_system::connection_failed, kcenon::network::protocols::quic::error, kcenon::network::ok(), and kcenon::network::internal::server.

|
nodiscardprivatenoexcept |
Determine encryption level from packet header.
| header | Packet header |
Definition at line 905 of file quic_socket.cpp.
|
private |
Internal receive loop implementation.
Definition at line 441 of file quic_socket.cpp.
|
nodiscardprivate |
Generate a new connection ID.
Definition at line 930 of file quic_socket.cpp.
Referenced by quic_socket().

|
private |
Handle received packet data.
| data | Raw packet bytes |
Definition at line 485 of file quic_socket.cpp.
References kcenon::network::protocols::quic::packet_number::decode(), kcenon::network::internal::handshake, kcenon::network::internal::handshake_start, kcenon::network::protocols::quic::hp_sample_size, kcenon::network::protocols::quic::frame_parser::parse_all(), kcenon::network::protocols::quic::packet_parser::parse_header(), kcenon::network::internal::server, kcenon::network::protocols::quic::packet_protection::unprotect(), and kcenon::network::protocols::quic::packet_protection::unprotect_header().

|
nodiscardnoexcept |
Check if the connection is established.
Definition at line 402 of file quic_socket.cpp.
References kcenon::network::internal::connected, and state_.
|
nodiscardnoexcept |
Check if the TLS handshake is complete.
Definition at line 407 of file quic_socket.cpp.
References handshake_complete_.
|
nodiscard |
Get the local connection ID.
Definition at line 427 of file quic_socket.cpp.
References local_conn_id_.
|
private |
Retransmission timeout handler.
Definition at line 945 of file quic_socket.cpp.
|
delete |
|
noexcept |
Definition at line 69 of file quic_socket.cpp.
|
private |
Process ACK frame.
| f | ACK frame |
Definition at line 715 of file quic_socket.cpp.
|
private |
Process CONNECTION_CLOSE frame.
| f | Connection close frame |
Definition at line 722 of file quic_socket.cpp.
References kcenon::network::internal::closed, and kcenon::network::internal::draining.
|
private |
Process CRYPTO frame data.
| f | Crypto frame |
Definition at line 653 of file quic_socket.cpp.
References kcenon::network::internal::client, and kcenon::network::internal::connected.
|
private |
Process a parsed frame.
| f | Frame to process |
Definition at line 616 of file quic_socket.cpp.
|
private |
Process HANDSHAKE_DONE frame.
Definition at line 745 of file quic_socket.cpp.
References kcenon::network::internal::client, and kcenon::network::internal::connected.
|
private |
Process STREAM frame data.
| f | Stream frame |
Definition at line 706 of file quic_socket.cpp.
|
private |
Queue crypto data for sending.
| data | Crypto data to send |
Definition at line 896 of file quic_socket.cpp.
|
nodiscard |
Get the remote connection ID.
Definition at line 432 of file quic_socket.cpp.
References remote_conn_id_.
|
nodiscard |
Get the remote endpoint.
Definition at line 422 of file quic_socket.cpp.
References remote_endpoint_.
|
nodiscardnoexcept |
Get the role (client or server)
Definition at line 417 of file quic_socket.cpp.
References role_.
|
nodiscardprivate |
Build and send a packet with frames.
| level | Encryption level |
| frames | Frames to include |
Definition at line 816 of file quic_socket.cpp.
References kcenon::network::protocols::quic::frame_builder::build(), kcenon::network::protocols::quic::packet_builder::build_handshake(), kcenon::network::protocols::quic::packet_builder::build_initial(), kcenon::network::protocols::quic::packet_builder::build_short(), kcenon::network::error_void(), kcenon::network::error_codes::common_errors::internal_error, kcenon::network::error_codes::common_errors::not_initialized, kcenon::network::ok(), and kcenon::network::protocols::quic::packet_protection::protect().

|
private |
Send pending outgoing packets.
Definition at line 761 of file quic_socket.cpp.
References kcenon::network::internal::closed, kcenon::network::protocols::quic::crypto_frame::data, kcenon::network::protocols::quic::stream_frame::data, kcenon::network::protocols::quic::stream_frame::fin, kcenon::network::internal::idle, kcenon::network::protocols::quic::crypto_frame::offset, kcenon::network::protocols::quic::stream_frame::offset, and kcenon::network::protocols::quic::stream_frame::stream_id.
|
nodiscard |
Send data on a stream.
| stream_id | Target stream ID |
| data | Data to send (moved for efficiency) |
| fin | True if this is the final data on the stream |
Definition at line 315 of file quic_socket.cpp.
References kcenon::network::error_codes::network_system::connection_failed, kcenon::network::error_void(), and kcenon::network::ok().

| auto kcenon::network::internal::quic_socket::set_close_callback | ( | close_callback | cb | ) | -> void |
Set callback for connection close.
| cb | Callback function |
Definition at line 125 of file quic_socket.cpp.
| auto kcenon::network::internal::quic_socket::set_connected_callback | ( | connected_callback | cb | ) | -> void |
Set callback for connection establishment.
| cb | Callback function |
Definition at line 113 of file quic_socket.cpp.
| auto kcenon::network::internal::quic_socket::set_error_callback | ( | error_callback | cb | ) | -> void |
Set callback for errors.
| cb | Callback function |
Definition at line 119 of file quic_socket.cpp.
| auto kcenon::network::internal::quic_socket::set_stream_data_callback | ( | stream_data_callback | cb | ) | -> void |
Set callback for stream data reception.
| cb | Callback function |
Definition at line 107 of file quic_socket.cpp.
|
inline |
Access the underlying UDP socket.
Definition at line 285 of file quic_socket.h.
References udp_socket_.
|
inline |
Access the underlying UDP socket (const)
Definition at line 291 of file quic_socket.h.
References udp_socket_.
| auto kcenon::network::internal::quic_socket::start_receive | ( | ) | -> void |
Start the receive loop.
Once called, the socket continuously receives UDP datagrams, processes QUIC packets, and invokes appropriate callbacks.
Definition at line 304 of file quic_socket.cpp.
|
nodiscardnoexcept |
Get the current connection state.
Definition at line 412 of file quic_socket.cpp.
References state_.
| auto kcenon::network::internal::quic_socket::stop_receive | ( | ) | -> void |
Stop the receive loop.
Definition at line 310 of file quic_socket.cpp.
Referenced by ~quic_socket().

|
private |
Transition to a new connection state.
| new_state | New state |
Definition at line 952 of file quic_socket.cpp.
|
mutableprivate |
Mutex for callback protection.
Definition at line 445 of file quic_socket.h.
|
private |
Close callback.
Definition at line 457 of file quic_socket.h.
|
private |
Connected callback.
Definition at line 451 of file quic_socket.h.
|
private |
QUIC crypto handler.
Definition at line 413 of file quic_socket.h.
|
private |
Error callback.
Definition at line 454 of file quic_socket.h.
|
private |
Is handshake complete.
Definition at line 467 of file quic_socket.h.
Referenced by is_handshake_complete().
|
private |
|
private |
|
private |
Largest received packet number for each level.
Definition at line 425 of file quic_socket.h.
|
private |
Local connection ID.
Definition at line 416 of file quic_socket.h.
Referenced by local_connection_id(), and quic_socket().
|
private |
Packet number for each encryption level.
Definition at line 422 of file quic_socket.h.
|
private |
Next stream ID to allocate.
Definition at line 428 of file quic_socket.h.
Referenced by quic_socket().
|
private |
Pending crypto data to send per encryption level.
Definition at line 435 of file quic_socket.h.
|
private |
Pending stream data to send (stream_id -> data queue)
Definition at line 438 of file quic_socket.h.
|
private |
Receive buffer (max UDP datagram size)
Definition at line 404 of file quic_socket.h.
|
private |
Remote connection ID.
Definition at line 419 of file quic_socket.h.
Referenced by remote_connection_id().
|
private |
|
private |
|
private |
Socket role (client/server)
Definition at line 407 of file quic_socket.h.
Referenced by quic_socket(), and role().
|
private |
Connection state.
Definition at line 410 of file quic_socket.h.
Referenced by is_connected(), and state().
|
mutableprivate |
Mutex for state protection.
Definition at line 480 of file quic_socket.h.
|
private |
Stream data callback.
Definition at line 448 of file quic_socket.h.
|
private |
Underlying UDP socket.
Definition at line 398 of file quic_socket.h.