2016-02-11 12:51:12 -05:00
|
|
|
//
|
|
|
|
// cxlxprotocol.cpp
|
|
|
|
// xlxd
|
|
|
|
//
|
|
|
|
// Created by Jean-Luc Deltombe (LX3JL) on 28/01/2016.
|
|
|
|
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
|
|
|
//
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// This file is part of xlxd.
|
|
|
|
//
|
|
|
|
// xlxd is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// xlxd is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <string.h>
|
2016-12-16 05:28:32 -05:00
|
|
|
#include "cxlxpeer.h"
|
2016-02-11 12:51:12 -05:00
|
|
|
#include "cxlxprotocol.h"
|
|
|
|
#include "creflector.h"
|
|
|
|
#include "cgatekeeper.h"
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// operation
|
|
|
|
|
|
|
|
bool CXlxProtocol::Init(void)
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
// base class
|
|
|
|
ok = CProtocol::Init();
|
|
|
|
|
|
|
|
// update the reflector callsign
|
|
|
|
m_ReflectorCallsign.PatchCallsign(0, (const uint8 *)"XLX", 3);
|
|
|
|
|
|
|
|
// create our socket
|
|
|
|
ok &= m_Socket.Open(XLX_PORT);
|
|
|
|
if ( !ok )
|
|
|
|
{
|
|
|
|
std::cout << "Error opening socket on port UDP" << XLX_PORT << " on ip " << g_Reflector.GetListenIp() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update time
|
|
|
|
m_LastKeepaliveTime.Now();
|
2016-12-16 05:28:32 -05:00
|
|
|
m_LastPeersLinkTime.Now();
|
2016-02-11 12:51:12 -05:00
|
|
|
|
|
|
|
// done
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// task
|
|
|
|
|
|
|
|
void CXlxProtocol::Task(void)
|
|
|
|
{
|
|
|
|
CBuffer Buffer;
|
|
|
|
CIp Ip;
|
|
|
|
CCallsign Callsign;
|
|
|
|
char Modules[NB_MODULES_MAX+1];
|
|
|
|
CDvHeaderPacket *Header;
|
|
|
|
CDvFramePacket *Frame;
|
|
|
|
CDvLastFramePacket *LastFrame;
|
2016-12-16 05:28:32 -05:00
|
|
|
uint8 Major, Minor, Revision;
|
2016-02-11 12:51:12 -05:00
|
|
|
|
|
|
|
// any incoming packet ?
|
|
|
|
if ( m_Socket.Receive(&Buffer, &Ip, 20) != -1 )
|
|
|
|
{
|
|
|
|
// crack the packet
|
|
|
|
if ( (Frame = IsValidDvFramePacket(Buffer)) != NULL )
|
|
|
|
{
|
|
|
|
//std::cout << "XLX (DExtra) DV frame" << std::endl;
|
|
|
|
|
|
|
|
// handle it
|
|
|
|
OnDvFramePacketIn(Frame);
|
|
|
|
}
|
|
|
|
else if ( (Header = IsValidDvHeaderPacket(Buffer)) != NULL )
|
|
|
|
{
|
|
|
|
//std::cout << "XLX (DExtra) DV header:" << std::endl << *Header << std::endl;
|
2016-12-16 05:28:32 -05:00
|
|
|
//std::cout << "XLX (DExtra) DV header on module " << Header->GetRpt2Module() << std::endl;
|
2016-02-11 12:51:12 -05:00
|
|
|
|
|
|
|
// callsign muted?
|
|
|
|
if ( g_GateKeeper.MayTransmit(Header->GetMyCallsign(), Ip) )
|
|
|
|
{
|
|
|
|
// handle it
|
|
|
|
OnDvHeaderPacketIn(Header, Ip);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete Header;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( (LastFrame = IsValidDvLastFramePacket(Buffer)) != NULL )
|
|
|
|
{
|
|
|
|
//std::cout << "XLX (DExtra) DV last frame" << std::endl;
|
|
|
|
|
|
|
|
// handle it
|
|
|
|
OnDvLastFramePacketIn(LastFrame);
|
|
|
|
}
|
2016-12-16 05:28:32 -05:00
|
|
|
else if ( IsValidConnectPacket(Buffer, &Callsign, Modules, &Major, &Minor, &Revision) )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
2016-12-16 05:28:32 -05:00
|
|
|
std::cout << "XLX (" << (int)Major << "." << (int)Minor << "." << (int)Revision
|
|
|
|
<< ") connect packet for modules " << Modules
|
|
|
|
<< " from " << Callsign << " at " << Ip << std::endl;
|
2016-02-11 12:51:12 -05:00
|
|
|
|
2016-12-16 05:28:32 -05:00
|
|
|
// callsign authorized?
|
|
|
|
if ( g_GateKeeper.MayLink(Callsign, Ip, PROTOCOL_XLX, Modules) )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
2016-12-16 05:28:32 -05:00
|
|
|
// following is version dependent
|
|
|
|
// for backward compatibility, only send ACK once
|
|
|
|
if ( (Major == 1) && (Minor < 4) )
|
|
|
|
{
|
|
|
|
// already connected ?
|
|
|
|
CPeers *peers = g_Reflector.GetPeers();
|
|
|
|
if ( peers->FindPeer(Callsign, Ip, PROTOCOL_XLX) == NULL )
|
|
|
|
{
|
|
|
|
// acknowledge the request
|
|
|
|
EncodeConnectAckPacket(&Buffer, Modules);
|
|
|
|
m_Socket.Send(Buffer, Ip);
|
|
|
|
}
|
|
|
|
g_Reflector.ReleasePeers();
|
|
|
|
}
|
|
|
|
else
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
|
|
|
// acknowledge the request
|
|
|
|
EncodeConnectAckPacket(&Buffer, Modules);
|
2016-12-16 05:28:32 -05:00
|
|
|
m_Socket.Send(Buffer, Ip);
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
2016-12-16 05:28:32 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// deny the request
|
|
|
|
EncodeConnectNackPacket(&Buffer);
|
|
|
|
m_Socket.Send(Buffer, Ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( IsValidAckPacket(Buffer, &Callsign, Modules) )
|
|
|
|
{
|
|
|
|
std::cout << "XLX ack packet for modules " << Modules << " from " << Callsign << " at " << Ip << std::endl;
|
|
|
|
|
|
|
|
// callsign authorized?
|
|
|
|
if ( g_GateKeeper.MayLink(Callsign, Ip, PROTOCOL_XLX, Modules) )
|
|
|
|
{
|
|
|
|
// already connected ?
|
|
|
|
CPeers *peers = g_Reflector.GetPeers();
|
|
|
|
if ( peers->FindPeer(Callsign, Ip, PROTOCOL_XLX) == NULL )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
2016-12-16 05:28:32 -05:00
|
|
|
// create the new peer
|
|
|
|
// this also create one client per module
|
|
|
|
CXlxPeer *peer = new CXlxPeer(Callsign, Ip, Modules);
|
|
|
|
|
|
|
|
// append the peer to reflector peer list
|
|
|
|
// this also add all new clients to reflector client list
|
|
|
|
peers->AddPeer(peer);
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
2016-12-16 05:28:32 -05:00
|
|
|
g_Reflector.ReleasePeers();
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( IsValidDisconnectPacket(Buffer, &Callsign) )
|
|
|
|
{
|
|
|
|
std::cout << "XLX disconnect packet from " << Callsign << " at " << Ip << std::endl;
|
|
|
|
|
2016-12-16 05:28:32 -05:00
|
|
|
// find peer
|
|
|
|
CPeers *peers = g_Reflector.GetPeers();
|
|
|
|
CPeer *peer = peers->FindPeer(Ip, PROTOCOL_XLX);
|
|
|
|
if ( peer != NULL )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
2016-12-16 05:28:32 -05:00
|
|
|
// remove it from reflector peer list
|
|
|
|
// this also remove all concerned clients from reflector client list
|
|
|
|
// and delete them
|
|
|
|
peers->RemovePeer(peer);
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
2016-12-16 05:28:32 -05:00
|
|
|
g_Reflector.ReleasePeers();
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
|
|
|
else if ( IsValidNackPacket(Buffer, &Callsign) )
|
|
|
|
{
|
|
|
|
std::cout << "XLX nack packet from " << Callsign << " at " << Ip << std::endl;
|
|
|
|
}
|
|
|
|
else if ( IsValidKeepAlivePacket(Buffer, &Callsign) )
|
|
|
|
{
|
|
|
|
//std::cout << "XLX keepalive packet from " << Callsign << " at " << Ip << std::endl;
|
|
|
|
|
2016-12-16 05:28:32 -05:00
|
|
|
// find peer
|
|
|
|
CPeers *peers = g_Reflector.GetPeers();
|
|
|
|
CPeer *peer = peers->FindPeer(Ip, PROTOCOL_XLX);
|
|
|
|
if ( peer != NULL )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
2016-12-16 05:28:32 -05:00
|
|
|
// keep it alive
|
|
|
|
peer->Alive();
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
2016-12-16 05:28:32 -05:00
|
|
|
g_Reflector.ReleasePeers();
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << "XLX packet (" << Buffer.size() << ")" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle end of streaming timeout
|
|
|
|
CheckStreamsTimeout();
|
|
|
|
|
|
|
|
// handle queue from reflector
|
|
|
|
HandleQueue();
|
|
|
|
|
|
|
|
// keep alive
|
|
|
|
if ( m_LastKeepaliveTime.DurationSinceNow() > XLX_KEEPALIVE_PERIOD )
|
|
|
|
{
|
|
|
|
// handle keep alives
|
|
|
|
HandleKeepalives();
|
|
|
|
|
2016-12-16 05:28:32 -05:00
|
|
|
// update time
|
|
|
|
m_LastKeepaliveTime.Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
// peer connections
|
|
|
|
if ( m_LastPeersLinkTime.DurationSinceNow() > XLX_RECONNECT_PERIOD )
|
|
|
|
{
|
2016-02-11 12:51:12 -05:00
|
|
|
// handle remote peers connections
|
|
|
|
HandlePeerLinks();
|
|
|
|
|
|
|
|
// update time
|
2016-12-16 05:28:32 -05:00
|
|
|
m_LastPeersLinkTime.Now();
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// queue helper
|
|
|
|
|
|
|
|
void CXlxProtocol::HandleQueue(void)
|
|
|
|
{
|
|
|
|
m_Queue.Lock();
|
|
|
|
while ( !m_Queue.empty() )
|
|
|
|
{
|
|
|
|
// get the packet
|
|
|
|
CPacket *packet = m_Queue.front();
|
|
|
|
m_Queue.pop();
|
|
|
|
|
|
|
|
// check if origin of packet is local
|
|
|
|
// if not, do not stream it out as it will cause
|
|
|
|
// network loop between linked XLX peers
|
|
|
|
if ( packet->IsLocalOrigin() )
|
|
|
|
{
|
|
|
|
// encode it
|
|
|
|
CBuffer buffer;
|
|
|
|
if ( EncodeDvPacket(*packet, &buffer) )
|
|
|
|
{
|
|
|
|
// and push it to all our clients linked to the module and who are not streaming in
|
|
|
|
CClients *clients = g_Reflector.GetClients();
|
|
|
|
int index = -1;
|
|
|
|
CClient *client = NULL;
|
|
|
|
while ( (client = clients->FindNextClient(PROTOCOL_XLX, &index)) != NULL )
|
|
|
|
{
|
|
|
|
// is this client busy ?
|
2016-12-16 05:28:32 -05:00
|
|
|
if ( !client->IsAMaster() && (client->GetReflectorModule() == packet->GetModuleId()) )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
|
|
|
// no, send the packet
|
|
|
|
m_Socket.Send(buffer, client->GetIp());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_Reflector.ReleaseClients();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
|
|
|
delete packet;
|
|
|
|
}
|
|
|
|
m_Queue.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// keepalive helpers
|
|
|
|
|
|
|
|
void CXlxProtocol::HandleKeepalives(void)
|
|
|
|
{
|
|
|
|
// DExtra protocol sends and monitors keepalives packets
|
|
|
|
// event if the client is currently streaming
|
|
|
|
// so, send keepalives to all
|
|
|
|
CBuffer keepalive;
|
|
|
|
EncodeKeepAlivePacket(&keepalive);
|
|
|
|
|
2016-12-16 05:28:32 -05:00
|
|
|
// iterate on peers
|
|
|
|
CPeers *peers = g_Reflector.GetPeers();
|
2016-02-11 12:51:12 -05:00
|
|
|
int index = -1;
|
2016-12-16 05:28:32 -05:00
|
|
|
CPeer *peer = NULL;
|
|
|
|
while ( (peer = peers->FindNextPeer(PROTOCOL_XLX, &index)) != NULL )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
|
|
|
// send keepalive
|
2016-12-16 05:28:32 -05:00
|
|
|
m_Socket.Send(keepalive, peer->GetIp());
|
2016-02-11 12:51:12 -05:00
|
|
|
|
|
|
|
// client busy ?
|
2016-12-16 05:28:32 -05:00
|
|
|
if ( peer->IsAMaster() )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
|
|
|
// yes, just tickle it
|
2016-12-16 05:28:32 -05:00
|
|
|
peer->Alive();
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
|
|
|
// otherwise check if still with us
|
2016-12-16 05:28:32 -05:00
|
|
|
else if ( !peer->IsAlive() )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
|
|
|
// no, disconnect
|
|
|
|
CBuffer disconnect;
|
|
|
|
EncodeDisconnectPacket(&disconnect);
|
2016-12-16 05:28:32 -05:00
|
|
|
m_Socket.Send(disconnect, peer->GetIp());
|
2016-02-11 12:51:12 -05:00
|
|
|
|
|
|
|
// remove it
|
2016-12-16 05:28:32 -05:00
|
|
|
std::cout << "XLX peer " << peer->GetCallsign() << " keepalive timeout" << std::endl;
|
|
|
|
peers->RemovePeer(peer);
|
2016-04-18 03:59:32 -04:00
|
|
|
}
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
2016-12-16 05:28:32 -05:00
|
|
|
g_Reflector.ReleasePeers();
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Peers helpers
|
|
|
|
|
|
|
|
void CXlxProtocol::HandlePeerLinks(void)
|
|
|
|
{
|
|
|
|
CBuffer buffer;
|
|
|
|
|
|
|
|
// get the list of peers
|
|
|
|
CPeerCallsignList *list = g_GateKeeper.GetPeerList();
|
2016-12-16 05:28:32 -05:00
|
|
|
CPeers *peers = g_Reflector.GetPeers();
|
2016-02-11 12:51:12 -05:00
|
|
|
|
2016-12-16 05:28:32 -05:00
|
|
|
// check if all our connected peers are still listed by gatekeeper
|
2016-02-11 12:51:12 -05:00
|
|
|
// if not, disconnect
|
|
|
|
int index = -1;
|
2016-12-16 05:28:32 -05:00
|
|
|
CPeer *peer = NULL;
|
|
|
|
while ( (peer = peers->FindNextPeer(PROTOCOL_XLX, &index)) != NULL )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
2016-12-16 05:28:32 -05:00
|
|
|
if ( list->FindListItem(peer->GetCallsign()) == NULL )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
|
|
|
// send disconnect packet
|
|
|
|
EncodeDisconnectPacket(&buffer);
|
2016-12-16 05:28:32 -05:00
|
|
|
m_Socket.Send(buffer, peer->GetIp());
|
|
|
|
std::cout << "Sending disconnect packet to XLX peer " << peer->GetCallsign() << std::endl;
|
2016-02-11 12:51:12 -05:00
|
|
|
// remove client
|
2016-12-16 05:28:32 -05:00
|
|
|
peers->RemovePeer(peer);
|
2016-02-11 12:51:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if all ours peers listed by gatekeeper are connected
|
|
|
|
// if not, connect or reconnect
|
|
|
|
for ( int i = 0; i < list->size(); i++ )
|
|
|
|
{
|
|
|
|
CCallsignListItem *item = &((list->data())[i]);
|
2016-12-16 05:28:32 -05:00
|
|
|
if ( peers->FindPeer(item->GetCallsign(), PROTOCOL_XLX) == NULL )
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
2016-08-17 02:29:15 -04:00
|
|
|
// resolve again peer's IP in case it's a dynamic IP
|
|
|
|
item->ResolveIp();
|
2016-02-11 12:51:12 -05:00
|
|
|
// send connect packet to re-initiate peer link
|
|
|
|
EncodeConnectPacket(&buffer, item->GetModules());
|
|
|
|
m_Socket.Send(buffer, item->GetIp(), XLX_PORT);
|
|
|
|
std::cout << "Sending connect packet to XLX peer " << item->GetCallsign() << " @ " << item->GetIp() << " for modules " << item->GetModules() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
2016-12-16 05:28:32 -05:00
|
|
|
g_Reflector.ReleasePeers();
|
2016-02-11 12:51:12 -05:00
|
|
|
g_GateKeeper.ReleasePeerList();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// streams helpers
|
|
|
|
|
|
|
|
bool CXlxProtocol::OnDvHeaderPacketIn(CDvHeaderPacket *Header, const CIp &Ip)
|
|
|
|
{
|
|
|
|
bool newstream = false;
|
|
|
|
CCallsign peer;
|
|
|
|
|
|
|
|
// todo: verify Packet.GetModuleId() is in authorized list of XLX of origin
|
|
|
|
// todo: do the same for DVFrame and DVLAstFrame packets
|
|
|
|
|
|
|
|
// tag packet as remote peer origin
|
|
|
|
Header->SetRemotePeerOrigin();
|
|
|
|
|
|
|
|
// find the stream
|
|
|
|
CPacketStream *stream = GetStream(Header->GetStreamId());
|
|
|
|
if ( stream == NULL )
|
|
|
|
{
|
|
|
|
// no stream open yet, open a new one
|
|
|
|
// find this client
|
2016-12-16 05:28:32 -05:00
|
|
|
CClient *client = g_Reflector.GetClients()->FindClient(Ip, PROTOCOL_XLX, Header->GetRpt2Module());
|
2016-02-11 12:51:12 -05:00
|
|
|
if ( client != NULL )
|
|
|
|
{
|
|
|
|
// and try to open the stream
|
|
|
|
if ( (stream = g_Reflector.OpenStream(Header, client)) != NULL )
|
|
|
|
{
|
|
|
|
// keep the handle
|
|
|
|
m_Streams.push_back(stream);
|
|
|
|
newstream = true;
|
|
|
|
}
|
|
|
|
// get origin
|
|
|
|
peer = client->GetCallsign();
|
|
|
|
}
|
|
|
|
// release
|
|
|
|
g_Reflector.ReleaseClients();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// stream already open
|
|
|
|
// skip packet, but tickle the stream
|
|
|
|
stream->Tickle();
|
|
|
|
}
|
|
|
|
|
|
|
|
// update last heard
|
2017-01-24 02:45:11 -05:00
|
|
|
g_Reflector.GetUsers()->Hearing(Header->GetMyCallsign(), Header->GetRpt1Callsign(), Header->GetRpt2Callsign(), peer);
|
2016-02-11 12:51:12 -05:00
|
|
|
g_Reflector.ReleaseUsers();
|
|
|
|
|
|
|
|
// done
|
|
|
|
return newstream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXlxProtocol::OnDvFramePacketIn(CDvFramePacket *DvFrame)
|
|
|
|
{
|
|
|
|
// tag packet as remote peer origin
|
|
|
|
DvFrame->SetRemotePeerOrigin();
|
|
|
|
|
|
|
|
// anc call base class
|
|
|
|
CDextraProtocol::OnDvFramePacketIn(DvFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXlxProtocol::OnDvLastFramePacketIn(CDvLastFramePacket *DvFrame)
|
|
|
|
{
|
|
|
|
// tag packet as remote peer origin
|
|
|
|
DvFrame->SetRemotePeerOrigin();
|
|
|
|
|
|
|
|
// anc call base class
|
|
|
|
CDextraProtocol::OnDvLastFramePacketIn(DvFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// packet decoding helpers
|
|
|
|
|
|
|
|
bool CXlxProtocol::IsValidKeepAlivePacket(const CBuffer &Buffer, CCallsign *callsign)
|
|
|
|
{
|
|
|
|
bool valid = false;
|
|
|
|
if (Buffer.size() == 9)
|
|
|
|
{
|
|
|
|
callsign->SetCallsign(Buffer.data(), 8);
|
|
|
|
valid = callsign->IsValid();
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-16 05:28:32 -05:00
|
|
|
bool CXlxProtocol::IsValidConnectPacket(const CBuffer &Buffer, CCallsign *callsign, char *modules, uint8 *major, uint8 *minor, uint8 *rev)
|
2016-02-11 12:51:12 -05:00
|
|
|
{
|
|
|
|
bool valid = false;
|
|
|
|
if ((Buffer.size() == 39) && (Buffer.data()[0] == 'L') && (Buffer.data()[38] == 0))
|
|
|
|
{
|
|
|
|
callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8);
|
|
|
|
::strcpy(modules, (const char *)&(Buffer.data()[12]));
|
|
|
|
valid = callsign->IsValid();
|
2016-12-16 05:28:32 -05:00
|
|
|
*major = Buffer.data()[9];
|
|
|
|
*minor = Buffer.data()[10];
|
|
|
|
*rev = Buffer.data()[11];
|
2016-02-11 12:51:12 -05:00
|
|
|
for ( int i = 0; i < ::strlen(modules); i++ )
|
|
|
|
{
|
|
|
|
valid &= IsLetter(modules[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXlxProtocol::IsValidDisconnectPacket(const CBuffer &Buffer, CCallsign *callsign)
|
|
|
|
{
|
|
|
|
bool valid = false;
|
|
|
|
if ((Buffer.size() == 10) && (Buffer.data()[0] == 'U') && (Buffer.data()[9] == 0))
|
|
|
|
{
|
|
|
|
callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8);
|
|
|
|
valid = callsign->IsValid();
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXlxProtocol::IsValidAckPacket(const CBuffer &Buffer, CCallsign *callsign, char *modules)
|
|
|
|
{
|
|
|
|
bool valid = false;
|
|
|
|
if ((Buffer.size() == 39) && (Buffer.data()[0] == 'A') && (Buffer.data()[38] == 0))
|
|
|
|
{
|
|
|
|
callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8);
|
|
|
|
::strcpy(modules, (const char *)&(Buffer.data()[12]));
|
|
|
|
valid = callsign->IsValid();
|
|
|
|
for ( int i = 0; i < ::strlen(modules); i++ )
|
|
|
|
{
|
|
|
|
valid &= IsLetter(modules[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXlxProtocol::IsValidNackPacket(const CBuffer &Buffer, CCallsign *callsign)
|
|
|
|
{
|
|
|
|
bool valid = false;
|
|
|
|
if ((Buffer.size() == 10) && (Buffer.data()[0] == 'N') && (Buffer.data()[9] == 0))
|
|
|
|
{
|
|
|
|
callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8);
|
|
|
|
valid = callsign->IsValid();
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// packet encoding helpers
|
|
|
|
|
|
|
|
void CXlxProtocol::EncodeKeepAlivePacket(CBuffer *Buffer)
|
|
|
|
{
|
|
|
|
Buffer->Set(GetReflectorCallsign());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXlxProtocol::EncodeConnectPacket(CBuffer *Buffer, const char *Modules)
|
|
|
|
{
|
|
|
|
uint8 tag[] = { 'L' };
|
|
|
|
|
|
|
|
// tag
|
|
|
|
Buffer->Set(tag, sizeof(tag));
|
|
|
|
// our callsign
|
|
|
|
Buffer->resize(Buffer->size()+8);
|
|
|
|
g_Reflector.GetCallsign().GetCallsign(Buffer->data()+1);
|
|
|
|
// our version
|
|
|
|
Buffer->Append((uint8)VERSION_MAJOR);
|
|
|
|
Buffer->Append((uint8)VERSION_MINOR);
|
|
|
|
Buffer->Append((uint8)VERSION_REVISION);
|
|
|
|
// the modules we share
|
|
|
|
Buffer->Append(Modules);
|
|
|
|
Buffer->resize(39);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXlxProtocol::EncodeDisconnectPacket(CBuffer *Buffer)
|
|
|
|
{
|
|
|
|
uint8 tag[] = { 'U' };
|
|
|
|
|
|
|
|
// tag
|
|
|
|
Buffer->Set(tag, sizeof(tag));
|
|
|
|
// our callsign
|
|
|
|
Buffer->resize(Buffer->size()+8);
|
|
|
|
g_Reflector.GetCallsign().GetCallsign(Buffer->data()+1);
|
|
|
|
Buffer->Append((uint8)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXlxProtocol::EncodeConnectAckPacket(CBuffer *Buffer, const char *Modules)
|
|
|
|
{
|
|
|
|
uint8 tag[] = { 'A' };
|
|
|
|
|
|
|
|
// tag
|
|
|
|
Buffer->Set(tag, sizeof(tag));
|
|
|
|
// our callsign
|
|
|
|
Buffer->resize(Buffer->size()+8);
|
|
|
|
g_Reflector.GetCallsign().GetCallsign(Buffer->data()+1);
|
|
|
|
// our version
|
|
|
|
Buffer->Append((uint8)VERSION_MAJOR);
|
|
|
|
Buffer->Append((uint8)VERSION_MINOR);
|
|
|
|
Buffer->Append((uint8)VERSION_REVISION);
|
|
|
|
// the modules we share
|
|
|
|
Buffer->Append(Modules);
|
|
|
|
Buffer->resize(39);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXlxProtocol::EncodeConnectNackPacket(CBuffer *Buffer)
|
|
|
|
{
|
|
|
|
uint8 tag[] = { 'N' };
|
|
|
|
|
|
|
|
// tag
|
|
|
|
Buffer->Set(tag, sizeof(tag));
|
|
|
|
// our callsign
|
|
|
|
Buffer->resize(Buffer->size()+8);
|
|
|
|
g_Reflector.GetCallsign().GetCallsign(Buffer->data()+1);
|
|
|
|
Buffer->Append((uint8)0);
|
|
|
|
}
|
|
|
|
|