diff --git a/src/cprotocol.cpp b/src/cprotocol.cpp
index 7fd4967..3904b07 100644
--- a/src/cprotocol.cpp
+++ b/src/cprotocol.cpp
@@ -4,6 +4,7 @@
//
// Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
+// Copyright © 2020 Thomas A. Early, N7TAE
//
// ----------------------------------------------------------------------------
// This file is part of xlxd.
@@ -19,7 +20,7 @@
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
-// along with Foobar. If not, see .
+// along with Foobar. If not, see .
// ----------------------------------------------------------------------------
#include "main.h"
@@ -36,7 +37,6 @@ CProtocol::CProtocol()
{
m_bStopThread = false;
m_pThread = NULL;
- m_Streams.reserve(NB_OF_MODULES);
}
@@ -52,7 +52,7 @@ CProtocol::~CProtocol()
m_pThread->join();
delete m_pThread;
}
-
+
// empty queue
m_Queue.Lock();
while ( !m_Queue.empty() )
@@ -69,13 +69,13 @@ bool CProtocol::Init(void)
{
// init reflector apparent callsign
m_ReflectorCallsign = g_Reflector.GetCallsign();
-
+
// reset stop flag
m_bStopThread = false;
// start thread;
m_pThread = new std::thread(CProtocol::Thread, this);
-
+
// done
return true;
}
@@ -138,7 +138,12 @@ void CProtocol::OnDvFramePacketIn(CDvFramePacket *Frame, const CIp *Ip)
{
// find the stream
CPacketStream *stream = GetStream(Frame->GetStreamId(), Ip);
- if ( stream != NULL )
+ if ( stream == NULL )
+ {
+ std::cout << "Deleting orphaned Frame with ID " << Frame->GetStreamId() << " on " << Ip << std::endl;
+ delete Frame;
+ }
+ else
{
//std::cout << "DV frame" << "from " << *Ip << std::endl;
// and push
@@ -152,13 +157,18 @@ void CProtocol::OnDvLastFramePacketIn(CDvLastFramePacket *Frame, const CIp *Ip)
{
// find the stream
CPacketStream *stream = GetStream(Frame->GetStreamId(), Ip);
- if ( stream != NULL )
+ if ( stream == NULL )
+ {
+ std::cout << "Deleting orphaned Last Frame with ID " << Frame->GetStreamId() << " on " << Ip << std::endl;
+ delete Frame;
+ }
+ else
{
// push
stream->Lock();
stream->Push(Frame);
stream->Unlock();
-
+
// and close the stream
g_Reflector.CloseStream(stream);
}
@@ -169,44 +179,41 @@ void CProtocol::OnDvLastFramePacketIn(CDvLastFramePacket *Frame, const CIp *Ip)
CPacketStream *CProtocol::GetStream(uint16 uiStreamId, const CIp *Ip)
{
- CPacketStream *stream = NULL;
-
- // find if we have a stream with same streamid in our cache
- for ( int i = 0; (i < m_Streams.size()) && (stream == NULL); i++ )
+ for ( auto it=m_Streams.begin(); it!=m_Streams.end(); it++ )
{
- if ( m_Streams[i]->GetStreamId() == uiStreamId )
+ if ( (*it)->GetStreamId() == uiStreamId )
{
// if Ip not NULL, also check if IP match
- if ( (Ip != NULL) && (m_Streams[i]->GetOwnerIp() != NULL) )
+ if ( (Ip != NULL) && ((*it)->GetOwnerIp() != NULL) )
{
- if ( *Ip == *(m_Streams[i]->GetOwnerIp()) )
+ if ( *Ip == *((*it)->GetOwnerIp()) )
{
- stream = m_Streams[i];
+ return *it;
}
}
}
}
// done
- return stream;
+ return NULL;
}
void CProtocol::CheckStreamsTimeout(void)
{
- for ( int i = 0; i < m_Streams.size(); i++ )
+ for ( auto it=m_Streams.begin(); it!=m_Streams.end(); )
{
// time out ?
- m_Streams[i]->Lock();
- if ( m_Streams[i]->IsExpired() )
+ (*it)->Lock();
+ if ( (*it)->IsExpired() )
{
// yes, close it
- m_Streams[i]->Unlock();
- g_Reflector.CloseStream(m_Streams[i]);
+ (*it)->Unlock();
+ g_Reflector.CloseStream(*it);
// and remove it
- m_Streams.erase(m_Streams.begin()+i);
+ it = m_Streams.erase(it);
}
else
{
- m_Streams[i]->Unlock();
+ (*it++)->Unlock();
}
}
}
@@ -256,5 +263,3 @@ uint32 CProtocol::ModuleToDmrDestId(char m) const
{
return (uint32)(m - 'A')+1;
}
-
-
diff --git a/src/cprotocol.h b/src/cprotocol.h
index 1b43371..f736ed7 100644
--- a/src/cprotocol.h
+++ b/src/cprotocol.h
@@ -4,6 +4,7 @@
//
// Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
+// Copyright © 2020 Thomas A. Early, N7TAE
//
// ----------------------------------------------------------------------------
// This file is part of xlxd.
@@ -19,7 +20,7 @@
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
-// along with Foobar. If not, see .
+// along with Foobar. If not, see .
// ----------------------------------------------------------------------------
#ifndef cprotocol_h
@@ -71,44 +72,44 @@ class CProtocol
public:
// constructor
CProtocol();
-
+
// destructor
virtual ~CProtocol();
-
+
// initialization
virtual bool Init(void);
virtual void Close(void);
-
+
// queue
CPacketQueue *GetQueue(void) { m_Queue.Lock(); return &m_Queue; }
void ReleaseQueue(void) { m_Queue.Unlock(); }
-
+
// get
const CCallsign &GetReflectorCallsign(void)const { return m_ReflectorCallsign; }
-
+
// task
static void Thread(CProtocol *);
- virtual void Task(void) {};
-
+ virtual void Task(void) {}
+
protected:
// packet encoding helpers
virtual bool EncodeDvPacket(const CPacket &, CBuffer *) const;
virtual bool EncodeDvHeaderPacket(const CDvHeaderPacket &, CBuffer *) const { return false; }
virtual bool EncodeDvFramePacket(const CDvFramePacket &, CBuffer *) const { return false; }
virtual bool EncodeDvLastFramePacket(const CDvLastFramePacket &, CBuffer *) const { return false; }
-
+
// stream helpers
virtual bool OnDvHeaderPacketIn(CDvHeaderPacket *, const CIp &) { return false; }
virtual void OnDvFramePacketIn(CDvFramePacket *, const CIp * = NULL);
virtual void OnDvLastFramePacketIn(CDvLastFramePacket *, const CIp * = NULL);
-
+
// stream handle helpers
CPacketStream *GetStream(uint16, const CIp * = NULL);
void CheckStreamsTimeout(void);
-
+
// queue helper
virtual void HandleQueue(void);
-
+
// keepalive helpers
virtual void HandleKeepalives(void) {}
@@ -116,7 +117,7 @@ protected:
bool IsNumber(char) const;
bool IsLetter(char) const;
bool IsSpace(char) const;
-
+
// dmr DstId to Module helper
virtual char DmrDstIdToModule(uint32) const;
virtual uint32 ModuleToDmrDestId(char) const;
@@ -124,20 +125,20 @@ protected:
protected:
// socket
CUdpSocket m_Socket;
-
+
// streams
- std::vector m_Streams;
-
+ std::list m_Streams;
+
// queue
CPacketQueue m_Queue;
-
+
// thread
bool m_bStopThread;
std::thread *m_pThread;
-
+
// identity
CCallsign m_ReflectorCallsign;
-
+
// debug
CTimePoint m_DebugTimer;
};