| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 13 | #include <Drv/Ip/TcpClientSocket.hpp> | ||
| 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 | |||
| 43 | 101 | TcpClientSocket::TcpClientSocket() : IpSocket() {} | |
| 44 | |||
| 45 | 101 | bool TcpClientSocket::isValidPort(U16 port) const { | |
| 46 | 101 | return port != 0; | |
| 47 | } | ||
| 48 | |||
| 49 | 101 | SocketIpStatus TcpClientSocket::openProtocol(SocketDescriptor& socketDescriptor) { | |
| 50 | 101 | struct sockaddr_in address; | |
| 51 | |||
| 52 | // Acquire a socket, or return error | ||
| 53 | 101 | int socketFd = ::socket(AF_INET, SOCK_STREAM, 0); | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
|
101 | if (socketFd == -1) { |
| 55 | ✗ | return SOCK_FAILED_TO_GET_SOCKET; | |
| 56 | } | ||
| 57 | // Set up the address port and name | ||
| 58 | 101 | address.sin_family = AF_INET; | |
| 59 | 101 | 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 | // Convert the configured IPv4 address (dotted-quad) to a network-order in_addr. | ||
| 67 | |||
| 68 |
1/1✓ Branch 3 taken 101 times.
|
101 | const SocketIpStatus addressStatus = IpSocket::addressToIp4(this->m_ipv4_address, &(address.sin_addr)); |
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
|
101 | if (addressStatus != SOCK_SUCCESS) { |
| 70 | ✗ | (void)::close(socketFd); | |
| 71 | ✗ | return SOCK_INVALID_IP_ADDRESS; | |
| 72 | }; | ||
| 73 | |||
| 74 |
2/3✓ Branch 5 taken 101 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 101 times.
|
101 | if (IpSocket::setupSocketOptions(socketFd) != SOCK_SUCCESS) { |
| 75 | ✗ | (void)::close(socketFd); | |
| 76 | ✗ | return SOCK_FAILED_TO_SET_SOCKET_OPTIONS; | |
| 77 | } | ||
| 78 | |||
| 79 | // Now apply timeouts | ||
| 80 |
2/3✓ Branch 5 taken 101 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 101 times.
|
101 | if (IpSocket::setupTimeouts(socketFd) != SOCK_SUCCESS) { |
| 81 | ✗ | (void)::close(socketFd); | |
| 82 | ✗ | return SOCK_FAILED_TO_SET_SOCKET_OPTIONS; | |
| 83 | } | ||
| 84 | |||
| 85 | // TCP requires connect to the socket to allow for communication | ||
| 86 |
2/3✓ Branch 1 taken 101 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
|
101 | if (::connect(socketFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) { |
| 87 | ✗ | (void)::close(socketFd); | |
| 88 | ✗ | return SOCK_FAILED_TO_CONNECT; | |
| 89 | } | ||
| 90 | 101 | socketDescriptor.fd = socketFd; | |
| 91 |
1/1✓ Branch 8 taken 101 times.
|
101 | Fw::Logger::log("Connected to %s:%hu as a tcp client\n", this->m_ipv4_address, this->m_port); |
| 92 | 101 | return SOCK_SUCCESS; | |
| 93 | } | ||
| 94 | |||
| 95 | 101 | FwSignedSizeType TcpClientSocket::sendProtocol(const SocketDescriptor& socketDescriptor, | |
| 96 | const U8* const data, | ||
| 97 | const FwSizeType size) { | ||
| 98 | return static_cast<FwSignedSizeType>( | ||
| 99 | 101 | ::send(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_SEND_FLAGS)); | |
| 100 | } | ||
| 101 | |||
| 102 | 101 | FwSignedSizeType TcpClientSocket::recvProtocol(const SocketDescriptor& socketDescriptor, | |
| 103 | U8* const data, | ||
| 104 | const FwSizeType size) { | ||
| 105 | return static_cast<FwSignedSizeType>( | ||
| 106 | 101 | ::recv(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_RECV_FLAGS)); | |
| 107 | } | ||
| 108 | |||
| 109 | } // namespace Drv | ||
| 110 |