From b55b6b3d31c41a63e919407f63a6b1adf45ba9c0 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Sun, 6 Sep 2020 21:27:48 +0100 Subject: [PATCH] Handle YSF network options cleanly. --- YSFGateway/YSFNetwork.cpp | 23 ++++++++++++++--------- YSFGateway/YSFNetwork.h | 3 ++- YSFParrot/Network.cpp | 6 +++++- YSFReflector/Network.cpp | 10 +++++++--- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/YSFGateway/YSFNetwork.cpp b/YSFGateway/YSFNetwork.cpp index dad803e..0e54e8d 100644 --- a/YSFGateway/YSFNetwork.cpp +++ b/YSFGateway/YSFNetwork.cpp @@ -34,6 +34,7 @@ m_address(), m_port(0U), m_poll(NULL), m_options(NULL), +m_opt(), m_unlink(NULL), m_buffer(1000U, "YSF Network Buffer"), m_pollTimer(1000U, 5U), @@ -66,6 +67,7 @@ m_address(), m_port(0U), m_poll(NULL), m_options(NULL), +m_opt(), m_unlink(NULL), m_buffer(1000U, "YSF Network Buffer"), m_pollTimer(1000U, 5U) @@ -92,6 +94,8 @@ m_pollTimer(1000U, 5U) CYSFNetwork::~CYSFNetwork() { delete[] m_poll; + delete[] m_unlink; + delete[] m_options; } bool CYSFNetwork::open() @@ -141,22 +145,23 @@ void CYSFNetwork::writePoll(unsigned int count) for (unsigned int i = 0U; i < count; i++) m_socket.write(m_poll, 14U, m_address, m_port); - if (m_options != NULL) + if (!m_opt.empty()) m_socket.write(m_options, 50U, m_address, m_port); } void CYSFNetwork::setOptions(const std::string& options) { - std::string opt = options; - - if (opt.size() < 1) + if (options.empty()) { + m_opt.clear(); return; + } - opt.resize(50, ' '); + m_opt = options; - for (unsigned int i = 0U; i < (50 - 4 - YSF_CALLSIGN_LENGTH); i++) { - m_options[i + 4U + YSF_CALLSIGN_LENGTH] = opt.at(i); - } + m_opt.resize(50, ' '); + + for (unsigned int i = 0U; i < (50U - 4U - YSF_CALLSIGN_LENGTH); i++) + m_options[i + 4U + YSF_CALLSIGN_LENGTH] = m_opt.at(i); } void CYSFNetwork::writeUnlink(unsigned int count) @@ -200,7 +205,7 @@ void CYSFNetwork::clock(unsigned int ms) m_linked = true; - if (m_options != NULL) + if (!m_opt.empty()) m_socket.write(m_options, 50U, m_address, m_port); } diff --git a/YSFGateway/YSFNetwork.h b/YSFGateway/YSFNetwork.h index 2f89b69..3f3f100 100644 --- a/YSFGateway/YSFNetwork.h +++ b/YSFGateway/YSFNetwork.h @@ -41,7 +41,7 @@ public: void write(const unsigned char* data); void writePoll(unsigned int count = 1U); - void setOptions(const std::string& options = NULL); + void setOptions(const std::string& options = ""); void writeUnlink(unsigned int count = 1U); unsigned int read(unsigned char* data); @@ -57,6 +57,7 @@ private: unsigned int m_port; unsigned char* m_poll; unsigned char* m_options; + std::string m_opt; unsigned char* m_unlink; CRingBuffer m_buffer; CTimer m_pollTimer; diff --git a/YSFParrot/Network.cpp b/YSFParrot/Network.cpp index 262fe8f..5443522 100644 --- a/YSFParrot/Network.cpp +++ b/YSFParrot/Network.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2014,2016,2018 by Jonathan Naylor G4KLX + * Copyright (C) 2009-2014,2016,2018,2020 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 @@ -89,6 +89,10 @@ unsigned int CNetwork::read(unsigned char* data) return 0U; } + // Throw away incoming options messages + if (::memcmp(data, "YSFO", 4U) == 0) + return 0U; + // Handle incoming unlinks if (::memcmp(data, "YSFU", 4U) == 0) return 0U; diff --git a/YSFReflector/Network.cpp b/YSFReflector/Network.cpp index 2de7679..d4367ea 100644 --- a/YSFReflector/Network.cpp +++ b/YSFReflector/Network.cpp @@ -99,15 +99,19 @@ unsigned int CNetwork::readData(unsigned char* data, unsigned int length, in_add if (len <= 0) return 0U; + if (m_debug) + CUtils::dump(1U, "YSF Network Data Received", data, len); + + // Throw away any options messages + if (::memcmp(data, "YSFO", 4U) == 0) + return 0U; + // Handle incoming status requests if (::memcmp(data, "YSFS", 4U) == 0) { m_socket.write(m_status, 42U, address, port); return 0U; } - if (m_debug) - CUtils::dump(1U, "YSF Network Data Received", data, len); - return len; }