From 7b702f252916cf064a6890de567bdd3b970ab319 Mon Sep 17 00:00:00 2001 From: bdshenker Date: Tue, 29 Oct 2024 14:07:40 -0700 Subject: [PATCH] #2074 Parameterize buffer size in UDP and TCP Server. --- Drv/TcpServer/TcpServerComponentImpl.cpp | 10 +++++++++- Drv/TcpServer/TcpServerComponentImpl.hpp | 6 +++++- Drv/Udp/UdpComponentImpl.cpp | 8 ++++++-- Drv/Udp/UdpComponentImpl.hpp | 6 +++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Drv/TcpServer/TcpServerComponentImpl.cpp b/Drv/TcpServer/TcpServerComponentImpl.cpp index ce6502fd88..153f7519c3 100644 --- a/Drv/TcpServer/TcpServerComponentImpl.cpp +++ b/Drv/TcpServer/TcpServerComponentImpl.cpp @@ -10,6 +10,7 @@ // // ====================================================================== +#include #include #include #include "Fw/Types/Assert.hpp" @@ -27,7 +28,14 @@ TcpServerComponentImpl::TcpServerComponentImpl(const char* const compName) SocketIpStatus TcpServerComponentImpl::configure(const char* hostname, const U16 port, const U32 send_timeout_seconds, - const U32 send_timeout_microseconds) { + const U32 send_timeout_microseconds, + FwSizeType buffer_size) { + + // Check that ensures the configured buffer size fits within the limits fixed-width type, U32 + + FW_ASSERT(buffer_size <= std::numeric_limits::max(), static_cast(buffer_size)); + m_allocation_size = buffer_size; // Store the buffer size + // (void)m_socket.configure(hostname, port, send_timeout_seconds, send_timeout_microseconds); return startup(); } diff --git a/Drv/TcpServer/TcpServerComponentImpl.hpp b/Drv/TcpServer/TcpServerComponentImpl.hpp index 1a9fe9497c..158bf0836c 100644 --- a/Drv/TcpServer/TcpServerComponentImpl.hpp +++ b/Drv/TcpServer/TcpServerComponentImpl.hpp @@ -55,12 +55,14 @@ class TcpServerComponentImpl : public TcpServerComponentBase, public SocketCompo * \param send_timeout_seconds: send timeout seconds component. Defaults to: SOCKET_TIMEOUT_SECONDS * \param send_timeout_microseconds: send timeout microseconds component. Must be less than 1000000. Defaults to: * SOCKET_TIMEOUT_MICROSECONDS + * \param buffer_size: size of the buffer to be allocated. Defaults to 1024. * \return status of the configure */ SocketIpStatus configure(const char* hostname, const U16 port, const U32 send_timeout_seconds = SOCKET_SEND_TIMEOUT_SECONDS, - const U32 send_timeout_microseconds = SOCKET_SEND_TIMEOUT_MICROSECONDS); + const U32 send_timeout_microseconds = SOCKET_SEND_TIMEOUT_MICROSECONDS, + FwSizeType buffer_size = 1024); /** * \brief is started @@ -161,6 +163,8 @@ class TcpServerComponentImpl : public TcpServerComponentBase, public SocketCompo Drv::SendStatus send_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) override; Drv::TcpServerSocket m_socket; //!< Socket implementation + + FwSizeType m_allocation_size; //!< Member variable to store the buffer size }; } // end namespace Drv diff --git a/Drv/Udp/UdpComponentImpl.cpp b/Drv/Udp/UdpComponentImpl.cpp index d5ba8f3eed..fd9a509b08 100644 --- a/Drv/Udp/UdpComponentImpl.cpp +++ b/Drv/Udp/UdpComponentImpl.cpp @@ -10,6 +10,7 @@ // // ====================================================================== +#include #include #include #include @@ -32,7 +33,10 @@ SocketIpStatus UdpComponentImpl::configureSend(const char* hostname, return m_socket.configureSend(hostname, port, send_timeout_seconds, send_timeout_microseconds); } -SocketIpStatus UdpComponentImpl::configureRecv(const char* hostname, const U16 port) { +SocketIpStatus UdpComponentImpl::configureRecv(const char* hostname, const U16 port, FwSizeType buffer_size) { + FW_ASSERT(buffer_size <= std::numeric_limits::max(), static_cast(buffer_size)); + m_allocation_size = buffer_size; // Store the buffer size + return m_socket.configureRecv(hostname, port); } @@ -51,7 +55,7 @@ IpSocket& UdpComponentImpl::getSocketHandler() { } Fw::Buffer UdpComponentImpl::getBuffer() { - return allocate_out(0, 1024); + return allocate_out(0, static_cast(m_allocation_size)); } void UdpComponentImpl::sendBuffer(Fw::Buffer buffer, SocketIpStatus status) { diff --git a/Drv/Udp/UdpComponentImpl.hpp b/Drv/Udp/UdpComponentImpl.hpp index 7829c7ec9b..b91ffea369 100644 --- a/Drv/Udp/UdpComponentImpl.hpp +++ b/Drv/Udp/UdpComponentImpl.hpp @@ -70,9 +70,10 @@ class UdpComponentImpl : public UdpComponentBase, public SocketComponentHelper { * * \param hostname: ip address of remote tcp server in the form x.x.x.x * \param port: port of remote tcp server + * \param buffer_size: size of the buffer to be allocated. Defaults to 1024. * \return status of the configure */ - SocketIpStatus configureRecv(const char* hostname, const U16 port); + SocketIpStatus configureRecv(const char* hostname, const U16 port, FwSizeType buffer_size = 1024); /** * \brief get the port being received on @@ -147,7 +148,10 @@ class UdpComponentImpl : public UdpComponentBase, public SocketComponentHelper { * \return SEND_OK on success, SEND_RETRY when critical data should be retried and SEND_ERROR upon error */ Drv::SendStatus send_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer); + Drv::UdpSocket m_socket; //!< Socket implementation + + FwSizeType m_allocation_size; //!< Member variable to store the buffer size }; } // end namespace Drv