12#include <system_error>
72 class quic_socket :
public std::enable_shared_from_this<quic_socket>
83 std::span<const uint8_t> data,
102 using close_callback = std::function<void(uint64_t error_code,
const std::string& reason)>;
162 [[nodiscard]]
auto connect(
const asio::ip::udp::endpoint& endpoint,
163 const std::string& server_name =
"") ->
VoidResult;
171 [[nodiscard]]
auto accept(
const std::string& cert_file,
180 [[nodiscard]]
auto close(uint64_t error_code = 0,
181 const std::string& reason =
"") ->
VoidResult;
208 std::vector<uint8_t>&& data,
237 [[nodiscard]]
auto is_connected() const noexcept ->
bool;
358 std::vector<protocols::quic::frame>&& frames) ->
VoidResult;
A QUIC socket that wraps UDP and integrates QUIC packet protection.
auto local_connection_id() const -> const protocols::quic::connection_id &
Get the local connection ID.
auto determine_encryption_level(const protocols::quic::packet_header &header) const noexcept -> protocols::quic::encryption_level
Determine encryption level from packet header.
std::function< void( uint64_t stream_id, std::span< const uint8_t > data, bool fin)> stream_data_callback
Callback for receiving stream data.
auto on_retransmit_timeout() -> void
Retransmission timeout handler.
auto remote_connection_id() const -> const protocols::quic::connection_id &
Get the remote connection ID.
stream_data_callback stream_data_cb_
Stream data callback.
std::function< void(uint64_t error_code, const std::string &reason)> close_callback
Callback when connection is closed.
std::atomic< quic_connection_state > state_
Connection state.
auto process_frame(const protocols::quic::frame &f) -> void
Process a parsed frame.
auto connect(const asio::ip::udp::endpoint &endpoint, const std::string &server_name="") -> VoidResult
Connect to a remote server (client only)
auto process_connection_close_frame(const protocols::quic::connection_close_frame &f) -> void
Process CONNECTION_CLOSE frame.
auto transition_state(quic_connection_state new_state) -> void
Transition to a new connection state.
auto socket() const -> const asio::ip::udp::socket &
Access the underlying UDP socket (const)
asio::ip::udp::socket udp_socket_
Underlying UDP socket.
std::array< uint64_t, 4 > next_packet_number_
Packet number for each encryption level.
std::mutex callback_mutex_
Mutex for callback protection.
std::atomic< bool > is_receiving_
Is receive loop running.
auto state() const noexcept -> quic_connection_state
Get the current connection state.
protocols::quic::connection_id remote_conn_id_
Remote connection ID.
auto send_stream_data(uint64_t stream_id, std::vector< uint8_t > &&data, bool fin=false) -> VoidResult
Send data on a stream.
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)
connected_callback connected_cb_
Connected callback.
auto process_handshake_done_frame() -> void
Process HANDSHAKE_DONE frame.
auto is_handshake_complete() const noexcept -> bool
Check if the TLS handshake is complete.
auto create_stream(bool unidirectional=false) -> Result< uint64_t >
Create a new stream.
auto queue_crypto_data(std::vector< uint8_t > &&data) -> void
Queue crypto data for sending.
std::atomic< bool > handshake_complete_
Is handshake complete.
std::array< uint8_t, 65536 > recv_buffer_
Receive buffer (max UDP datagram size)
auto socket() -> asio::ip::udp::socket &
Access the underlying UDP socket.
auto process_ack_frame(const protocols::quic::ack_frame &f) -> void
Process ACK frame.
auto send_pending_packets() -> void
Send pending outgoing packets.
auto process_stream_frame(const protocols::quic::stream_frame &f) -> void
Process STREAM frame data.
auto send_packet(protocols::quic::encryption_level level, std::vector< protocols::quic::frame > &&frames) -> VoidResult
Build and send a packet with frames.
auto role() const noexcept -> quic_role
Get the role (client or server)
quic_socket(asio::ip::udp::socket socket, quic_role role)
Constructs a QUIC socket.
error_callback error_cb_
Error callback.
auto remote_endpoint() const -> asio::ip::udp::endpoint
Get the remote endpoint.
quic_socket(const quic_socket &)=delete
auto handle_packet(std::span< const uint8_t > data) -> void
Handle received packet data.
auto do_receive() -> void
Internal receive loop implementation.
std::array< std::deque< std::vector< uint8_t > >, 4 > pending_crypto_data_
Pending crypto data to send per encryption level.
auto is_connected() const noexcept -> bool
Check if the connection is established.
std::function< void(std::error_code)> error_callback
Callback for error handling.
quic_role role_
Socket role (client/server)
uint64_t next_stream_id_
Next stream ID to allocate.
auto generate_connection_id() -> protocols::quic::connection_id
Generate a new connection ID.
protocols::quic::quic_crypto crypto_
QUIC crypto handler.
~quic_socket()
Destructor.
std::function< void()> connected_callback
Callback when connection is established.
auto close_stream(uint64_t stream_id) -> VoidResult
Close a stream.
protocols::quic::connection_id local_conn_id_
Local connection ID.
auto stop_receive() -> void
Stop the receive loop.
asio::ip::udp::endpoint remote_endpoint_
Remote endpoint.
auto set_stream_data_callback(stream_data_callback cb) -> void
Set callback for stream data reception.
asio::steady_timer retransmit_timer_
Retransmission timer.
auto start_receive() -> void
Start the receive loop.
asio::steady_timer idle_timer_
Idle timeout timer.
auto accept(const std::string &cert_file, const std::string &key_file) -> VoidResult
Accept an incoming connection (server only)
auto set_close_callback(close_callback cb) -> void
Set callback for connection close.
std::array< uint64_t, 4 > largest_received_pn_
Largest received packet number for each level.
std::mutex state_mutex_
Mutex for state protection.
quic_socket & operator=(const quic_socket &)=delete
auto set_connected_callback(connected_callback cb) -> void
Set callback for connection establishment.
auto process_crypto_frame(const protocols::quic::crypto_frame &f) -> void
Process CRYPTO frame data.
close_callback close_cb_
Close callback.
auto set_error_callback(error_callback cb) -> void
Set callback for errors.
QUIC Connection ID (RFC 9000 Section 5.1)
QUIC-TLS integration handler (RFC 9001)
quic_role
Role of the QUIC endpoint (client or server)
::kcenon::network::VoidResult VoidResult
quic_connection_state
QUIC connection state machine states.
@ handshake_start
Initiating handshake.
@ close
Connection close frame.
@ connected
Handshake complete, can send/receive data.
@ closed
Connection terminated.
@ closing
CONNECTION_CLOSE sent, waiting for timeout.
@ draining
CONNECTION_CLOSE received, draining period.
@ idle
Connection not yet started.
encryption_level
QUIC encryption levels (RFC 9001 Section 4)
@ handshake
Handshake encryption.
std::variant< padding_frame, ping_frame, ack_frame, reset_stream_frame, stop_sending_frame, crypto_frame, new_token_frame, stream_frame, max_data_frame, max_stream_data_frame, max_streams_frame, data_blocked_frame, stream_data_blocked_frame, streams_blocked_frame, new_connection_id_frame, retire_connection_id_frame, path_challenge_frame, path_response_frame, connection_close_frame, handshake_done_frame > frame
Variant type holding any QUIC frame.
std::variant< long_header, short_header > packet_header
Variant type for packet headers.
Network-specific error and result type definitions.
ACK frame (RFC 9000 Section 19.3)
CONNECTION_CLOSE frame (RFC 9000 Section 19.19)
CRYPTO frame (RFC 9000 Section 19.6)
STREAM frame (RFC 9000 Section 19.8)