17 #ifdef TGT_OS_TYPE_VXWORKS 29 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN 30 #include <sys/socket.h> 32 #include <arpa/inet.h> 34 #error OS not supported for IP Socket Communications 50 struct sockaddr_in address;
52 if ((serverFd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
56 address.sin_family = AF_INET;
57 address.sin_port = htons(this->
m_port);
60 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 61 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
70 if (::bind(serverFd, reinterpret_cast<struct sockaddr*>(&address),
sizeof(address)) < 0) {
75 socklen_t size =
sizeof(address);
76 if (::getsockname(serverFd, reinterpret_cast<struct sockaddr *>(&address), &size) == -1) {
81 if (::listen(serverFd, 1) < 0) {
87 socketDescriptor.
serverFd = serverFd;
88 this->
m_port = ntohs(address.sin_port);
101 if (serverFd == -1) {
106 clientFd = ::accept(serverFd,
nullptr,
nullptr);
117 socketDescriptor.
fd = clientFd;
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.
SocketIpStatus startup(SocketDescriptor &socketDescriptor)
Opens the server socket and listens, does not block.
SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size)
send data out the IP socket from the given buffer
Failed to configure socket.
static void log(const char *format,...)
log a formated string with supplied arguments
SocketIpStatus recv(const SocketDescriptor &fd, U8 *const data, U32 &size)
receive data from the IP socket from the given buffer
I32 recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv...
Failed to listen on socket.
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
U16 m_port
IP address port used.
Failed to accept connection.
Socket operation successful.
U16 getListenPort()
get the port being listened on
TcpServerSocket()
Constructor for client socket tcp implementation.
Failed to read back port from connection.
SocketIpStatus setupTimeouts(PlatformIntType socketFd)
setup the socket timeout properties of the opened outgoing socket
C++-compatible configuration header for fprime configuration.
uint8_t U8
8-bit unsigned integer
PlatformIntType fd
Used for all sockets to track the communication file descriptor.
void close(const SocketDescriptor &socketDescriptor)
closes the socket
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
PlatformIntType serverFd
Used for server sockets to track the listening file descriptor.
SocketIpStatus
Status enumeration for socket return values.
void terminate(const SocketDescriptor &socketDescriptor)
close the server socket created by the startup call
Socket has not been started.
Helper base-class for setting up Berkeley sockets.
Failed to bind to socket.
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
Tcp specific implementation for opening a client socket connected to this server. ...
I32 sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.