From 00bf570b5197c5dfb388b7821899057b160e0b31 Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Thu, 29 Sep 2022 14:47:07 -0400 Subject: [PATCH] Simplify network messagees. We only send list data, so no need to specify if list or waves. --- src/DYio/DYMessage.h | 49 ++++++------------------------------------ src/DYio/TCPServer.cpp | 14 ++++++------ src/DYio/TCPServer.h | 6 +++--- 3 files changed, 17 insertions(+), 52 deletions(-) diff --git a/src/DYio/DYMessage.h b/src/DYio/DYMessage.h index 05f5053..0ee83c0 100644 --- a/src/DYio/DYMessage.h +++ b/src/DYio/DYMessage.h @@ -6,56 +6,21 @@ namespace Daqromancy { - enum class BSMessageType : uint64_t + struct DYMessage { - List, - Mixed, //Maybe someday hahaha - None - }; - - struct BSHeader - { - BSMessageType type = BSMessageType::None; - uint64_t size = 0; - }; - - struct BSMessage - { - BSMessage() = default; - BSMessage(const std::vector& data) + DYMessage() = default; + DYMessage(const std::vector& data) { - header.type = BSMessageType::List; + size = data.size() * Data::dataSize; + body.resize(size); for (const auto& datum : data) LoadDYDataToBuffer(body, datum); } - BSHeader header; + uint64_t size; //in bytes std::vector body; - - std::size_t Size() const - { - return header.size; - } - - //Nasty work. Convert header into a raw bytes array for transmission. Makes it so padding is no longer a concern. - std::vector GetHeaderRaw() const - { - std::vector rawBytes(sizeof(header.type) + sizeof(header.size)); - std::size_t position = 0; - int loopIndex; - - char* dataPointer = (char*)&header.type; - for (loopIndex = 0; loopIndex < sizeof(header.type); loopIndex++) - rawBytes[position++] = dataPointer[loopIndex]; - - dataPointer = (char*)&header.size; - for (loopIndex = 0; loopIndex < sizeof(header.size); loopIndex++) - rawBytes[position++] = dataPointer[loopIndex]; - - return rawBytes; - } }; } -#endif \ No newline at end of file +#endif diff --git a/src/DYio/TCPServer.cpp b/src/DYio/TCPServer.cpp index ab979cb..7a38263 100644 --- a/src/DYio/TCPServer.cpp +++ b/src/DYio/TCPServer.cpp @@ -16,7 +16,7 @@ namespace Daqromancy { that the loop of write calls and job submissions had stopped (or was not yet started), so we need to restart/start the cycle of writing data to the socket. */ - void TCPServerConnection::Send(const BSMessage& message) + void TCPServerConnection::Send(const DYMessage& message) { asio::post(m_contextRef, [this, message]() @@ -38,13 +38,13 @@ namespace Daqromancy { void TCPServerConnection::WriteHeader() { - std::vector headerData = m_queue.Front().GetHeaderRaw(); - asio::async_write(m_socket, asio::buffer(headerData, headerData.size()), + uint64_t messageSize = m_queue.Front().size; + asio::async_write(m_socket, asio::buffer(&messageSize, sizeof(messageSize)), [this](std::error_code ec, std::size_t length) //Callback upon completion { if (!ec) { - if (m_queue.Front().Size() > 0) + if (m_queue.Front().size > 0) WriteBody(); //submit next job to asio: write body data else { @@ -64,7 +64,7 @@ namespace Daqromancy { void TCPServerConnection::WriteBody() { - asio::async_write(m_socket, asio::buffer(m_queue.Front().body, m_queue.Front().Size()), + asio::async_write(m_socket, asio::buffer(m_queue.Front().body, m_queue.Front().size), [this](std::error_code ec, std::size_t length) { if (!ec) @@ -140,7 +140,7 @@ namespace Daqromancy { if (!m_dataHandle.dataQueue->IsEmpty()) continue; - BSMessage message(m_dataHandle.dataQueue->Front()); + DYMessage message(m_dataHandle.dataQueue->Front()); MessageClients(message); m_dataHandle.dataQueue->PopFront(); } @@ -159,7 +159,7 @@ namespace Daqromancy { m_dataHandle.dataQueue->ResetWakeup(); } - void TCPServer::MessageClients(const BSMessage& message) + void TCPServer::MessageClients(const DYMessage& message) { bool isAnyClientInvalid = false; std::scoped_lock guard(m_clientMutex); diff --git a/src/DYio/TCPServer.h b/src/DYio/TCPServer.h index 6069621..dc0f978 100644 --- a/src/DYio/TCPServer.h +++ b/src/DYio/TCPServer.h @@ -22,7 +22,7 @@ namespace Daqromancy { ~TCPServerConnection(); bool IsConnected() const { return m_socket.is_open(); } - void Send(const BSMessage& message); + void Send(const DYMessage& message); void Disconnect(); @@ -33,7 +33,7 @@ namespace Daqromancy { asio::ip::tcp::socket m_socket; asio::io_context& m_contextRef; - ThreadSafeQueue m_queue; + ThreadSafeQueue m_queue; }; /* @@ -86,7 +86,7 @@ namespace Daqromancy { private: - void MessageClients(const BSMessage& message); + void MessageClients(const DYMessage& message); void WaitForClient(); asio::io_context m_context;