F´ Flight Software - C/C++ Documentation NASA-v1.6.0
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 <FpConfig.hpp>
16
17#ifdef TGT_OS_TYPE_VXWORKS
18 #include <socket.h>
19 #include <inetLib.h>
20 #include <fioLib.h>
21 #include <hostLib.h>
22 #include <ioLib.h>
23 #include <vxWorks.h>
24 #include <sockLib.h>
25 #include <taskLib.h>
26 #include <sysLib.h>
27 #include <errnoLib.h>
28 #include <cstring>
29#elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
30 #include <sys/socket.h>
31 #include <unistd.h>
32 #include <arpa/inet.h>
33#else
34 #error OS not supported for IP Socket Communications
35#endif
36
37#include <cstdio>
38#include <cstring>
39
40namespace Drv {
41
43
45 NATIVE_INT_TYPE socketFd = -1;
46 struct sockaddr_in address;
47
48 // Acquire a socket, or return error
49 if ((socketFd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
51 }
52 // Set up the address port and name
53 address.sin_family = AF_INET;
54 address.sin_port = htons(this->m_port);
55
56 // OS specific settings
57#if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
58 address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
59#endif
60
61 // First IP address to socket sin_addr
62 if (IpSocket::addressToIp4(m_hostname, &(address.sin_addr)) != SOCK_SUCCESS) {
63 ::close(socketFd);
65 };
66
67 // Now apply timeouts
68 if (IpSocket::setupTimeouts(socketFd) != SOCK_SUCCESS) {
69 ::close(socketFd);
71 }
72
73 // TCP requires connect to the socket to allow for communication
74 if (::connect(socketFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
75 ::close(socketFd);
77 }
78
79 fd = socketFd;
80 Fw::Logger::logMsg("Connected to %s:%hu as a tcp client\n", reinterpret_cast<POINTER_CAST>(m_hostname), m_port);
81 return SOCK_SUCCESS;
82}
83
84I32 TcpClientSocket::sendProtocol(const U8* const data, const U32 size) {
85 return ::send(this->m_fd, data, size, SOCKET_IP_SEND_FLAGS);
86}
87
88I32 TcpClientSocket::recvProtocol(U8* const data, const U32 size) {
89 return ::recv(this->m_fd, data, size, SOCKET_IP_RECV_FLAGS);
90}
91
92} // namespace Drv
PlatformPointerCastType POINTER_CAST
Definition BasicTypes.h:53
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
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 Berkley sockets.
Definition IpSocket.hpp:45
void close()
closes the socket
Definition IpSocket.cpp:109
U16 m_port
IP address port used.
Definition IpSocket.hpp:180
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition IpSocket.hpp:182
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:80
NATIVE_INT_TYPE m_fd
Definition IpSocket.hpp:177
SocketIpStatus setupTimeouts(NATIVE_INT_TYPE socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition IpSocket.cpp:63
TcpClientSocket()
Constructor for client socket tcp implementation.
SocketIpStatus openProtocol(NATIVE_INT_TYPE &fd)
Tcp specific implementation for opening a client socket.
I32 recvProtocol(U8 *const data, const U32 size)
Protocol specific implementation of recv. Called directly with error handling from recv.
I32 sendProtocol(const U8 *const data, const U32 size)
Protocol specific implementation of send. Called directly with retry from send.
static void logMsg(const char *fmt, POINTER_CAST a0=0, POINTER_CAST a1=0, POINTER_CAST a2=0, POINTER_CAST a3=0, POINTER_CAST a4=0, POINTER_CAST a5=0, POINTER_CAST a6=0, POINTER_CAST a7=0, POINTER_CAST a8=0, POINTER_CAST a9=0)
Definition Logger.cpp:18
SocketIpStatus
Status enumeration for socket return values.
Definition IpSocket.hpp:23
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
Definition IpSocket.hpp:27
@ SOCK_SUCCESS
Socket operation successful.
Definition IpSocket.hpp:24
@ SOCK_FAILED_TO_SET_SOCKET_OPTIONS
Failed to configure socket.
Definition IpSocket.hpp:29
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
Definition IpSocket.hpp:25
@ SOCK_FAILED_TO_CONNECT
Failed to connect socket.
Definition IpSocket.hpp:28