F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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/FPrimeBasicTypes.hpp>
15 #include <Fw/Logger/Logger.hpp>
16 #include <Fw/Types/Assert.hpp>
17 
18 #ifdef TGT_OS_TYPE_VXWORKS
19 #include <errnoLib.h>
20 #include <fioLib.h>
21 #include <hostLib.h>
22 #include <inetLib.h>
23 #include <ioLib.h>
24 #include <sockLib.h>
25 #include <socket.h>
26 #include <sysLib.h>
27 #include <taskLib.h>
28 #include <vxWorks.h>
29 #include <cstring>
30 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
31 #include <arpa/inet.h>
32 #include <sys/socket.h>
33 #include <unistd.h>
34 #else
35 #error OS not supported for IP Socket Communications
36 #endif
37 
38 #include <cstdio>
39 #include <cstring>
40 
41 namespace Drv {
42 
44 
46  return port != 0;
47 }
48 
50  int socketFd = -1;
51  struct sockaddr_in address;
52 
53  // Acquire a socket, or return error
54  if ((socketFd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
56  }
57  // Set up the address port and name
58  address.sin_family = AF_INET;
59  address.sin_port = htons(this->m_port);
60 
61  // OS specific settings
62 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
63  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
64 #endif
65 
66  // First IP address to socket sin_addr
67  if (IpSocket::addressToIp4(m_hostname, &(address.sin_addr)) != SOCK_SUCCESS) {
68  ::close(socketFd);
70  };
71 
72  // Now apply timeouts
73  if (IpSocket::setupTimeouts(socketFd) != SOCK_SUCCESS) {
74  ::close(socketFd);
76  }
77 
78  // TCP requires connect to the socket to allow for communication
79  if (::connect(socketFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
80  ::close(socketFd);
82  }
83  socketDescriptor.fd = socketFd;
84  Fw::Logger::log("Connected to %s:%hu as a tcp client\n", m_hostname, m_port);
85  return SOCK_SUCCESS;
86 }
87 
89  const U8* const data,
90  const FwSizeType size) {
91  return static_cast<FwSignedSizeType>(
92  ::send(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_SEND_FLAGS));
93 }
94 
96  U8* const data,
97  const FwSizeType size) {
98  return static_cast<FwSignedSizeType>(
99  ::recv(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_RECV_FLAGS));
100 }
101 
102 } // namespace Drv
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:86
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
Tcp specific implementation for opening a client socket.
Socket open failed.
Definition: IpSocket.hpp:31
bool isValidPort(U16 port) override
Check if the given port is valid for the socket.
Failed to connect socket.
Definition: IpSocket.hpp:34
Failed to configure socket.
Definition: IpSocket.hpp:35
PlatformSizeType FwSizeType
SocketIpStatus recv(const SocketDescriptor &fd, U8 *const data, FwSizeType &size)
receive data from the IP socket from the given buffer
Definition: IpSocket.cpp:167
SocketIpStatus setupTimeouts(int socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:69
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
int fd
Used for all sockets to track the communication file descriptor.
Definition: IpSocket.hpp:22
PlatformSignedSizeType FwSignedSizeType
virtual SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size)
send data out the IP socket from the given buffer
Definition: IpSocket.cpp:132
Bad IP address supplied.
Definition: IpSocket.hpp:33
U16 m_port
IP address port used.
Definition: IpSocket.hpp:229
TcpClientSocket()
Constructor for client socket tcp implementation.
Socket operation successful.
Definition: IpSocket.hpp:30
FwSignedSizeType sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size) override
Protocol specific implementation of send. Called directly with retry from send.
FwSignedSizeType recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const FwSizeType size) override
Protocol specific implementation of recv. Called directly with error handling from recv...
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
void close(const SocketDescriptor &socketDescriptor)
closes the socket
Definition: IpSocket.cpp:107
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition: IpSocket.hpp:230
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:57