Simlify the Icom networking section.

This commit is contained in:
Jonathan Naylor 2018-03-16 07:07:33 +00:00
parent e1d2d52cc3
commit 8d7772ab75
9 changed files with 28 additions and 210 deletions

View File

@ -40,7 +40,6 @@ m_file(file),
m_callsign(), m_callsign(),
m_rptAddress(), m_rptAddress(),
m_rptPort(0U), m_rptPort(0U),
m_myAddress(),
m_myPort(0U), m_myPort(0U),
m_rptDebug(false), m_rptDebug(false),
m_daemon(false), m_daemon(false),
@ -114,8 +113,6 @@ bool CConf::read()
m_rptAddress = value; m_rptAddress = value;
else if (::strcmp(key, "RptPort") == 0) else if (::strcmp(key, "RptPort") == 0)
m_rptPort = (unsigned int)::atoi(value); m_rptPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "LocalAddress") == 0)
m_myAddress = value;
else if (::strcmp(key, "LocalPort") == 0) else if (::strcmp(key, "LocalPort") == 0)
m_myPort = (unsigned int)::atoi(value); m_myPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "Debug") == 0) else if (::strcmp(key, "Debug") == 0)
@ -181,11 +178,6 @@ unsigned int CConf::getRptPort() const
return m_rptPort; return m_rptPort;
} }
std::string CConf::getMyAddress() const
{
return m_myAddress;
}
unsigned int CConf::getMyPort() const unsigned int CConf::getMyPort() const
{ {
return m_myPort; return m_myPort;

View File

@ -34,7 +34,6 @@ public:
std::string getCallsign() const; std::string getCallsign() const;
std::string getRptAddress() const; std::string getRptAddress() const;
unsigned int getRptPort() const; unsigned int getRptPort() const;
std::string getMyAddress() const;
unsigned int getMyPort() const; unsigned int getMyPort() const;
bool getRptDebug() const; bool getRptDebug() const;
bool getDaemon() const; bool getDaemon() const;
@ -68,7 +67,6 @@ private:
std::string m_callsign; std::string m_callsign;
std::string m_rptAddress; std::string m_rptAddress;
unsigned int m_rptPort; unsigned int m_rptPort;
std::string m_myAddress;
unsigned int m_myPort; unsigned int m_myPort;
bool m_rptDebug; bool m_rptDebug;
bool m_daemon; bool m_daemon;

View File

@ -26,17 +26,10 @@
const unsigned int BUFFER_LENGTH = 200U; const unsigned int BUFFER_LENGTH = 200U;
CIcomNetwork::CIcomNetwork(const std::string& localAddress, unsigned int localPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug) : CIcomNetwork::CIcomNetwork(unsigned int localPort, bool debug) :
m_socket(localAddress, localPort), m_socket(localPort),
m_address(), m_debug(debug)
m_port(gatewayPort),
m_debug(debug),
m_buffer(1000U, "Icom")
{ {
assert(gatewayPort > 0U);
assert(!gatewayAddress.empty());
m_address = CUDPSocket::lookup(gatewayAddress);
} }
CIcomNetwork::~CIcomNetwork() CIcomNetwork::~CIcomNetwork()
@ -53,7 +46,7 @@ bool CIcomNetwork::open()
return m_socket.open(); return m_socket.open();
} }
bool CIcomNetwork::write(const unsigned char* data, unsigned int length) bool CIcomNetwork::write(const unsigned char* data, unsigned int length, const in_addr& address, unsigned int port)
{ {
assert(data != NULL); assert(data != NULL);
@ -78,22 +71,22 @@ bool CIcomNetwork::write(const unsigned char* data, unsigned int length)
if (m_debug) if (m_debug)
CUtils::dump(1U, "Icom Data Sent", buffer, 102U); CUtils::dump(1U, "Icom Data Sent", buffer, 102U);
return m_socket.write(buffer, 102U, m_address, m_port); return m_socket.write(buffer, 102U, address, port);
} }
void CIcomNetwork::clock(unsigned int ms) bool CIcomNetwork::read(unsigned char* data, in_addr& address, unsigned int& port)
{ {
assert(data != NULL);
unsigned char buffer[BUFFER_LENGTH]; unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); int length = m_socket.read(buffer, BUFFER_LENGTH, address, port);
if (length <= 0) if (length <= 0)
return; return false;
// Invalid packet type? // Invalid packet type?
if (::memcmp(buffer, "ICOM", 4U) != 0) if (::memcmp(buffer, "ICOM", 4U) != 0)
return; return false;
// An Icom repeater connect request // An Icom repeater connect request
if (buffer[4U] == 0x01U && buffer[5U] == 0x61U) { if (buffer[4U] == 0x01U && buffer[5U] == 0x61U) {
@ -101,27 +94,17 @@ void CIcomNetwork::clock(unsigned int ms)
buffer[37U] = 0x02U; buffer[37U] = 0x02U;
buffer[38U] = 0x4FU; buffer[38U] = 0x4FU;
buffer[39U] = 0x4BU; buffer[39U] = 0x4BU;
m_socket.write(buffer, length, m_address, m_port); m_socket.write(buffer, length, address, port);
return; return false;
} }
if (length != 102) if (length != 102)
return; return false;
if (m_debug) if (m_debug)
CUtils::dump(1U, "Icom Data Received", buffer, length); CUtils::dump(1U, "Icom Data Received", buffer, length);
m_buffer.addData(buffer + 40U, 33U); ::memcpy(data, buffer + 40U, 33U);
}
bool CIcomNetwork::read(unsigned char* data)
{
assert(data != NULL);
if (m_buffer.isEmpty())
return false;
m_buffer.getData(data, 33U);
return true; return true;
} }

