From fd9ddd718185573a589d0818675b32b62e64835a Mon Sep 17 00:00:00 2001 From: Tom Early Date: Sat, 25 Feb 2023 06:22:47 -0700 Subject: [PATCH] packets need a copy function --- reflector/DExtraProtocol.cpp | 5 ----- reflector/DVFramePacket.cpp | 5 +++++ reflector/DVFramePacket.h | 1 + reflector/DVHeaderPacket.cpp | 5 +++++ reflector/DVHeaderPacket.h | 1 + reflector/Packet.h | 1 + reflector/Protocol.h | 4 ++-- reflector/Reflector.cpp | 18 ++++++------------ 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/reflector/DExtraProtocol.cpp b/reflector/DExtraProtocol.cpp index f104690..9e4c64b 100644 --- a/reflector/DExtraProtocol.cpp +++ b/reflector/DExtraProtocol.cpp @@ -211,11 +211,6 @@ void CDextraProtocol::HandleQueue(void) // is this client busy ? if ( !client->IsAMaster() && (client->GetReflectorModule() == packet->GetPacketModule()) ) { - if (packet->IsDvHeader()) - { - buffer.Dump("Header packet"); - std::cout << "Is going to " << client->GetCallsign() << " at " << client->GetIp() << std::endl; - } // no, send the packet int n = packet->IsDvHeader() ? 5 : 1; for ( int i = 0; i < n; i++ ) diff --git a/reflector/DVFramePacket.cpp b/reflector/DVFramePacket.cpp index 6260270..eaab0a5 100644 --- a/reflector/DVFramePacket.cpp +++ b/reflector/DVFramePacket.cpp @@ -167,6 +167,11 @@ CDvFramePacket::CDvFramePacket(const int16_t *usrp, uint16_t streamid, bool isla m_TCPack.codec_in = ECodecType::usrp; } +std::unique_ptr CDvFramePacket::Copy(void) +{ + return std::unique_ptr(new CDvFramePacket(*this)); +} + // Network unsigned int CDvFramePacket::GetNetworkSize() { diff --git a/reflector/DVFramePacket.h b/reflector/DVFramePacket.h index bc91aed..21bbcd8 100644 --- a/reflector/DVFramePacket.h +++ b/reflector/DVFramePacket.h @@ -63,6 +63,7 @@ public: void EncodeInterlinkPacket(CBuffer &buf) const; // identity + std::unique_ptr Copy(void); bool IsDvHeader(void) const { return false; } bool IsDvFrame(void) const { return true; } diff --git a/reflector/DVHeaderPacket.cpp b/reflector/DVHeaderPacket.cpp index 356d2c6..bc23760 100644 --- a/reflector/DVHeaderPacket.cpp +++ b/reflector/DVHeaderPacket.cpp @@ -165,6 +165,11 @@ CDvHeaderPacket::CDvHeaderPacket(const CM17Packet &m17) : CPacket(m17) m_csRPT1.SetCSModule('G'); } +std::unique_ptr CDvHeaderPacket::Copy(void) +{ + return std::unique_ptr(new CDvHeaderPacket(*this)); +} + //////////////////////////////////////////////////////////////////////////////////////// // conversion diff --git a/reflector/DVHeaderPacket.h b/reflector/DVHeaderPacket.h index 7ffd25c..7bbfd29 100644 --- a/reflector/DVHeaderPacket.h +++ b/reflector/DVHeaderPacket.h @@ -66,6 +66,7 @@ public: void EncodeInterlinkPacket(CBuffer &buf) const; // identity + std::unique_ptr Copy(void); bool IsDvHeader(void) const { return true; } bool IsDvFrame(void) const { return false; } diff --git a/reflector/Packet.h b/reflector/Packet.h index 94e6ab5..0ee7012 100644 --- a/reflector/Packet.h +++ b/reflector/Packet.h @@ -41,6 +41,7 @@ public: CPacket(const CM17Packet &); // identity + virtual std::unique_ptr Copy(void) = 0; virtual bool IsDvHeader(void) const = 0; virtual bool IsDvFrame(void) const = 0; bool IsLastPacket(void) const { return m_bLastPacket; } diff --git a/reflector/Protocol.h b/reflector/Protocol.h index 8dafe08..678b533 100644 --- a/reflector/Protocol.h +++ b/reflector/Protocol.h @@ -80,7 +80,7 @@ public: virtual void Task(void) = 0; // pass-through - void Push(std::shared_ptr p) { m_Queue.Push(p); } + void Push(std::unique_ptr p) { m_Queue.Push(std::move(p)); } protected: // stream helpers @@ -126,7 +126,7 @@ protected: std::unordered_map> m_Streams; // queue - CSafePacketQueue> m_Queue; + CSafePacketQueue> m_Queue; // thread std::atomic keep_running; diff --git a/reflector/Reflector.cpp b/reflector/Reflector.cpp index f3b9839..39cf577 100644 --- a/reflector/Reflector.cpp +++ b/reflector/Reflector.cpp @@ -265,10 +265,8 @@ void CReflector::RouterThread(const char ThisModule) while (keep_running) { // wait until something shows up - auto uptmp = streamIn->PopWait(); - // convert the incoming packet to a shared_ptr - std::shared_ptr packet = std::move(uptmp); - // set origin + auto packet = streamIn->PopWait(); + packet->SetPacketModule(ThisModule); // iterate on all protocols @@ -276,22 +274,18 @@ void CReflector::RouterThread(const char ThisModule) for ( auto it=m_Protocols.begin(); it!=m_Protocols.end(); it++ ) { // make a copy! after the Push(tmp), tmp will be nullptr! - auto tmp = packet; + auto copy = packet->Copy(); // if packet is header, update RPT2 according to protocol - if ( tmp->IsDvHeader() ) + if ( copy->IsDvHeader() ) { // get our callsign CCallsign csRPT = (*it)->GetReflectorCallsign(); csRPT.SetCSModule(ThisModule); - //(dynamic_cast(tmp.get()))->SetRpt2Callsign(csRPT); - auto x = dynamic_cast(tmp.get()); - x->SetRpt2Callsign(csRPT); - if ((*it)->GetPort() == 30001) - std::cout << (*it)->GetReflectorCallsign() << ": " << csRPT << " == " << x->GetRpt2Callsign() << std::endl; + (dynamic_cast(copy.get()))->SetRpt2Callsign(csRPT); } - (*it)->Push(tmp); + (*it)->Push(std::move(copy)); } m_Protocols.Unlock(); }