fprime/Drv/TcpServer/TcpServerComponentImpl.hpp
Thomas Boyer-Chammard d0246f148b
Add Framer FPP interface, implement FprimeFramer and adapt ComQueue (#3486)
* Initial FprimeFramer and FprimePacketizer

* Code clarity + set up UTs

* Rework ComQueue and ComStub to use DataWithContext

* Add packets to RefPackets.fppi

* Fix ComQueue tests

* Add hotfix to FileDownlink instead of ComQueue

* Fix cancelPacket as well

* Fix ComQueue UTs by removing hotfix

* Refactor DataWithContext to use an FPP object for context instead of Fw.Buffer

* Touch up testing

* Add docs

* more docs

* More docs

* Rework buffer deallocation pattern to pass-through ComQueue

* Update ComStub UTs

* Restore original FileDownlink.cpp

* Formatting tweak

* Update deprecated getSerializeRepr() calls

* deserialization methods

* Fix spelling

* add cast for safety

* CMakefile change

* Bump ComQueue depth

* Update RPI deployment with new Downlink stack

* Rename comQueueIn port to comPktQueueIn

* Fix comQueueIn to comPktQueueIn change

* Remove legacy Svc.Framer

* Fix CMake UTs

* Fix RPI topology config

* Fix FprimeProtocol.fpp module

* Fix namespacing

* Use const reference for FrameContext port

* Review comments EXCEPT port passback refactor

* Rework ComStub with new ByteStream

* New ByteStream - ComInterface model

* Rework TcpClient / TcpServer with new bytestream

* Adapt UDP component for new ByteStream

* Adapt FrameAccumulator for new ByteStream

* Adapt FprimeFramer for new ByteStream

* Update Ref topology with new ByteStream model

* Remove all legacy deallocates from Drivers; reintroduce DEPRECATED model types

* Fix spelling and include error

* More spelling....

* RPI and RpiDemo fixes

* Fix conversion warning on RPI

* static_cast for short int on RPI

* Standardize port names

* Remove legacy Drv types and merge RECV/SEND enum type, delete StreamCrossover

* Update SDDs

* Update SDDs

* Fix ComInterface <-> Framer interfaction, clarify comments and fix annotations

* Switch ComStub from ASSERT to log failure and return buffer

* Add history size check + clarify test handler overrides

* Fix RPI topology to wire comStub on Uplink

* Rename comm to comDriver in RPI topology

* Update communication adapter interface docs
2025-04-29 16:40:36 -07:00

172 lines
6.4 KiB
C++

// ======================================================================
// \title TcpServerComponentImpl.hpp
// \author mstarch
// \brief hpp file for TcpServerComponentImpl component implementation class
//
// \copyright
// Copyright 2009-2020, by the California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
//
// ======================================================================
#ifndef TcpServerComponentImpl_HPP
#define TcpServerComponentImpl_HPP
#include <IpCfg.hpp>
#include <Drv/Ip/IpSocket.hpp>
#include <Drv/Ip/SocketComponentHelper.hpp>
#include <Drv/Ip/TcpServerSocket.hpp>
#include "Drv/TcpServer/TcpServerComponentAc.hpp"
namespace Drv {
class TcpServerComponentImpl final : public TcpServerComponentBase, public SocketComponentHelper {
public:
// ----------------------------------------------------------------------
// Construction, initialization, and destruction
// ----------------------------------------------------------------------
/**
* \brief construct the TcpServer component.
* \param compName: name of this component
*/
TcpServerComponentImpl(const char* const compName);
/**
* \brief Destroy the component
*/
~TcpServerComponentImpl();
// ----------------------------------------------------------------------
// Helper methods to start and stop socket
// ----------------------------------------------------------------------
/**
* \brief Configures the TcpServer settings but does not open the connection
*
* The TcpServerComponent needs to listen for a remote TCP client. This call configures the hostname, port and
* send timeouts for that socket connection. This call should be performed on system startup before recv or send
* are called. Note: hostname must be a dot-notation IP address of the form "x.x.x.x". DNS translation is left up
* to the user.
*
* \param hostname: ip address of remote tcp server in the form x.x.x.x
* \param port: port of remote tcp server
* \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,
FwSizeType buffer_size = 1024);
/**
* \brief is started
*/
bool isStarted();
/**
* \brief startup the server socket for communications
*
* Start up the server socket by listening on a port. Note: does not accept clients, this is done in open to
* facilitate re-connection of clients.
*/
SocketIpStatus startup();
/**
* \brief terminate the server socket
*
* Close the server socket. Should be done after all clients are shutdown and closed.
*/
void terminate();
/**
* \brief get the port being listened on
*
* Most useful when listen was configured to use port "0", this will return the port used for listening after a port
* has been determined. Will return 0 if the connection has not been setup.
*
* \return receive port
*/
U16 getListenPort();
PROTECTED:
// ----------------------------------------------------------------------
// Implementations for socket read task virtual methods
// ----------------------------------------------------------------------
/**
* \brief returns a reference to the socket handler
*
* Gets a reference to the current socket handler in order to operate generically on the IpSocket instance. Used for
* receive, and open calls. This socket handler will be a TcpServer.
*
* \return IpSocket reference
*/
IpSocket& getSocketHandler() override;
/**
* \brief returns a buffer to fill with data
*
* Gets a reference to a buffer to fill with data. This allows the component to determine how to provide a
* buffer and the socket read task just fills said buffer.
*
* \return Fw::Buffer to fill with data
*/
Fw::Buffer getBuffer() override;
/**
* \brief sends a buffer to be filled with data
*
* Sends the buffer gotten by getBuffer that has now been filled with data. This is used to delegate to the
* component how to send back the buffer. Ignores buffers with error status error.
*
* \return Fw::Buffer filled with data to send out
*/
void sendBuffer(Fw::Buffer buffer, SocketIpStatus status) override;
/**
* \brief called when the IPv4 system has been connected
*/
void connected() override;
/**
* \brief read from the socket, overridden to start and terminate the server socket
*/
void readLoop() override;
PRIVATE:
// ----------------------------------------------------------------------
// Handler implementations for user-defined typed input ports
// ----------------------------------------------------------------------
/**
* \brief Send data out of the TcpServer
*
* Passing data to this port will send data from the TcpServer to whatever TCP client this component has connected
* to. Should the socket not be opened or was disconnected, then this port call will return SEND_RETRY and critical
* transmissions should be retried. OTHER_ERROR indicates an unresolvable error. OP_OK is returned when the data
* has been sent.
*
* Note: this component delegates the reopening of the socket to the read thread and thus the caller should retry
* after the read thread has attempted to reopen the port but does not need to reopen the port manually.
*
* \param portNum: fprime port number of the incoming port call
* \param fwBuffer: buffer containing data to be sent
*/
void send_handler(const FwIndexType 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
#endif // end TcpServerComponentImpl