CProtocol::m_Streams is list

pull/1/head
Tom Early 6 years ago
parent b8f41a4314
commit 4bc72ce09e

@ -4,6 +4,7 @@
// //
// Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015. // Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. // Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
// Copyright © 2020 Thomas A. Early, N7TAE
// //
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// This file is part of xlxd. // This file is part of xlxd.
@ -19,7 +20,7 @@
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>. // along with Foobar. If not, see <http://www.gnu.org/licenses/>.
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#include "main.h" #include "main.h"
@ -36,7 +37,6 @@ CProtocol::CProtocol()
{ {
m_bStopThread = false; m_bStopThread = false;
m_pThread = NULL; m_pThread = NULL;
m_Streams.reserve(NB_OF_MODULES);
} }
@ -52,7 +52,7 @@ CProtocol::~CProtocol()
m_pThread->join(); m_pThread->join();
delete m_pThread; delete m_pThread;
} }
// empty queue // empty queue
m_Queue.Lock(); m_Queue.Lock();
while ( !m_Queue.empty() ) while ( !m_Queue.empty() )
@ -69,13 +69,13 @@ bool CProtocol::Init(void)
{ {
// init reflector apparent callsign // init reflector apparent callsign
m_ReflectorCallsign = g_Reflector.GetCallsign(); m_ReflectorCallsign = g_Reflector.GetCallsign();
// reset stop flag // reset stop flag
m_bStopThread = false; m_bStopThread = false;
// start thread; // start thread;
m_pThread = new std::thread(CProtocol::Thread, this); m_pThread = new std::thread(CProtocol::Thread, this);
// done // done
return true; return true;
} }
@ -138,7 +138,12 @@ void CProtocol::OnDvFramePacketIn(CDvFramePacket *Frame, const CIp *Ip)
{ {
// find the stream // find the stream
CPacketStream *stream = GetStream(Frame->GetStreamId(), Ip); 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; //std::cout << "DV frame" << "from " << *Ip << std::endl;
// and push // and push
@ -152,13 +157,18 @@ void CProtocol::OnDvLastFramePacketIn(CDvLastFramePacket *Frame, const CIp *Ip)
{ {
// find the stream // find the stream
CPacketStream *stream = GetStream(Frame->GetStreamId(), Ip); 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 // push
stream->Lock(); stream->Lock();
stream->Push(Frame); stream->Push(Frame);
stream->Unlock(); stream->Unlock();
// and close the stream // and close the stream
g_Reflector.CloseStream(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 *CProtocol::GetStream(uint16 uiStreamId, const CIp *Ip)
{ {
CPacketStream *stream = NULL; for ( auto it=m_Streams.begin(); it!=m_Streams.end(); it++ )
// find if we have a stream with same streamid in our cache
for ( int i = 0; (i < m_Streams.size()) && (stream == NULL); i++ )
{ {
if ( m_Streams[i]->GetStreamId() == uiStreamId ) if ( (*it)->GetStreamId() == uiStreamId )
{ {
// if Ip not NULL, also check if IP match // 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 // done
return stream; return NULL;
} }
void CProtocol::CheckStreamsTimeout(void) 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 ? // time out ?
m_Streams[i]->Lock(); (*it)->Lock();
if ( m_Streams[i]->IsExpired() ) if ( (*it)->IsExpired() )
{ {
// yes, close it // yes, close it
m_Streams[i]->Unlock(); (*it)->Unlock();
g_Reflector.CloseStream(m_Streams[i]); g_Reflector.CloseStream(*it);
// and remove it // and remove it
m_Streams.erase(m_Streams.begin()+i); it = m_Streams.erase(it);
} }
else else
{ {
m_Streams[i]->Unlock(); (*it++)->Unlock();
} }
} }
} }
@ -256,5 +263,3 @@ uint32 CProtocol::ModuleToDmrDestId(char m) const
{ {
return (uint32)(m - 'A')+1; return (uint32)(m - 'A')+1;
} }

@ -4,6 +4,7 @@
// //
// Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015. // Created by Jean-Luc Deltombe (LX3JL) on 01/11/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. // Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
// Copyright © 2020 Thomas A. Early, N7TAE
// //
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// This file is part of xlxd. // This file is part of xlxd.
@ -19,7 +20,7 @@
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>. // along with Foobar. If not, see <http://www.gnu.org/licenses/>.
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#ifndef cprotocol_h #ifndef cprotocol_h
@ -71,44 +72,44 @@ class CProtocol
public: public:
// constructor // constructor
CProtocol(); CProtocol();
// destructor // destructor
virtual ~CProtocol(); virtual ~CProtocol();
// initialization // initialization
virtual bool Init(void); virtual bool Init(void);
virtual void Close(void); virtual void Close(void);
// queue // queue
CPacketQueue *GetQueue(void) { m_Queue.Lock(); return &m_Queue; } CPacketQueue *GetQueue(void) { m_Queue.Lock(); return &m_Queue; }
void ReleaseQueue(void) { m_Queue.Unlock(); } void ReleaseQueue(void) { m_Queue.Unlock(); }
// get // get
const CCallsign &GetReflectorCallsign(void)const { return m_ReflectorCallsign; } const CCallsign &GetReflectorCallsign(void)const { return m_ReflectorCallsign; }
// task // task
static void Thread(CProtocol *); static void Thread(CProtocol *);
virtual void Task(void) {}; virtual void Task(void) {}
protected: protected:
// packet encoding helpers // packet encoding helpers
virtual bool EncodeDvPacket(const CPacket &, CBuffer *) const; virtual bool EncodeDvPacket(const CPacket &, CBuffer *) const;
virtual bool EncodeDvHeaderPacket(const CDvHeaderPacket &, CBuffer *) const { return false; } virtual bool EncodeDvHeaderPacket(const CDvHeaderPacket &, CBuffer *) const { return false; }
virtual bool EncodeDvFramePacket(const CDvFramePacket &, CBuffer *) const { return false; } virtual bool EncodeDvFramePacket(const CDvFramePacket &, CBuffer *) const { return false; }
virtual bool EncodeDvLastFramePacket(const CDvLastFramePacket &, CBuffer *) const { return false; } virtual bool EncodeDvLastFramePacket(const CDvLastFramePacket &, CBuffer *) const { return false; }
// stream helpers // stream helpers
virtual bool OnDvHeaderPacketIn(CDvHeaderPacket *, const CIp &) { return false; } virtual bool OnDvHeaderPacketIn(CDvHeaderPacket *, const CIp &) { return false; }
virtual void OnDvFramePacketIn(CDvFramePacket *, const CIp * = NULL); virtual void OnDvFramePacketIn(CDvFramePacket *, const CIp * = NULL);
virtual void OnDvLastFramePacketIn(CDvLastFramePacket *, const CIp * = NULL); virtual void OnDvLastFramePacketIn(CDvLastFramePacket *, const CIp * = NULL);
// stream handle helpers // stream handle helpers
CPacketStream *GetStream(uint16, const CIp * = NULL); CPacketStream *GetStream(uint16, const CIp * = NULL);
void CheckStreamsTimeout(void); void CheckStreamsTimeout(void);
// queue helper // queue helper
virtual void HandleQueue(void); virtual void HandleQueue(void);
// keepalive helpers // keepalive helpers
virtual void HandleKeepalives(void) {} virtual void HandleKeepalives(void) {}
@ -116,7 +117,7 @@ protected:
bool IsNumber(char) const; bool IsNumber(char) const;
bool IsLetter(char) const; bool IsLetter(char) const;
bool IsSpace(char) const; bool IsSpace(char) const;
// dmr DstId to Module helper // dmr DstId to Module helper
virtual char DmrDstIdToModule(uint32) const; virtual char DmrDstIdToModule(uint32) const;
virtual uint32 ModuleToDmrDestId(char) const; virtual uint32 ModuleToDmrDestId(char) const;
@ -124,20 +125,20 @@ protected:
protected: protected:
// socket // socket
CUdpSocket m_Socket; CUdpSocket m_Socket;
// streams // streams
std::vector<CPacketStream *> m_Streams; std::list<CPacketStream *> m_Streams;
// queue // queue
CPacketQueue m_Queue; CPacketQueue m_Queue;
// thread // thread
bool m_bStopThread; bool m_bStopThread;
std::thread *m_pThread; std::thread *m_pThread;
// identity // identity
CCallsign m_ReflectorCallsign; CCallsign m_ReflectorCallsign;
// debug // debug
CTimePoint m_DebugTimer; CTimePoint m_DebugTimer;
}; };

Loading…
Cancel
Save

Powered by TurnKey Linux.