fprime/Drv/Ip/UdpSocket.hpp
M Starch 9ed68fdd06
Correcting shutdown/close usage; refactoring TcpServer (#2950)
* Correcting shutdown/close usage; refactoring TcpServer

* Fixing Tcp

* Review and CI fixes

* ...and the other half of the fixes
2024-10-16 09:36:34 -07:00

123 lines
5.2 KiB
C++

// ======================================================================
// \title UdpSocket.hpp
// \author mstarch
// \brief hpp file for UdpSocket core implementation classes
//
// \copyright
// Copyright 2009-2020, by the California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
//
// ======================================================================
#ifndef DRV_IP_UDPSOCKET_HPP_
#define DRV_IP_UDPSOCKET_HPP_
#include <FpConfig.hpp>
#include <Drv/Ip/IpSocket.hpp>
#include <IpCfg.hpp>
namespace Drv {
struct SocketState;
/**
* \brief Helper for setting up Udp using Berkeley sockets as a client
*
* Certain IP headers have conflicting definitions with the m_data member of various types in fprime. UdpSocket
* separates the ip setup from the incoming Fw::Buffer in the primary component class preventing this collision.
*/
class UdpSocket : public IpSocket {
public:
/**
* \brief Constructor for client socket udp implementation
*/
UdpSocket();
/**
* \brief to cleanup state created at instantiation
*/
virtual ~UdpSocket();
/**
* \brief configure the udp socket for outgoing transmissions
*
* Configures the UDP handler to use the given hostname and port for outgoing transmissions. Incoming hostname
* and port are configured using the `configureRecv` function call for UDP as it requires separate host/port pairs
* for outgoing and incoming transmissions. Hostname DNS translation is left up to the caller and thus hostname must
* be an IP address in dot-notation of the form "x.x.x.x". Port cannot be set to 0 as dynamic port assignment is not
* supported on remote ports. It is possible to configure the UDP port as a single-direction send port only.
*
* Note: delegates to `IpSocket::configure`
*
* \param hostname: socket uses for outgoing transmissions. Must be of form x.x.x.x
* \param port: port socket uses for outgoing transmissions. Must NOT be 0.
* \param send_timeout_seconds: send timeout seconds portion
* \param send_timeout_microseconds: send timeout microseconds portion. Must be less than 1000000
* \return status of configure
*/
SocketIpStatus configureSend(const char* hostname, const U16 port, const U32 send_timeout_seconds,
const U32 send_timeout_microseconds);
/**
* \brief configure the udp socket for incoming transmissions
*
* Configures the UDP handler to use the given hostname and port for incoming transmissions. Outgoing hostname
* and port are configured using the `configureSend` function call for UDP as it requires separate host/port pairs
* for outgoing and incoming transmissions. Hostname DNS translation is left up to the caller and thus hostname must
* be an IP address in dot-notation of the form "x.x.x.x". It is possible to configure the UDP port as a
* single-direction receive port only.
*
* \param hostname: socket uses for incoming transmissions. Must be of form x.x.x.x
* \param port: port socket uses for incoming transmissions.
* \return status of configure
*/
SocketIpStatus configureRecv(const char* hostname, const U16 port);
/**
* \brief get the port being received on
*
* Most useful when receive was configured to use port "0", this will return the port used for receiving data after
* a port has been determined. Will return 0 if the connection has not been setup.
*
* \return receive port
*/
U16 getRecvPort();
PROTECTED:
/**
* \brief bind the UDP to a port such that it can receive packets at the previously configured port
* \param socketDescriptor: socket descriptor used in bind
* \return status of the bind
*/
SocketIpStatus bind(const PlatformIntType fd);
/**
* \brief udp specific implementation for opening a socket.
* \param socketDescriptor: (output) file descriptor opened. Only valid on SOCK_SUCCESS. Otherwise will be invalid
* \return status of open
*/
SocketIpStatus openProtocol(SocketDescriptor& socketDescriptor) override;
/**
* \brief Protocol specific implementation of send. Called directly with retry from send.
* \param socketDescriptor: descriptor to send to
* \param data: data to send
* \param size: size of data to send
* \return: size of data sent, or -1 on error.
*/
I32 sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) override;
/**
* \brief Protocol specific implementation of recv. Called directly with error handling from recv.
* \param socketDescriptor: descriptor to recv from
* \param data: data pointer to fill
* \param size: size of data buffer
* \return: size of data received, or -1 on error.
*/
I32 recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) override;
private:
SocketState* m_state; //!< State storage
U16 m_recv_port; //!< IP address port used
char m_recv_hostname[SOCKET_MAX_HOSTNAME_SIZE]; //!< Hostname to supply
};
} // namespace Drv
#endif /* DRV_IP_UDPSOCKET_HPP_ */