View File

@ -19,7 +19,6 @@
#ifndef IcomNetwork_H #ifndef IcomNetwork_H
#define IcomNetwork_H #define IcomNetwork_H
#include "RingBuffer.h"
#include "UDPSocket.h" #include "UDPSocket.h"
#include "Timer.h" #include "Timer.h"
@ -28,25 +27,22 @@
class CIcomNetwork { class CIcomNetwork {
public: public:
CIcomNetwork(const std::string& localAddress, unsigned int localPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug); CIcomNetwork(unsigned int localPort, bool debug);
~CIcomNetwork(); ~CIcomNetwork();
bool open(); bool open();
bool write(const unsigned char* data, unsigned int length); bool write(const unsigned char* data, unsigned int length, const in_addr& address, unsigned int port);
bool read(unsigned char* data); bool read(unsigned char* data, in_addr& address, unsigned int& port);
void close(); void close();
void clock(unsigned int ms);
private: private:
CUDPSocket m_socket; CUDPSocket m_socket;
in_addr m_address; in_addr m_address;
unsigned int m_port; unsigned int m_port;
bool m_debug; bool m_debug;
CRingBuffer<unsigned char> m_buffer;
}; };
#endif #endif

View File

@ -161,7 +161,10 @@ void CNXDNGateway::run()
} }
#endif #endif
CIcomNetwork localNetwork(m_conf.getMyAddress(), m_conf.getMyPort(), m_conf.getRptAddress(), m_conf.getRptPort(), m_conf.getRptDebug()); in_addr rptAddr = CUDPSocket::lookup(m_conf.getRptAddress());
unsigned int rptPort = m_conf.getRptPort();
CIcomNetwork localNetwork(m_conf.getMyPort(), m_conf.getRptDebug());
ret = localNetwork.open(); ret = localNetwork.open();
if (!ret) { if (!ret) {
::LogFinalise(); ::LogFinalise();
@ -243,7 +246,7 @@ void CNXDNGateway::run()
if (currentId != 9999U && currentAddr.s_addr == address.s_addr && currentPort == port) { if (currentId != 9999U && currentAddr.s_addr == address.s_addr && currentPort == port) {
// Don't pass reflector control data through to the MMDVM // Don't pass reflector control data through to the MMDVM
if (::memcmp(buffer, "NXDND", 5U) == 0) if (::memcmp(buffer, "NXDND", 5U) == 0)
localNetwork.write(buffer + 10U, len - 10U); localNetwork.write(buffer + 10U, len - 10U, rptAddr, rptPort);
// Any network activity is proof that the reflector is alive // Any network activity is proof that the reflector is alive
lostTimer.start(); lostTimer.start();
@ -251,7 +254,7 @@ void CNXDNGateway::run()
} }
// From the MMDVM to the reflector or control data // From the MMDVM to the reflector or control data
len = localNetwork.read(buffer); len = localNetwork.read(buffer, address, port);
if (len > 0U) { if (len > 0U) {
if (buffer[0U] == 0x81U || buffer[0U] == 0x83U) { if (buffer[0U] == 0x81U || buffer[0U] == 0x83U) {
grp = (buffer[7U] & 0x20U) == 0x20U; grp = (buffer[7U] & 0x20U) == 0x20U;
@ -322,7 +325,7 @@ void CNXDNGateway::run()
if (voice != NULL) { if (voice != NULL) {
unsigned int length = voice->read(buffer); unsigned int length = voice->read(buffer);
if (length > 0U) if (length > 0U)
localNetwork.write(buffer, length); localNetwork.write(buffer, length, rptAddr, rptPort);
} }
unsigned int ms = stopWatch.elapsed(); unsigned int ms = stopWatch.elapsed();
@ -371,8 +374,6 @@ void CNXDNGateway::run()
lostTimer.stop(); lostTimer.stop();
} }
localNetwork.clock(ms);
if (ms < 5U) if (ms < 5U)
CThread::sleep(5U); CThread::sleep(5U);
} }

View File

@ -2,7 +2,6 @@
Callsign=G4KLX Callsign=G4KLX
RptAddress=127.0.0.1 RptAddress=127.0.0.1
RptPort=14021 RptPort=14021
LocalAddress=127.0.0.1
LocalPort=14020 LocalPort=14020
Debug=0 Debug=0
Daemon=1 Daemon=1

View File

@ -155,7 +155,6 @@
<ClInclude Include="NXDNLookup.h" /> <ClInclude Include="NXDNLookup.h" />
<ClInclude Include="NXDNNetwork.h" /> <ClInclude Include="NXDNNetwork.h" />
<ClInclude Include="Reflectors.h" /> <ClInclude Include="Reflectors.h" />
<ClInclude Include="RingBuffer.h" />
<ClInclude Include="StopWatch.h" /> <ClInclude Include="StopWatch.h" />
<ClInclude Include="Thread.h" /> <ClInclude Include="Thread.h" />
<ClInclude Include="Timer.h" /> <ClInclude Include="Timer.h" />

View File

@ -53,9 +53,6 @@
<ClInclude Include="NXDNNetwork.h"> <ClInclude Include="NXDNNetwork.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="RingBuffer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Voice.h"> <ClInclude Include="Voice.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>

View File

@ -1,147 +0,0 @@
/*
* Copyright (C) 2006-2009,2012,2013,2015,2016,2018 by Jonathan Naylor G4KLX
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef RingBuffer_H
#define RingBuffer_H
#include <cstdio>
#include <cassert>
#include <cstring>
template<class T> class CRingBuffer {
public:
CRingBuffer(unsigned int length, const char* name) :
m_length(length),
m_name(name),
m_buffer(NULL),
m_iPtr(0U),
m_oPtr(0U)
{
assert(length > 0U);
assert(name != NULL);
m_buffer = new T[length];
::memset(m_buffer, 0x00, m_length * sizeof(T));
}
~CRingBuffer()
{
delete[] m_buffer;
}
bool addData(const T* buffer, unsigned int nSamples)
{
if (nSamples >= freeSpace()) {
::fprintf(stderr, "**** Overflow in %s ring buffer, %u >= %u\n", m_name, nSamples, freeSpace());
return false;
}
for (unsigned int i = 0U; i < nSamples; i++) {
m_buffer[m_iPtr++] = buffer[i];
if (m_iPtr == m_length)
m_iPtr = 0U;
}
return true;
}
bool getData(T* buffer, unsigned int nSamples)
{
if (dataSize() < nSamples) {
::fprintf(stderr, "**** Underflow in %s ring buffer, %u < %u\n", m_name, dataSize(), nSamples);
return false;
}
for (unsigned int i = 0U; i < nSamples; i++) {
buffer[i] = m_buffer[m_oPtr++];
if (m_oPtr == m_length)
m_oPtr = 0U;
}
return true;
}
bool peek(T* buffer, unsigned int nSamples)
{
if (dataSize() < nSamples) {
::fprintf(stderr, "**** Underflow peek in %s ring buffer, %u < %u\n", m_name, dataSize(), nSamples);
return false;
}
unsigned int ptr = m_oPtr;
for (unsigned int i = 0U; i < nSamples; i++) {
buffer[i] = m_buffer[ptr++];
if (ptr == m_length)
ptr = 0U;
}
return true;
}
void clear()
{
m_iPtr = 0U;
m_oPtr = 0U;
::memset(m_buffer, 0x00, m_length * sizeof(T));
}
unsigned int freeSpace() const
{
if (m_oPtr == m_iPtr)
return m_length;
if (m_oPtr > m_iPtr)
return m_oPtr - m_iPtr;
return (m_length + m_oPtr) - m_iPtr;
}
unsigned int dataSize() const
{
return m_length - freeSpace();
}
bool hasSpace(unsigned int length) const
{
return freeSpace() > length;
}
bool hasData() const
{
return m_oPtr != m_iPtr;
}
bool isEmpty() const
{
return m_oPtr == m_iPtr;
}
private:
unsigned int m_length;
const char* m_name;
T* m_buffer;
unsigned int m_iPtr;
unsigned int m_oPtr;
};
#endif