diff --git a/NXDNParrot/NXDNParrot.cpp b/NXDNParrot/NXDNParrot.cpp index d52f826..c26bbb1 100644 --- a/NXDNParrot/NXDNParrot.cpp +++ b/NXDNParrot/NXDNParrot.cpp @@ -80,7 +80,7 @@ void CNXDNParrot::run() for (;;) { unsigned char buffer[200U]; - unsigned int len = network.read(buffer); + unsigned int len = network.read(buffer, 200U); if (len > 0U) { parrot.write(buffer, len); watchdogTimer.start(); @@ -120,7 +120,6 @@ void CNXDNParrot::run() unsigned int ms = stopWatch.elapsed(); stopWatch.start(); - network.clock(ms); watchdogTimer.clock(ms); turnaroundTimer.clock(ms); diff --git a/NXDNParrot/NXDNParrot.vcxproj b/NXDNParrot/NXDNParrot.vcxproj index c1d6ca3..ada9a49 100644 --- a/NXDNParrot/NXDNParrot.vcxproj +++ b/NXDNParrot/NXDNParrot.vcxproj @@ -149,7 +149,6 @@ - diff --git a/NXDNParrot/NXDNParrot.vcxproj.filters b/NXDNParrot/NXDNParrot.vcxproj.filters index aaaed0a..a02a136 100644 --- a/NXDNParrot/NXDNParrot.vcxproj.filters +++ b/NXDNParrot/NXDNParrot.vcxproj.filters @@ -20,9 +20,6 @@ Header Files - - Header Files - Header Files diff --git a/NXDNParrot/Network.cpp b/NXDNParrot/Network.cpp index 73ad5d9..6942556 100644 --- a/NXDNParrot/Network.cpp +++ b/NXDNParrot/Network.cpp @@ -22,13 +22,10 @@ #include #include -const unsigned int BUFFER_LENGTH = 200U; - CNetwork::CNetwork(unsigned int port) : m_socket(port), m_address(), -m_port(0U), -m_buffer(1000U, "NXDN Network") +m_port(0U) { } @@ -53,41 +50,25 @@ bool CNetwork::write(const unsigned char* data, unsigned int length) return m_socket.write(data, length, m_address, m_port); } -void CNetwork::clock(unsigned int ms) +unsigned int CNetwork::read(unsigned char* data, unsigned int len) { - 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(data, len, address, port); if (length <= 0) - return; + return 0U; m_address.s_addr = address.s_addr; m_port = port; - if (::memcmp(buffer, "NXDNP", 5U) == 0 && length == 15) { // A poll - write(buffer, length); - } else if (::memcmp(buffer, "NXDND", 5U) == 0 && length == 43) { - unsigned char l = length; - m_buffer.addData(&l, 1U); - m_buffer.addData(buffer, length); - } -} - -unsigned int CNetwork::read(unsigned char* data) -{ - assert(data != NULL); - - if (m_buffer.isEmpty()) + if (::memcmp(data, "NXDNP", 5U) == 0 && length == 15) { // A poll + write(data, length); return 0U; - - unsigned char len = 0U; - m_buffer.getData(&len, 1U); - - m_buffer.getData(data, len); - - return len; + } else if (::memcmp(data, "NXDND", 5U) == 0 && length == 43) { + return 43U; + } else { + return 0U; + } } void CNetwork::end() diff --git a/NXDNParrot/Network.h b/NXDNParrot/Network.h index 135c578..d9cc008 100644 --- a/NXDNParrot/Network.h +++ b/NXDNParrot/Network.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2014,2016 by Jonathan Naylor G4KLX + * Copyright (C) 2009-2014,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 @@ -19,7 +19,6 @@ #ifndef Network_H #define Network_H -#include "RingBuffer.h" #include "UDPSocket.h" #include @@ -34,19 +33,16 @@ public: bool write(const unsigned char* data, unsigned int length); - unsigned int read(unsigned char* data); + unsigned int read(unsigned char* data, unsigned int length); void end(); void close(); - void clock(unsigned int ms); - private: CUDPSocket m_socket; in_addr m_address; unsigned int m_port; - CRingBuffer m_buffer; }; #endif diff --git a/NXDNParrot/RingBuffer.h b/NXDNParrot/RingBuffer.h deleted file mode 100644 index f93e7a2..0000000 --- a/NXDNParrot/RingBuffer.h +++ /dev/null @@ -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 -#include -#include - -template 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 diff --git a/NXDNReflector/NXCoreNetwork.cpp b/NXDNReflector/NXCoreNetwork.cpp index de5eb2b..189ac70 100644 --- a/NXDNReflector/NXCoreNetwork.cpp +++ b/NXDNReflector/NXCoreNetwork.cpp @@ -31,8 +31,7 @@ const unsigned int NXCORE_PORT = 41300U; CNXCoreNetwork::CNXCoreNetwork(const std::string& address, bool debug) : m_socket(NXCORE_PORT), m_address(), -m_debug(debug), -m_buffer(1000U, "NXCore Network") +m_debug(debug) { assert(!address.empty()); @@ -81,45 +80,35 @@ bool CNXCoreNetwork::write(const unsigned char* data, unsigned int len) return m_socket.write(buffer, 102U, m_address, NXCORE_PORT); } -void CNXCoreNetwork::clock(unsigned int ms) +unsigned int CNXCoreNetwork::read(unsigned char* data, unsigned int len) { 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(data, BUFFER_LENGTH, address, port); if (length <= 0) - return; + return 0U; // Check if the data is for us if (m_address.s_addr != address.s_addr || port != NXCORE_PORT) { LogMessage("NXCore packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, NXCORE_PORT, port); - return; + return 0U; } // Invalid packet type? if (::memcmp(buffer, "ICOM", 4U) != 0) - return; + return 0U; if (length != 102) - return; + return 0U; if (m_debug) CUtils::dump(1U, "NXCore Network Data Received", buffer, length); - m_buffer.addData(buffer + 40U, 33U); -} + ::memcpy(data, buffer + 40U, 33U); -bool CNXCoreNetwork::read(unsigned char* data, unsigned int len) -{ - assert(data != NULL); - - if (m_buffer.isEmpty()) - return false; - - m_buffer.getData(data, 33U); - - return true; + return 33U; } void CNXCoreNetwork::close() diff --git a/NXDNReflector/NXCoreNetwork.h b/NXDNReflector/NXCoreNetwork.h index 5ac90a7..99c016d 100644 --- a/NXDNReflector/NXCoreNetwork.h +++ b/NXDNReflector/NXCoreNetwork.h @@ -19,7 +19,6 @@ #ifndef NXCoreNetwork_H #define NXCoreNetwork_H -#include "RingBuffer.h" #include "UDPSocket.h" #include "Timer.h" @@ -35,17 +34,14 @@ public: bool write(const unsigned char* data, unsigned int len); - bool read(unsigned char* data, unsigned int len); + unsigned int read(unsigned char* data, unsigned int len); void close(); - void clock(unsigned int ms); - private: - CUDPSocket m_socket; - in_addr m_address; - bool m_debug; - CRingBuffer m_buffer; + CUDPSocket m_socket; + in_addr m_address; + bool m_debug; }; #endif diff --git a/NXDNReflector/NXDNNetwork.cpp b/NXDNReflector/NXDNNetwork.cpp index 091b82a..e6d05f2 100644 --- a/NXDNReflector/NXDNNetwork.cpp +++ b/NXDNReflector/NXDNNetwork.cpp @@ -98,6 +98,13 @@ unsigned int CNXDNNetwork::read(unsigned char* data, unsigned int length, in_add if (len <= 0) return 0U; + // Invalid packet type? + if (::memcmp(data, "NXDN", 4U) != 0) + return 0U; + + if (length != 15 && length != 43) + return 0U; + if (m_debug) CUtils::dump(1U, "NXDN Network Data Received", data, len); diff --git a/NXDNReflector/NXDNReflector.cpp b/NXDNReflector/NXDNReflector.cpp index 08712e9..f1337de 100644 --- a/NXDNReflector/NXDNReflector.cpp +++ b/NXDNReflector/NXDNReflector.cpp @@ -370,9 +370,6 @@ void CNXDNReflector::run() dumpTimer.start(); } - if (m_nxCoreNetwork != NULL) - m_nxCoreNetwork->clock(ms); - if (ms < 5U) CThread::sleep(5U); } diff --git a/NXDNReflector/NXDNReflector.vcxproj b/NXDNReflector/NXDNReflector.vcxproj index 27d0cbf..d72db0b 100644 --- a/NXDNReflector/NXDNReflector.vcxproj +++ b/NXDNReflector/NXDNReflector.vcxproj @@ -26,7 +26,6 @@ - diff --git a/NXDNReflector/NXDNReflector.vcxproj.filters b/NXDNReflector/NXDNReflector.vcxproj.filters index a479108..e361874 100644 --- a/NXDNReflector/NXDNReflector.vcxproj.filters +++ b/NXDNReflector/NXDNReflector.vcxproj.filters @@ -26,9 +26,6 @@ Header Files - - Header Files - Header Files diff --git a/NXDNReflector/RingBuffer.h b/NXDNReflector/RingBuffer.h deleted file mode 100644 index 186709d..0000000 --- a/NXDNReflector/RingBuffer.h +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright (C) 2006-2009,2012,2013,2015,2016 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 -#include -#include - -template 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