11#if defined(_WIN32) || defined(_WIN64)
15#include <asio/io_context.hpp>
16#include <asio/steady_timer.hpp>
18#if defined(_WIN32) || defined(_WIN64)
34 return StreamFramer { std::in_place_type<Transitive::Protocol::LineFramer>,
38 return StreamFramer { std::in_place_type<Transitive::Protocol::RawChunkFramer> };
40 return StreamFramer { std::in_place_type<Transitive::Protocol::LengthPrefixFramer>,
47 template <
typename Transport>
49 { Transport::open(stream, info) } -> std::same_as<asio::error_code>;
55 template <
typename Transport>
57 { Transport::settle_time(info) } -> std::convertible_to<std::chrono::milliseconds>;
63 template <
typename Transport>
65 Transport::ready(stream, info);
68 template <
typename Transport>
69 concept HasControlLines =
requires(
typename Transport::Stream& stream, std::optional<bool> level) {
70 { Transport::set_control_lines(stream, level, level) } -> std::same_as<bool>;
73 template <
typename Transport>
74 concept HasBreak =
requires(
typename Transport::Stream& stream) {
75 { Transport::send_break(stream) } -> std::same_as<bool>;
81 template <
typename Transport,
bool = Transport::listens>
83 typename Transport::Protocol::acceptor
acceptor;
85 typename Transport::Bound
bound;
94 template <
typename Transport>
157template <
typename Transport>
160 using Config =
typename Transport::Config;
184 [[nodiscard]] std::string
get_name()
const override {
return std::string(Transport::name); }
185 [[nodiscard]] std::string
get_version()
const override {
return "1.0"; }
192 bool send(uint64_t endpoint_id,
const uint8_t* data,
size_t size)
override;
193 bool send_to(uint64_t endpoint_id,
const uint8_t* data,
size_t size,
194 const std::string& address, uint16_t port)
override;
198 bool set_control_lines(uint64_t endpoint_id, std::optional<bool> dtr, std::optional<bool> rts)
override;
218 using Stream =
typename Transport::Stream;
227 using Frame = std::shared_ptr<const std::vector<uint8_t>>;
246 ,
framer(
detail::make_stream_framer(framing, max_message_size))
253 copy.state =
state.load();
Abstract interface for network transport backends.
ListenerPtr find_listener(uint64_t endpoint_id) const
void schedule_reconnect(const ConnectionPtr &conn)
std::function< uint64_t()> m_allocate_endpoint_id
void shutdown() override
Release all resources, close all endpoints.
Memory::FlatMap< uint64_t, ListenerPtr > ListenerTable
Memory::SnapshotCell< ConnectionTable > m_connections
void on_connection_error(const ConnectionPtr &conn, const asio::error_code &ec)
void set_receive_callback(NetworkReceiveCallback callback) override
Register the receive callback.
ConnectionPtr find_connection(uint64_t endpoint_id) const
void start() override
Start receive threads and accept connections.
EndpointStateCallback m_state_callback
typename SerialTransport::Config Config
bool send_break(uint64_t endpoint_id) override
Hold the transmit line of an open serial endpoint in break.
StreamBackend(const Config &config, asio::io_context &context)
Construct with config and a reference to the shared io_context.
void cancel_timers(ConnectionState &conn)
StreamBackend(const StreamBackend &)=delete
NetworkTransport get_transport() const override
void close_listener(ListenerState &listener)
bool is_initialized() const override
~StreamBackend() override
std::shared_ptr< ConnectionState > ConnectionPtr
void transition_state(ConnectionState &conn, EndpointState new_state)
Memory::SnapshotCell< ListenerTable > m_listeners
StreamBackend & operator=(const StreamBackend &)=delete
void drop_queued_writes(ConnectionState &conn)
typename SerialTransport::Stream Stream
StreamBackend & operator=(StreamBackend &&)=delete
void start_write(const ConnectionPtr &conn)
static constexpr FramingPolicy resolve_framing(FramingPolicy framing)
static constexpr size_t k_default_read_size
bool is_running() const override
void start_receive_chain(const ConnectionPtr &conn)
void enqueue_write(const ConnectionPtr &conn, ConnectionState::Frame frame)
void set_endpoint_id_allocator(std::function< uint64_t()> allocator) override
Set the endpoint id allocator (called by NetworkSubsystem).
bool send_to(uint64_t endpoint_id, const uint8_t *data, size_t size, const std::string &address, uint16_t port) override
Send data to a specific address through an endpoint.
void close_endpoint(uint64_t endpoint_id) override
Close an endpoint and release its resources.
size_t get_write_backlog(uint64_t endpoint_id) const override
Bytes accepted by send and not yet written to the peer.
std::shared_ptr< ListenerState > ListenerPtr
StreamBackend(StreamBackend &&)=delete
void on_bytes_received(const ConnectionPtr &conn, const asio::error_code &ec, size_t bytes)
EndpointState get_endpoint_state(uint64_t endpoint_id) const override
Query the current state of an endpoint.
std::string get_version() const override
void stop() override
Stop receive threads without releasing resources.
std::string get_name() const override
bool initialize() override
Initialise backend resources (sockets, SHM segments, etc.).
std::vector< EndpointInfo > get_endpoints() const override
List all endpoints currently managed by this backend.
NetworkReceiveCallback m_receive_callback
asio::io_context & m_context
void start_connect(const ConnectionPtr &conn)
std::atomic< bool > m_initialized
Memory::FlatMap< uint64_t, ConnectionPtr > ConnectionTable
bool set_control_lines(uint64_t endpoint_id, std::optional< bool > dtr, std::optional< bool > rts) override
Set the DTR and RTS lines of an open serial endpoint.
bool send(uint64_t endpoint_id, const uint8_t *data, size_t size) override
Send data through an endpoint.
void set_state_callback(EndpointStateCallback callback) override
Register the endpoint state change callback.
void on_connect_complete(const ConnectionPtr &conn, const asio::error_code &ec)
void start_accept(const ListenerPtr &listener)
void finish_open(const ConnectionPtr &conn)
std::atomic< bool > m_running
uint64_t open_endpoint(const EndpointInfo &info) override
Open a new endpoint.
Map kept as one sorted vector, cheap to copy.
Lock-free cell holding one immutable snapshot of a value, read without blocking.
Messages with a four byte big-endian length before each payload.
Splits a byte stream into lines, whatever the chunking.
A transport that prepares the stream just before the endpoint reports OPEN.
A synchronous-open transport that holds the endpoint in OPENING for a while after the open.
A transport whose stream opens in one synchronous call instead of async_connect.
std::variant< Transitive::Protocol::LengthPrefixFramer, Transitive::Protocol::LineFramer, Transitive::Protocol::RawChunkFramer > StreamFramer
StreamFramer make_stream_framer(FramingPolicy framing, size_t max_message_size)
std::function< void( uint64_t endpoint_id, const uint8_t *data, size_t size, std::string_view sender_addr)> NetworkReceiveCallback
Callback signature for inbound data on an endpoint.
EndpointState
Observable connection state for an endpoint.
NetworkTransport
Identifies the transport protocol a backend implements.
std::function< void( const EndpointInfo &info, EndpointState previous, EndpointState current)> EndpointStateCallback
Callback signature for endpoint state changes.
FramingPolicy
How messages are delimited on the wire.
constexpr size_t default_max_line
constexpr size_t default_max_frame
Describes one logical send/receive endpoint managed by a backend.
std::atomic< bool > closed
std::shared_ptr< const std::vector< uint8_t > > Frame
ConnectionState(asio::io_context &ctx, FramingPolicy framing, size_t max_message_size, size_t read_size)
std::unique_ptr< asio::steady_timer > reconnect_timer
const Frame & front() const
EndpointInfo snapshot() const
std::unique_ptr< asio::steady_timer > settle_timer
std::atomic< size_t > pending_bytes
detail::StreamFramer framer
std::atomic< EndpointState > state
std::vector< Frame > outbox
void push(Frame frame)
The outbox is a vector with a head index, not a deque: it is touched only by the io thread,...
std::vector< uint8_t > read_buffer
detail::AcceptSide< Transport > accept
ListenerState(asio::io_context &ctx)
std::atomic< bool > closed
AcceptSide(asio::io_context &)
Transport::Protocol::acceptor acceptor
AcceptSide(asio::io_context &ctx)
Transport::Protocol::socket pending_socket
What a listener holds, which a transport without listeners does not have.
The framer for raw streams: every push is one message, with nothing to reassemble.