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

◆ send()

bool MayaFlux::Core::TCPBackend::send ( uint64_t  endpoint_id,
const uint8_t *  data,
size_t  size 
)
overridevirtual

Send data through an endpoint.

Parameters
endpoint_idTarget endpoint.
dataPointer to payload bytes.
sizePayload size in bytes.
Returns
true if the send was accepted (queued or completed).

For UDP: sends a single datagram via sendto(). Non-blocking. For TCP: writes framed message. May block briefly if kernel send buffer is full. For SHM: writes into the shared segment.

Implements MayaFlux::Core::INetworkBackend.

Definition at line 286 of file TCPBackend.cpp.

287{
288 std::shared_lock lock(m_connections_mutex);
289 auto it = m_connections.find(endpoint_id);
290 if (it == m_connections.end()) {
291 return false;
292 }
293
294 auto& conn = *it->second;
295 if (conn.info.state != EndpointState::OPEN) {
296 return false;
297 }
298
299 auto frame = std::make_shared<std::vector<uint8_t>>(sizeof(uint32_t) + size);
300 uint32_t net_len = htonl(static_cast<uint32_t>(size));
301 std::memcpy(frame->data(), &net_len, sizeof(uint32_t));
302 std::memcpy(frame->data() + sizeof(uint32_t), data, size);
303
304 asio::async_write(
305 conn.socket,
306 asio::buffer(*frame),
307 [frame, endpoint_id, this](const asio::error_code& ec, size_t) {
308 if (ec) {
309 MF_WARN(Journal::Component::Core, Journal::Context::NetworkBackend,
310 "TCP write failed on endpoint {}: {}", endpoint_id, ec.message());
311
312 std::shared_lock lk(m_connections_mutex);
313 auto cit = m_connections.find(endpoint_id);
314 if (cit != m_connections.end()) {
315 on_connection_error(*cit->second, ec);
316 }
317 }
318 });
319
320 return true;
321}
bool size
std::unordered_map< uint64_t, std::unique_ptr< ConnectionState > > m_connections
std::shared_mutex m_connections_mutex

References m_connections, m_connections_mutex, MayaFlux::Core::OPEN, and size.