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.
119{
120 bool is_listener = !info.remote_address.empty() ? false
121 : (info.local_port > 0);
122
123 if (is_listener) {
124 auto listener = std::make_unique<ListenerState>(
m_context);
125 listener->info = info;
127
128 asio::error_code ec;
129 auto endpoint = asio::ip::tcp::endpoint(asio::ip::tcp::v4(), info.local_port);
130
131 if (listener->acceptor.open(endpoint.protocol(), ec)) {
133 "Failed to open TCP acceptor: {}", ec.message());
134 return 0;
135 }
136
137 if (listener->acceptor.set_option(asio::socket_base::reuse_address(true), ec)) {
139 "Failed to set SO_REUSEADDR on TCP acceptor: {}", ec.message());
140 return 0;
141 }
142
143 if (listener->acceptor.bind(endpoint, ec)) {
145 "Failed to bind TCP acceptor to port {}: {}", info.local_port, ec.message());
146 return 0;
147 }
148
149 if (listener->acceptor.listen(asio::socket_base::max_listen_connections, ec)) {
151 "Failed to listen on port {}: {}", info.local_port, ec.message());
152 return 0;
153 }
154
156
157 auto* raw = listener.get();
158
159 {
162 }
163
166 }
167
169
171 "TCP listener {} opened on port {}", info.id, info.local_port);
172
173 return info.id;
174 }
175
176 auto conn = std::make_unique<ConnectionState>(
m_context);
177 conn->info = info;
179 conn->is_outbound = true;
180
181 auto* raw = conn.get();
182
183 {
186 }
187
189
191 "TCP connection {} opening to {}:{}", info.id, info.remote_address, info.remote_port);
192
193 return info.id;
194}
#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.