MayaFlux 1.0.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 118 of file TCPBackend.cpp.

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;
126 listener->info.state = EndpointState::OPENING;
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
155 listener->info.state = EndpointState::OPEN;
156
157 auto* raw = listener.get();
158
159 {
160 std::unique_lock lock(m_listeners_mutex);
161 m_listeners[info.id] = std::move(listener);
162 }
163
164 if (m_state_callback) {
166 }
167
168 start_accept(*raw);
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;
178 conn->info.state = EndpointState::OPENING;
179 conn->is_outbound = true;
180
181 auto* raw = conn.get();
182
183 {
184 std::unique_lock lock(m_connections_mutex);
185 m_connections[info.id] = std::move(conn);
186 }
187
188 start_connect(*raw);
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.

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: