mirror of
https://github.com/nasa/fprime.git
synced 2025-12-10 00:44:37 -06:00
108 lines
3.3 KiB
C++
108 lines
3.3 KiB
C++
// ======================================================================
|
|
// \title TcpClientSocket.cpp
|
|
// \author mstarch
|
|
// \brief cpp file for TcpClientSocket core implementation classes
|
|
//
|
|
// \copyright
|
|
// Copyright 2009-2020, by the California Institute of Technology.
|
|
// ALL RIGHTS RESERVED. United States Government Sponsorship
|
|
// acknowledged.
|
|
//
|
|
// ======================================================================
|
|
|
|
#include <Drv/Ip/TcpClientSocket.hpp>
|
|
#include <Fw/FPrimeBasicTypes.hpp>
|
|
#include <Fw/Logger/Logger.hpp>
|
|
#include <Fw/Types/Assert.hpp>
|
|
|
|
#ifdef TGT_OS_TYPE_VXWORKS
|
|
#include <errnoLib.h>
|
|
#include <fioLib.h>
|
|
#include <hostLib.h>
|
|
#include <inetLib.h>
|
|
#include <ioLib.h>
|
|
#include <sockLib.h>
|
|
#include <socket.h>
|
|
#include <sysLib.h>
|
|
#include <taskLib.h>
|
|
#include <vxWorks.h>
|
|
#include <cstring>
|
|
#elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
|
|
#include <arpa/inet.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#else
|
|
#error OS not supported for IP Socket Communications
|
|
#endif
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
namespace Drv {
|
|
|
|
TcpClientSocket::TcpClientSocket() : IpSocket() {}
|
|
|
|
bool TcpClientSocket::isValidPort(U16 port) {
|
|
return port != 0;
|
|
}
|
|
|
|
SocketIpStatus TcpClientSocket::openProtocol(SocketDescriptor& socketDescriptor) {
|
|
int socketFd = -1;
|
|
struct sockaddr_in address;
|
|
|
|
// Acquire a socket, or return error
|
|
if ((socketFd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
|
return SOCK_FAILED_TO_GET_SOCKET;
|
|
}
|
|
// Set up the address port and name
|
|
address.sin_family = AF_INET;
|
|
address.sin_port = htons(this->m_port);
|
|
|
|
// OS specific settings
|
|
#if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
|
|
address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
|
|
#endif
|
|
|
|
// First IP address to socket sin_addr
|
|
if (IpSocket::addressToIp4(m_hostname, &(address.sin_addr)) != SOCK_SUCCESS) {
|
|
::close(socketFd);
|
|
return SOCK_INVALID_IP_ADDRESS;
|
|
};
|
|
|
|
if (IpSocket::setupSocketOptions(socketFd) != SOCK_SUCCESS) {
|
|
::close(socketFd);
|
|
return SOCK_FAILED_TO_SET_SOCKET_OPTIONS;
|
|
}
|
|
|
|
// Now apply timeouts
|
|
if (IpSocket::setupTimeouts(socketFd) != SOCK_SUCCESS) {
|
|
::close(socketFd);
|
|
return SOCK_FAILED_TO_SET_SOCKET_OPTIONS;
|
|
}
|
|
|
|
// TCP requires connect to the socket to allow for communication
|
|
if (::connect(socketFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
|
|
::close(socketFd);
|
|
return SOCK_FAILED_TO_CONNECT;
|
|
}
|
|
socketDescriptor.fd = socketFd;
|
|
Fw::Logger::log("Connected to %s:%hu as a tcp client\n", m_hostname, m_port);
|
|
return SOCK_SUCCESS;
|
|
}
|
|
|
|
FwSignedSizeType TcpClientSocket::sendProtocol(const SocketDescriptor& socketDescriptor,
|
|
const U8* const data,
|
|
const FwSizeType size) {
|
|
return static_cast<FwSignedSizeType>(
|
|
::send(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_SEND_FLAGS));
|
|
}
|
|
|
|
FwSignedSizeType TcpClientSocket::recvProtocol(const SocketDescriptor& socketDescriptor,
|
|
U8* const data,
|
|
const FwSizeType size) {
|
|
return static_cast<FwSignedSizeType>(
|
|
::recv(socketDescriptor.fd, data, static_cast<size_t>(size), SOCKET_IP_RECV_FLAGS));
|
|
}
|
|
|
|
} // namespace Drv
|