F´ Flight Software - C/C++ Documentation
devel
A framework for building embedded system applications to NASA flight quality standards.
|
Helper base-class for setting up Berkeley sockets. More...
#include <Drv/Ip/IpSocket.hpp>
Public Member Functions | |
IpSocket () | |
virtual | ~IpSocket () |
SocketIpStatus | configure (const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds) |
configure the ip socket with host and transmission timeouts More... | |
SocketIpStatus | open (SocketDescriptor &socketDescriptor) |
open the IP socket for communications More... | |
SocketIpStatus | send (const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) |
send data out the IP socket from the given buffer More... | |
SocketIpStatus | recv (const SocketDescriptor &fd, U8 *const data, U32 &size) |
receive data from the IP socket from the given buffer More... | |
void | close (const SocketDescriptor &socketDescriptor) |
closes the socket More... | |
void | shutdown (const SocketDescriptor &socketDescriptor) |
shutdown the socket More... | |
Protected Member Functions | |
virtual bool | isValidPort (U16 port) |
Check if the given port is valid for the socket. More... | |
SocketIpStatus | setupTimeouts (PlatformIntType socketFd) |
setup the socket timeout properties of the opened outgoing socket More... | |
virtual SocketIpStatus | openProtocol (SocketDescriptor &fd)=0 |
Protocol specific open implementation, called from open. More... | |
virtual I32 | sendProtocol (const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size)=0 |
Protocol specific implementation of send. Called directly with retry from send. More... | |
virtual I32 | recvProtocol (const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size)=0 |
Protocol specific implementation of recv. Called directly with error handling from recv. More... | |
Static Protected Member Functions | |
static SocketIpStatus | addressToIp4 (const char *address, void *ip4) |
converts a given address in dot form x.x.x.x to an ip address. ONLY works for IPv4. More... | |
Protected Attributes | |
U32 | m_timeoutSeconds |
U32 | m_timeoutMicroseconds |
U16 | m_port |
IP address port used. More... | |
char | m_hostname [SOCKET_MAX_HOSTNAME_SIZE] |
Hostname to supply. More... | |
Helper base-class for setting up Berkeley sockets.
Certain IP headers have conflicting definitions with the m_data member of various types in fprime. TcpHelper separates the ip setup from the incoming Fw::Buffer in the primary component class preventing this collision.
Definition at line 55 of file IpSocket.hpp.
Drv::IpSocket::IpSocket | ( | ) |
Definition at line 49 of file IpSocket.cpp.
|
inlinevirtual |
Definition at line 58 of file IpSocket.hpp.
|
staticprotected |
converts a given address in dot form x.x.x.x to an ip address. ONLY works for IPv4.
address | address to convert |
ip4 | IPv4 representation structure to fill |
Definition at line 85 of file IpSocket.cpp.
void Drv::IpSocket::close | ( | const SocketDescriptor & | socketDescriptor | ) |
closes the socket
Closes the socket opened by the open call. In this case of the TcpServer, this does NOT close server's listening port but will close the active client connection.
socketDescriptor | socket descriptor to close |
Definition at line 106 of file IpSocket.cpp.
SocketIpStatus Drv::IpSocket::configure | ( | const char * | hostname, |
const U16 | port, | ||
const U32 | send_timeout_seconds, | ||
const U32 | send_timeout_microseconds | ||
) |
configure the ip socket with host and transmission timeouts
Configures the IP handler (Tcp, Tcp server, and Udp) to use the given hostname and port. When multiple ports are used for send/receive these settings affect the send direction (as is the case for udp). 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.
Note: for UDP sockets this is equivalent to configureSend
and only sets up the transmission direction of the socket. A separate call to configureRecv
is required to receive on the socket and should be made before the open
call has been made.
hostname | socket uses for outgoing transmissions (and incoming when tcp). Must be of form x.x.x.x |
port | port socket uses for outgoing transmissions (and incoming when tcp). Must NOT be 0. |
send_timeout_seconds | send timeout seconds portion |
send_timeout_microseconds | send timeout microseconds portion. Must be less than 1000000 |
Definition at line 53 of file IpSocket.cpp.
|
protectedvirtual |
Check if the given port is valid for the socket.
Some ports should be allowed for sockets and disabled on others (e.g. port 0 is a valid tcp server port but not a client. This will check the port and return "true" if the port is valid, or "false" otherwise. In the default implementation, all ports are considered valid.
port | port to check |
Reimplemented in Drv::TcpClientSocket.
Definition at line 64 of file IpSocket.cpp.
SocketIpStatus Drv::IpSocket::open | ( | SocketDescriptor & | socketDescriptor | ) |
open the IP socket for communications
This will open the IP socket for communication. This method error checks and validates properties set using the configure
method. Tcp sockets will open bidirectional communication assuming the configure
function was previously called. Udp sockets allow configureRecv
and configure
/configureSend
calls to configure for each direction separately and may be operated in a single-direction or bidirectional mode. This call returns a status of SOCK_SEND means the port is ready for transmissions and any other status should be treated as an error with the socket not capable of sending nor receiving. This method will properly close resources on any unsuccessful status.
In the case of server components (TcpServer) this function will block until a client has connected.
Note: delegates to openProtocol for protocol specific implementation
socketDescriptor | socket descriptor to update with opened port |
Definition at line 119 of file IpSocket.cpp.
|
protectedpure virtual |
Protocol specific open implementation, called from open.
socketDescriptor | (output) socket descriptor opened. Only valid on SOCK_SUCCESS. Otherwise will be invalid |
Implemented in Drv::UdpSocket, Drv::TcpServerSocket, and Drv::TcpClientSocket.
SocketIpStatus Drv::IpSocket::recv | ( | const SocketDescriptor & | fd, |
U8 *const | data, | ||
U32 & | size | ||
) |
receive data from the IP socket from the given buffer
Receives data from the IpSocket. Should the socket be unavailable, SOCK_DISCONNECTED will be returned and the socket should be reopened using the open
call. This can happen even when the socket has already been opened should a transmission error/closure be detected. Since this blocks until data is available, it will retry as long as EINTR is set and less than a max number of iterations has passed. This function will block to receive data and will retry (up to a configured set of retries) as long as EINTR is returned.
Note: delegates to recvProtocol
to send the data
socketDescriptor | socket descriptor to recv from |
data | pointer to data to fill with received data |
size | maximum size of data buffer to fill |
Definition at line 163 of file IpSocket.cpp.
|
protectedpure virtual |
Protocol specific implementation of recv. Called directly with error handling from recv.
socket | socket descriptor to recv from |
data | data pointer to fill |
size | size of data buffer |
Implemented in Drv::UdpSocket, Drv::TcpServerSocket, and Drv::TcpClientSocket.
SocketIpStatus Drv::IpSocket::send | ( | const SocketDescriptor & | socketDescriptor, |
const U8 *const | data, | ||
const U32 | size | ||
) |
send data out the IP socket from the given buffer
Sends data out of the IpSocket. This outgoing transmission will be retried several times if the transmission fails to send all the data. Retries are globally configured in the IpCfg.hpp
header. Should the socket be unavailable, SOCK_DISCONNECTED is returned and the socket should be reopened using the open
call. This can happen even when the socket has already been opened should a transmission error/closure be detected. Unless an error is received, all data will have been transmitted.
Note: delegates to sendProtocol
to send the data
fd | file descriptor to send to |
data | pointer to data to send |
size | size of data to send |
Definition at line 131 of file IpSocket.cpp.
|
protectedpure virtual |
Protocol specific implementation of send. Called directly with retry from send.
socketDescriptor | socket descriptor to send to |
data | data to send |
size | size of data to send |
Implemented in Drv::UdpSocket, Drv::TcpServerSocket, and Drv::TcpClientSocket.
|
protected |
setup the socket timeout properties of the opened outgoing socket
socketDescriptor | socket descriptor to setup |
Definition at line 68 of file IpSocket.cpp.
void Drv::IpSocket::shutdown | ( | const SocketDescriptor & | socketDescriptor | ) |
shutdown the socket
Shuts down the socket opened by the open call. In this case of the TcpServer, this does shut down server's listening port, but rather shuts down the active client.
A shut down begins the termination of communication. The underlying socket will coordinate a clean shutdown, and it is safe to close the socket once a recv with 0 size has returned or an appropriate timeout has been reached.
socketDescriptor | socket descriptor to shutdown |
Definition at line 110 of file IpSocket.cpp.
|
protected |
Hostname to supply.
Definition at line 211 of file IpSocket.hpp.
|
protected |
IP address port used.
Definition at line 210 of file IpSocket.hpp.
|
protected |
Definition at line 209 of file IpSocket.hpp.
|
protected |
Definition at line 208 of file IpSocket.hpp.