| 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 | #include <cstdio> | ||
| 19 | #include <cstring> | ||
| 20 | |||
| 21 | namespace Drv { | ||
| 22 | |||
| 23 | 103 | TcpClientSocket::TcpClientSocket() : IpSocket() {} | |
| 24 | |||
| 25 | 103 | bool TcpClientSocket::isValidPort(U16 port) const { | |
| 26 | 103 | return port != 0; | |
| 27 | } | ||
| 28 | |||
| 29 | 103 | SocketIpStatus TcpClientSocket::openProtocol(SocketDescriptor& socketDescriptor) { | |
| 30 | 103 | struct sockaddr_in address; | |
| 31 | |||
| 32 | // Acquire a socket, or return error | ||
| 33 | 103 | int socketFd = ::socket(AF_INET, SOCK_STREAM, 0); | |
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
|
103 | if (socketFd == -1) { |
| 35 | ✗ | return SOCK_FAILED_TO_GET_SOCKET; | |
| 36 | } | ||
| 37 | // Set up the address port and name | ||
| 38 | 103 | address.sin_family = AF_INET; | |
| 39 | 103 | address.sin_port = htons(this->m_port); | |
| 40 | |||
| 41 | // OS specific settings | ||
| 42 | #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN | ||
| 43 | address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in)); | ||
| 44 | #endif | ||
| 45 | |||
| 46 | // Convert the configured IPv4 address (dotted-quad) to a network-order in_addr. | ||
| 47 | |||
| 48 |
1/1✓ Branch 3 taken 103 times.
|
103 | const SocketIpStatus addressStatus = IpSocket::addressToIp4(this->m_ipv4_address, &(address.sin_addr)); |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
|
103 | if (addressStatus != SOCK_SUCCESS) { |
| 50 | ✗ | (void)::close(socketFd); | |
| 51 | ✗ | return SOCK_INVALID_IP_ADDRESS; | |
| 52 | }; | ||
| 53 | |||
| 54 |
2/3✓ Branch 5 taken 103 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 103 times.
|
103 | if (IpSocket::setupSocketOptions(socketFd) != SOCK_SUCCESS) { |
| 55 | ✗ | (void)::close(socketFd); | |
| 56 | ✗ | return SOCK_FAILED_TO_SET_SOCKET_OPTIONS; | |
| 57 | } | ||
| 58 | |||
| 59 | // Now apply timeouts | ||
| 60 |
2/3✓ Branch 5 taken 103 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 103 times.
|
103 | if (IpSocket::setupTimeouts(socketFd) != SOCK_SUCCESS) { |
| 61 | ✗ | (void)::close(socketFd); | |
| 62 | ✗ | return SOCK_FAILED_TO_SET_SOCKET_OPTIONS; | |
| 63 | } | ||
| 64 | |||
| 65 | // Sends to a server that has disconnected must not raise SIGPIPE | ||
| 66 |
2/3✓ Branch 5 taken 103 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 103 times.
|
103 | if (IpSocket::setupNoSigPipe(socketFd) != SOCK_SUCCESS) { |
| 67 | ✗ | (void)::close(socketFd); | |
| 68 | ✗ | return SOCK_FAILED_TO_SET_SOCKET_OPTIONS; | |
| 69 | } | ||
| 70 | |||
| 71 | // TCP requires connect to the socket to allow for communication | ||
| 72 |
2/3✓ Branch 1 taken 103 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 103 times.
|
103 | if (::connect(socketFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) { |
| 73 | ✗ | (void)::close(socketFd); | |
| 74 | ✗ | return SOCK_FAILED_TO_CONNECT; | |
| 75 | } | ||
| 76 | 103 | socketDescriptor.fd = socketFd; | |
| 77 |
1/1✓ Branch 8 taken 103 times.
|
103 | Fw::Logger::log("Connected to %s:%hu as a tcp client\n", this->m_ipv4_address, this->m_port); |
| 78 | 103 | return SOCK_SUCCESS; | |
| 79 | } | ||
| 80 | |||
| 81 | 103 | FwSignedSizeType TcpClientSocket::sendProtocol(const SocketDescriptor& socketDescriptor, | |
| 82 | const U8* const data, | ||
| 83 | const FwSizeType size) { | ||
| 84 | return static_cast<FwSignedSizeType>( | ||
| 85 | 103 | ::send(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_SEND_FLAGS | SEND_NO_SIGNAL_FLAGS)); | |
| 86 | } | ||
| 87 | |||
| 88 | 101 | FwSignedSizeType TcpClientSocket::recvProtocol(const SocketDescriptor& socketDescriptor, | |
| 89 | U8* const data, | ||
| 90 | const FwSizeType size) { | ||
| 91 | return static_cast<FwSignedSizeType>( | ||
| 92 | 101 | ::recv(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_RECV_FLAGS)); | |
| 93 | } | ||
| 94 | |||
| 95 | } // namespace Drv | ||
| 96 |