F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
TcpClientSocket.cpp
Go to the documentation of this file.
1// ======================================================================
2// \title TcpClientSocket.cpp
3// \author mstarch
4// \brief cpp file for TcpClientSocket core implementation classes
5//
6// \copyright
7// Copyright 2009-2020, by the California Institute of Technology.
8// ALL RIGHTS RESERVED. United States Government Sponsorship
9// acknowledged.
10//
11// ======================================================================
12
14#include <Fw/Logger/Logger.hpp>
15#include <Fw/Types/Assert.hpp>
16#include <FpConfig.hpp>
17
18#ifdef TGT_OS_TYPE_VXWORKS
19 #include <socket.h>
20 #include <inetLib.h>
21 #include <fioLib.h>
22 #include <hostLib.h>
23 #include <ioLib.h>
24 #include <vxWorks.h>
25 #include <sockLib.h>
26 #include <taskLib.h>
27 #include <sysLib.h>
28 #include <errnoLib.h>
29 #include <cstring>
30#elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
31 #include <sys/socket.h>
32 #include <unistd.h>
33 #include <arpa/inet.h>
34#else
35 #error OS not supported for IP Socket Communications
36#endif
37
38#include <cstdio>
39#include <cstring>
40
41namespace Drv {
42
44
46 return port != 0;
47}
48
49
51 NATIVE_INT_TYPE socketFd = -1;
52 struct sockaddr_in address;
53
54 // Acquire a socket, or return error
55 if ((socketFd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
57 }
58 // Set up the address port and name
59 address.sin_family = AF_INET;
60 address.sin_port = htons(this->m_port);
61
62 // OS specific settings
63#if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
64 address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
65#endif
66
67 // First IP address to socket sin_addr
68 if (IpSocket::addressToIp4(m_hostname, &(address.sin_addr)) != SOCK_SUCCESS) {
69 ::close(socketFd);
71 };
72
73 // Now apply timeouts
74 if (IpSocket::setupTimeouts(socketFd) != SOCK_SUCCESS) {
75 ::close(socketFd);
77 }
78
79 // TCP requires connect to the socket to allow for communication
80 if (::connect(socketFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
81 ::close(socketFd);
83 }
84 socketDescriptor.fd = socketFd;
85 Fw::Logger::log("Connected to %s:%hu as a tcp client\n", m_hostname, m_port);
86 return SOCK_SUCCESS;
87}
88
89I32 TcpClientSocket::sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) {
90 return static_cast<I32>(::send(socketDescriptor.fd, data, size, SOCKET_IP_SEND_FLAGS));
91}
92
93I32 TcpClientSocket::recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) {
94 return static_cast<I32>(::recv(socketDescriptor.fd, data, size, SOCKET_IP_RECV_FLAGS));
95}
96
97} // namespace Drv
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:55
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:30
C++-compatible configuration header for fprime configuration.
@ SOCKET_IP_RECV_FLAGS
Definition IpCfg.hpp:20
@ SOCKET_IP_SEND_FLAGS
Definition IpCfg.hpp:19
Helper base-class for setting up Berkeley sockets.
Definition IpSocket.hpp:55
U16 m_port
IP address port used.
Definition IpSocket.hpp:210
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition IpSocket.hpp:211
SocketIpStatus setupTimeouts(PlatformIntType socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition IpSocket.cpp:68
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.
Definition IpSocket.cpp:85
SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size)
send data out the IP socket from the given buffer
Definition IpSocket.cpp:131
SocketIpStatus recv(const SocketDescriptor &fd, U8 *const data, U32 &size)
receive data from the IP socket from the given buffer
Definition IpSocket.cpp:163
void close(const SocketDescriptor &socketDescriptor)
closes the socket
Definition IpSocket.cpp:106
TcpClientSocket()
Constructor for client socket tcp implementation.
I32 recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv.
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
Tcp specific implementation for opening a client socket.
I32 sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
bool isValidPort(U16 port) override
Check if the given port is valid for the socket.
static void log(const char *format,...)
log a formated string with supplied arguments
Definition Logger.cpp:21
SocketIpStatus
Status enumeration for socket return values.
Definition IpSocket.hpp:29
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
Definition IpSocket.hpp:33
@ SOCK_SUCCESS
Socket operation successful.
Definition IpSocket.hpp:30
@ SOCK_FAILED_TO_SET_SOCKET_OPTIONS
Failed to configure socket.
Definition IpSocket.hpp:35
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
Definition IpSocket.hpp:31
@ SOCK_FAILED_TO_CONNECT
Failed to connect socket.
Definition IpSocket.hpp:34
PlatformIntType fd
Used for all sockets to track the communication file descriptor.
Definition IpSocket.hpp:22