Open a new endpoint.
The subsystem assigns the id before calling this. The backend stores and uses it for subsequent operations.
For UDP: binds a local port and/or stores a default remote target. For TCP: connects to a remote host, or begins listening on a port. Outbound connect may block. Use co_open_endpoint on the service for non-blocking connect. For SHM: opens or creates a named shared memory segment.
115{
116 bool is_listener = !info.remote_address.empty() ? false
117 : (info.local_port > 0);
118
119 if (is_listener) {
120 auto listener = std::make_unique<ListenerState>(
m_context);
121 listener->info = info;
123
124 asio::error_code ec;
125 auto endpoint = asio::ip::tcp::endpoint(asio::ip::tcp::v4(), info.local_port);
126
127 if (listener->acceptor.open(endpoint.protocol(), ec)) {
129 "Failed to open TCP acceptor: {}", ec.message());
130 return 0;
131 }
132
133 if (listener->acceptor.set_option(asio::socket_base::reuse_address(true), ec)) {
135 "Failed to set SO_REUSEADDR on TCP acceptor: {}", ec.message());
136 return 0;
137 }
138
139 if (listener->acceptor.bind(endpoint, ec)) {
141 "Failed to bind TCP acceptor to port {}: {}", info.local_port, ec.message());
142 return 0;
143 }
144
145 if (listener->acceptor.listen(asio::socket_base::max_listen_connections, ec)) {
147 "Failed to listen on port {}: {}", info.local_port, ec.message());
148 return 0;
149 }
150
152
153 auto* raw = listener.get();
154
155 {
158 }
159
162 }
163
165
167 "TCP listener {} opened on port {}", info.id, info.local_port);
168
169 return info.id;
170 }
171
172 auto conn = std::make_unique<ConnectionState>(
m_context);
173 conn->info = info;
175 conn->is_outbound = true;
176
177 auto* raw = conn.get();
178
179 {
182 }
183
185
187 "TCP connection {} opening to {}:{}", info.id, info.remote_address, info.remote_port);
188
189 return info.id;
190}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
void start_connect(ConnectionState &conn)
Initiate async_connect for an outbound endpoint.
std::unordered_map< uint64_t, std::unique_ptr< ConnectionState > > m_connections
std::shared_mutex m_listeners_mutex
std::unordered_map< uint64_t, std::unique_ptr< ListenerState > > m_listeners
void start_accept(ListenerState &listener)
Initiate async_accept loop for a listener.
std::shared_mutex m_connections_mutex
asio::io_context & m_context
EndpointStateCallback m_state_callback
@ NetworkBackend
Network transport backend (UDP, TCP, SHM)
@ Core
Core engine, backend, subsystems.