15 std::string_view session_id)
16 : session_id_(session_id), socket_(std::move(socket))
27 auto result =
close(0);
47 return socket_->remote_endpoint();
54 return is_active_.load(std::memory_order_relaxed);
59 if (!is_active_.load())
62 "Session is not active",
66 std::lock_guard<std::mutex> lock(socket_mutex_);
77 return socket_->send_stream_data(default_stream_id_, std::move(data),
false);
82 std::vector<uint8_t> bytes(data.begin(), data.end());
83 return send(std::move(bytes));
87 std::vector<uint8_t>&& data,
90 if (!is_active_.load())
93 "Session is not active",
97 std::lock_guard<std::mutex> lock(socket_mutex_);
107 return socket_->send_stream_data(stream_id, std::move(data), fin);
112 if (!is_active_.load())
115 "Session is not active",
119 std::lock_guard<std::mutex> lock(socket_mutex_);
127 return socket_->create_stream(
false);
132 if (!is_active_.load())
135 "Session is not active",
139 std::lock_guard<std::mutex> lock(socket_mutex_);
147 return socket_->create_stream(
true);
152 if (!is_active_.load())
155 "Session is not active",
159 std::lock_guard<std::mutex> lock(socket_mutex_);
167 return socket_->close_stream(stream_id);
172 if (!is_active_.exchange(
false))
182 std::lock_guard<std::mutex> lock(socket_mutex_);
185 result = socket_->close(error_code,
"Session closed");
191 std::lock_guard<std::mutex> lock(callback_mutex_);
198 catch (
const std::exception& e)
201 + std::string(e.what()));
216 std::function<
void(
const std::vector<uint8_t>&)> callback) ->
void
218 std::lock_guard<std::mutex> lock(callback_mutex_);
219 receive_callback_ = std::move(callback);
223 std::function<
void(uint64_t,
const std::vector<uint8_t>&,
bool)> callback)
226 std::lock_guard<std::mutex> lock(callback_mutex_);
227 stream_receive_callback_ = std::move(callback);
232 std::lock_guard<std::mutex> lock(callback_mutex_);
233 close_callback_ = std::move(callback);
238 if (!is_active_.load())
243 auto weak_self = weak_from_this();
245 std::lock_guard<std::mutex> lock(socket_mutex_);
252 socket_->set_stream_data_callback(
253 [weak_self](uint64_t stream_id, std::span<const uint8_t> data,
bool fin)
255 if (
auto self = weak_self.lock())
257 self->on_stream_data(stream_id, data, fin);
262 socket_->set_error_callback([weak_self](std::error_code ec)
264 if (
auto self = weak_self.lock())
271 socket_->set_close_callback(
272 [weak_self](uint64_t error_code,
const std::string& reason)
274 if (
auto self = weak_self.lock())
276 self->on_close(error_code, reason);
287 std::lock_guard<std::mutex> lock(socket_mutex_);
298 std::lock_guard<std::mutex> lock(socket_mutex_);
303 return socket_->local_connection_id() == conn_id ||
304 socket_->remote_connection_id() == conn_id;
308 std::span<const uint8_t> data,
311 if (!is_active_.load())
320 +
" bytes on stream " + std::to_string(stream_id));
322 std::vector<uint8_t> data_copy(data.begin(), data.end());
324 std::lock_guard<std::mutex> lock(callback_mutex_);
327 if (stream_receive_callback_)
331 stream_receive_callback_(stream_id, data_copy, fin);
333 catch (
const std::exception& e)
336 "[quic_session] Exception in stream receive callback: "
337 + std::string(e.what()));
342 if (stream_id == default_stream_id_ && receive_callback_)
346 receive_callback_(data_copy);
348 catch (
const std::exception& e)
351 + std::string(e.what()));
362 auto result = close(1);
370 + reason +
" (code: " + std::to_string(error_code) +
")");
372 is_active_.store(
false);
376 std::lock_guard<std::mutex> lock(callback_mutex_);
383 catch (
const std::exception& e)
386 + std::string(e.what()));
static void report_bytes_received(size_t bytes)
Report bytes received.
static void report_bytes_sent(size_t bytes)
Report bytes sent.
QUIC Connection ID (RFC 9000 Section 5.1)
auto close() -> void override
Closes the session (interface version).
auto close_stream(uint64_t stream_id) -> VoidResult override
Closes a stream (interface version).
auto matches_connection_id(const protocols::quic::connection_id &conn_id) const -> bool
Check if this session matches a connection ID.
auto set_receive_callback(std::function< void(const std::vector< uint8_t > &)> callback) -> void
Set callback for received data on the default stream.
auto on_error(std::error_code ec) -> void
auto handle_packet(std::span< const uint8_t > data) -> void
Handle incoming packet (called by server).
auto create_stream() -> Result< uint64_t > override
Creates a new bidirectional stream (interface version).
auto set_stream_receive_callback(std::function< void(uint64_t, const std::vector< uint8_t > &, bool)> callback) -> void
Set callback for stream data reception.
std::atomic< bool > is_active_
quic_session(std::shared_ptr< internal::quic_socket > socket, std::string_view session_id)
Constructs a QUIC session with an existing socket.
auto send_on_stream(uint64_t stream_id, std::vector< uint8_t > &&data, bool fin=false) -> VoidResult override
Sends data on a specific stream (interface version).
~quic_session() noexcept
Destructor; closes the session if still active.
auto create_unidirectional_stream() -> Result< uint64_t > override
Creates a new unidirectional stream (interface version).
auto on_stream_data(uint64_t stream_id, std::span< const uint8_t > data, bool fin) -> void
auto session_id() const -> const std::string &
Get the unique session identifier.
auto start_session() -> void
Start receiving data (called by server).
auto remote_endpoint() const -> asio::ip::udp::endpoint
Get the remote endpoint address.
auto is_active() const noexcept -> bool
Check if the session is currently active.
auto send(std::string_view data) -> VoidResult
Send string data on the default stream.
std::shared_ptr< internal::quic_socket > socket_
auto stats() const -> core::quic_connection_stats
Get connection statistics.
auto set_close_callback(std::function< void()> callback) -> void
Set callback when session closes.
auto on_close(uint64_t error_code, const std::string &reason) -> void
Logger system integration interface for network_system.
#define NETWORK_LOG_INFO(msg)
#define NETWORK_LOG_ERROR(msg)
#define NETWORK_LOG_DEBUG(msg)
constexpr int connection_closed
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
Network system metrics definitions and reporting utilities.
QUIC-specific session with stream multiplexing.
Statistics for a QUIC connection.