From 2c43f0e0357b4749b7199aeb54af1e747caee702 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Thu, 22 Mar 2018 20:41:18 +0000 Subject: [PATCH] Simplify the networking code. --- P25Parrot/Network.cpp | 43 ++----- P25Parrot/Network.h | 6 +- P25Parrot/P25Parrot.cpp | 1 - P25Parrot/P25Parrot.vcxproj | 1 - P25Parrot/P25Parrot.vcxproj.filters | 3 - P25Parrot/RingBuffer.h | 147 ---------------------- P25Reflector/P25Reflector.vcxproj | 1 - P25Reflector/P25Reflector.vcxproj.filters | 3 - P25Reflector/RingBuffer.h | 147 ---------------------- 9 files changed, 13 insertions(+), 339 deletions(-) delete mode 100644 P25Parrot/RingBuffer.h delete mode 100644 P25Reflector/RingBuffer.h diff --git a/P25Parrot/Network.cpp b/P25Parrot/Network.cpp index dd292c6..75c9e5a 100644 --- a/P25Parrot/Network.cpp +++ b/P25Parrot/Network.cpp @@ -27,8 +27,7 @@ const unsigned int BUFFER_LENGTH = 200U; CNetwork::CNetwork(unsigned int port) : m_socket(port), m_address(), -m_port(0U), -m_buffer(1000U, "P25 Network") +m_port(0U) { } @@ -53,44 +52,26 @@ 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 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; m_address.s_addr = address.s_addr; m_port = port; - if (buffer[0U] == 0xF0U) { // A poll - write(buffer, length); - } else if (buffer[0U] == 0xF1U) { // An unlink - // Nothing to do - } else { - 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 (data[0U] == 0xF0U) { // 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 (data[0U] == 0xF1U) { // An unlink + // Nothing to do + return 0U; + } else { + return length; + } } void CNetwork::end() diff --git a/P25Parrot/Network.h b/P25Parrot/Network.h index 135c578..607bed5 100644 --- a/P25Parrot/Network.h +++ b/P25Parrot/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 @@ -40,13 +39,10 @@ public: 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/P25Parrot/P25Parrot.cpp b/P25Parrot/P25Parrot.cpp index 6456d37..ab3bee8 100644 --- a/P25Parrot/P25Parrot.cpp +++ b/P25Parrot/P25Parrot.cpp @@ -119,7 +119,6 @@ void CP25Parrot::run() unsigned int ms = stopWatch.elapsed(); stopWatch.start(); - network.clock(ms); watchdogTimer.clock(ms); turnaroundTimer.clock(ms); diff --git a/P25Parrot/P25Parrot.vcxproj b/P25Parrot/P25Parrot.vcxproj index 4ca4c99..1b3bab9 100644 --- a/P25Parrot/P25Parrot.vcxproj +++ b/P25Parrot/P25Parrot.vcxproj @@ -149,7 +149,6 @@ - diff --git a/P25Parrot/P25Parrot.vcxproj.filters b/P25Parrot/P25Parrot.vcxproj.filters index acb98ba..4b63e88 100644 --- a/P25Parrot/P25Parrot.vcxproj.filters +++ b/P25Parrot/P25Parrot.vcxproj.filters @@ -20,9 +20,6 @@ Header Files - - Header Files - Header Files diff --git a/P25Parrot/RingBuffer.h b/P25Parrot/RingBuffer.h deleted file mode 100644 index f93e7a2..0000000 --- a/P25Parrot/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/P25Reflector/P25Reflector.vcxproj b/P25Reflector/P25Reflector.vcxproj index 53ed138..0367cab 100644 --- a/P25Reflector/P25Reflector.vcxproj +++ b/P25Reflector/P25Reflector.vcxproj @@ -25,7 +25,6 @@ - diff --git a/P25Reflector/P25Reflector.vcxproj.filters b/P25Reflector/P25Reflector.vcxproj.filters index 902b79d..8b2ddfc 100644 --- a/P25Reflector/P25Reflector.vcxproj.filters +++ b/P25Reflector/P25Reflector.vcxproj.filters @@ -29,9 +29,6 @@ Header Files - - Header Files - Header Files diff --git a/P25Reflector/RingBuffer.h b/P25Reflector/RingBuffer.h deleted file mode 100644 index 186709d..0000000 --- a/P25Reflector/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