MayaFlux 0.4.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 282 of file TCPBackend.cpp.

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