MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open_endpoint()

uint64_t MayaFlux::Core::TCPBackend::open_endpoint ( const EndpointInfo info)
overridevirtual

Open a new endpoint.

Parameters
infoEndpoint configuration with id pre-assigned by subsystem.
Returns
The endpoint id on success, or 0 on failure.

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.

Implements MayaFlux::Core::INetworkBackend.

Definition at line 114 of file TCPBackend.cpp.

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;
122 listener->info.state = EndpointState::OPENING;
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
151 listener->info.state = EndpointState::OPEN;
152
153 auto* raw = listener.get();
154
155 {
156 std::unique_lock lock(m_listeners_mutex);
157 m_listeners[info.id] = std::move(listener);
158 }
159
160 if (m_state_callback) {
162 }
163
164 start_accept(*raw);
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;
174 conn->info.state = EndpointState::OPENING;
175 conn->is_outbound = true;
176
177 auto* raw = conn.get();
178
179 {
180 std::unique_lock lock(m_connections_mutex);
181 m_connections[info.id] = std::move(conn);
182 }
183
184 start_connect(*raw);
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.

References MayaFlux::Journal::Core, MayaFlux::Core::EndpointInfo::id, MayaFlux::Core::EndpointInfo::local_port, m_connections, m_connections_mutex, m_context, m_listeners, m_listeners_mutex, m_state_callback, MF_ERROR, MF_INFO, MayaFlux::Journal::NetworkBackend, MayaFlux::Core::OPEN, MayaFlux::Core::OPENING, MayaFlux::Core::EndpointInfo::remote_address, MayaFlux::Core::EndpointInfo::remote_port, start_accept(), start_connect(), and MayaFlux::Core::EndpointInfo::state.

+ Here is the call graph for this function: