From 216520c2b9196cdd30ffe50387693a7fa34cf730 Mon Sep 17 00:00:00 2001 From: Mateusz Daniluk <121170681+VeithMetro@users.noreply.github.com> Date: Fri, 20 Sep 2024 12:26:51 +0200 Subject: [PATCH] [MessageControl] Fixing UDP output and allowing it to store more than one message (#315) * Reviving the UDP functionality of MessageControl plugin * Removing unnecessary comments and adding the size as a static constexpr --- MessageControl/MessageControl.cpp | 2 +- MessageControl/MessageOutput.cpp | 24 +++++++++++++----------- MessageControl/MessageOutput.h | 9 ++++++--- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/MessageControl/MessageControl.cpp b/MessageControl/MessageControl.cpp index a9e39435..02774b35 100644 --- a/MessageControl/MessageControl.cpp +++ b/MessageControl/MessageControl.cpp @@ -114,7 +114,7 @@ namespace Thunder { Announce(new Publishers::FileOutput(abbreviate, _config.FileName.Value())); } if ((_config.Remote.Binding.Value().empty() == false) && (_config.Remote.Port.Value() != 0)) { - Announce(new Publishers::UDPOutput(Core::NodeId(_config.Remote.NodeId()))); + Announce(new Publishers::UDPOutput(abbreviate, Core::NodeId(_config.Remote.NodeId()))); } _webSocketExporter.Initialize(service, _config.MaxExportConnections.Value()); diff --git a/MessageControl/MessageOutput.cpp b/MessageControl/MessageOutput.cpp index abd8fc93..8cf00a0f 100644 --- a/MessageControl/MessageOutput.cpp +++ b/MessageControl/MessageOutput.cpp @@ -142,32 +142,34 @@ namespace Publishers { { } - void UDPOutput::Channel::Output(const Core::Messaging::Metadata& metadata, const Core::Messaging::IEvent* message) + void UDPOutput::Channel::Output(const string& text) { _adminLock.Lock(); - uint16_t length = 0; - ASSERT(metadata.Type() != Core::Messaging::Metadata::INVALID); + ASSERT((_loaded + text.length() + 1) < sizeof(_sendBuffer)); - length += metadata.Serialize(_sendBuffer + length, sizeof(_sendBuffer) - length); - length += message->Serialize(_sendBuffer + length, sizeof(_sendBuffer) - length); - _loaded = length; + if ((_loaded + text.length() + 1) < sizeof(_sendBuffer)) { + Core::FrameType<0> frame(_sendBuffer + _loaded, sizeof(_sendBuffer) - _loaded, sizeof(_sendBuffer) - _loaded); + Core::FrameType<0>::Writer frameWriter(frame, 0); + frameWriter.NullTerminatedText(text); + _loaded += frameWriter.Offset(); + } _adminLock.Unlock(); Trigger(); } - UDPOutput::UDPOutput(const Core::NodeId& nodeId) - : _output(nodeId) { + UDPOutput::UDPOutput(const Core::Messaging::MessageInfo::abbreviate abbreviate, const Core::NodeId& nodeId) + : _convertor(abbreviate) + , _output(nodeId) + { _output.Open(0); } void UDPOutput::Message(const Core::Messaging::MessageInfo& metadata, const string& text) /* override */ { - //yikes, recreating stuff from received pieces - Messaging::TextMessage textMessage(text); - _output.Output(metadata, &textMessage); + _output.Output(_convertor.Convert(metadata, text)); } } // namespace Publishers diff --git a/MessageControl/MessageOutput.h b/MessageControl/MessageOutput.h index 73c1d834..b414f8da 100644 --- a/MessageControl/MessageOutput.h +++ b/MessageControl/MessageOutput.h @@ -309,6 +309,8 @@ namespace Publishers { class UDPOutput : public IPublish { private: + static constexpr uint16_t UDPBufferSize = 4 * 1024; + class Channel : public Core::SocketDatagram { public: Channel() = delete; @@ -318,7 +320,7 @@ namespace Publishers { explicit Channel(const Core::NodeId& nodeId); ~Channel() override; - void Output(const Core::Messaging::Metadata& metadata, const Core::Messaging::IEvent* message); + void Output(const string& text); private: uint16_t SendData(uint8_t* dataFrame, const uint16_t maxSendSize) override; @@ -326,7 +328,7 @@ namespace Publishers { uint16_t ReceiveData(uint8_t*, const uint16_t) override; void StateChange() override; - uint8_t _sendBuffer[Messaging::MessageUnit::TempDataBufferSize]; + uint8_t _sendBuffer[UDPBufferSize]; uint16_t _loaded; Core::CriticalSection _adminLock; }; @@ -336,12 +338,13 @@ namespace Publishers { UDPOutput(const UDPOutput&) = delete; UDPOutput& operator=(const UDPOutput&) = delete; - explicit UDPOutput(const Core::NodeId& nodeId); + explicit UDPOutput(const Core::Messaging::MessageInfo::abbreviate abbreviate, const Core::NodeId& nodeId); ~UDPOutput() = default; void Message(const Core::Messaging::MessageInfo& metadata, const string& text); private: + Text _convertor; Channel _output; };