/** * Digital Voice Modem - Common Library * GPLv2 Open Source. Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * @package DVM / Common Library * */ /* * Copyright (C) 2024 by Bryan Biedenkapp N2PLL * * 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. */ #include "Defines.h" #include "network/RawFrameQueue.h" #include "network/UDPSocket.h" #include "Log.h" #include "Utils.h" using namespace network; #include #include #include #include // --------------------------------------------------------------------------- // Public Class Members // --------------------------------------------------------------------------- /// /// Initializes a new instance of the RawFrameQueue class. /// /// Local port used to listen for incoming data. /// RawFrameQueue::RawFrameQueue(UDPSocket* socket, bool debug) : m_socket(socket), m_buffers(), m_debug(debug) { /* stub */ } /// /// Finalizes a instance of the RawFrameQueue class. /// RawFrameQueue::~RawFrameQueue() { /* stub */ } /// /// Read message from the received UDP packet. /// /// Actual length of message read from packet. /// IP address data read from. /// /// Buffer containing message read. UInt8Array RawFrameQueue::read(int& messageLength, sockaddr_storage& address, uint32_t& addrLen) { messageLength = -1; // read message from socket uint8_t buffer[DATA_PACKET_LENGTH]; ::memset(buffer, 0x00U, DATA_PACKET_LENGTH); int length = m_socket->read(buffer, DATA_PACKET_LENGTH, address, addrLen); if (length < 0) { LogError(LOG_NET, "Failed reading data from the network"); return nullptr; } if (length > 0) { if (m_debug) Utils::dump(1U, "Network Packet", buffer, length); // copy message messageLength = length; UInt8Array message = std::unique_ptr(new uint8_t[length]); ::memcpy(message.get(), buffer, length); return message; } return nullptr; } /// /// Cache "message" to frame queue. /// /// Message buffer to frame and queue. /// Length of message. /// IP address to write data to. /// /// void RawFrameQueue::enqueueMessage(const uint8_t* message, uint32_t length, sockaddr_storage& addr, uint32_t addrLen) { assert(message != nullptr); assert(length > 0U); uint8_t* buffer = new uint8_t[length]; ::memset(buffer, 0x00U, length); ::memcpy(buffer, message, length); if (m_debug) Utils::dump(1U, "RawFrameQueue::enqueueMessage() Buffered Message", buffer, length); UDPDatagram* dgram = new UDPDatagram; dgram->buffer = buffer; dgram->length = length; dgram->address = addr; dgram->addrLen = addrLen; m_buffers.push_back(dgram); } /// /// Flush the message queue. /// /// bool RawFrameQueue::flushQueue() { if (m_buffers.empty()) { return false; } // bryanb: this is the same as above -- but for some assinine reason prevents // weirdness if (m_buffers.size() == 0U) { return false; } // LogDebug(LOG_NET, "m_buffers len = %u", m_buffers.size()); bool ret = true; if (!m_socket->write(m_buffers)) { LogError(LOG_NET, "Failed writing data to the network"); ret = false; } for (auto& buffer : m_buffers) { if (buffer != nullptr) { // LogDebug(LOG_NET, "deleting buffer, addr %p len %u", buffer->buffer, buffer->length); if (buffer->buffer != nullptr) { delete buffer->buffer; buffer->length = 0; buffer->buffer = nullptr; } delete buffer; buffer = nullptr; } } m_buffers.clear(); return ret; }