F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
TcpClientComponentImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TcpClientComponentImpl.cpp
3 // \author mstarch
4 // \brief cpp file for TcpClientComponentImpl component implementation class
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 <limits>
15 #include <FpConfig.hpp>
16 #include "Fw/Types/Assert.hpp"
17 
18 
19 namespace Drv {
20 
21 // ----------------------------------------------------------------------
22 // Construction, initialization, and destruction
23 // ----------------------------------------------------------------------
24 
26  : TcpClientComponentBase(compName),
27  SocketReadTask() {}
28 
30  const U16 port,
31  const U32 send_timeout_seconds,
32  const U32 send_timeout_microseconds,
33  FwSizeType buffer_size) {
34 
35  // Check that ensures the configured buffer size fits within the limits fixed-width type, U32
36  FW_ASSERT(buffer_size <= std::numeric_limits<U32>::max(), static_cast<FwAssertArgType>(buffer_size));
37  m_allocation_size = buffer_size; // Store the buffer size
38  return m_socket.configure(hostname, port, send_timeout_seconds, send_timeout_microseconds);
39 }
40 
42 
43 // ----------------------------------------------------------------------
44 // Implementations for socket read task virtual methods
45 // ----------------------------------------------------------------------
46 
48  return m_socket;
49 }
50 
52  return allocate_out(0, static_cast<U32>(m_allocation_size));
53 }
54 
57  this->recv_out(0, buffer, recvStatus);
58 }
59 
62  this->ready_out(0);
63  }
64 
65 }
66 
67 // ----------------------------------------------------------------------
68 // Handler implementations for user-defined typed input ports
69 // ----------------------------------------------------------------------
70 
71 Drv::SendStatus TcpClientComponentImpl::send_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
72  Drv::SocketIpStatus status = m_socket.send(fwBuffer.getData(), fwBuffer.getSize());
73  // Only deallocate buffer when the caller is not asked to retry
74  if (status == SOCK_INTERRUPTED_TRY_AGAIN) {
76  } else if (status != SOCK_SUCCESS) {
77  deallocate_out(0, fwBuffer);
79  }
80  deallocate_out(0, fwBuffer);
81  return SendStatus::SEND_OK;
82 }
83 
84 } // end namespace Drv
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
PlatformSizeType FwSizeType
Definition: FpConfig.h:30
C++-compatible configuration header for fprime configuration.
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:47
SocketIpStatus send(const U8 *const data, const U32 size)
send data out the IP socket from the given buffer
Definition: IpSocket.cpp:168
SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the ip socket with host and transmission timeouts
Definition: IpSocket.cpp:53
Status associated with the received data.
@ RECV_OK
Receive worked as expected.
@ RECV_ERROR
Receive error occurred retrying may succeed.
Status returned by the send call.
@ SEND_ERROR
Send error occurred retrying may succeed.
@ SEND_RETRY
Data send should be retried.
@ SEND_OK
Send worked as expected.
supports a task to read a given socket adaptation
Auto-generated base for TcpClient component.
bool isConnected_ready_OutputPort(FwIndexType portNum)
void ready_out(FwIndexType portNum)
Invoke output port ready.
void deallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port deallocate.
void recv_out(FwIndexType portNum, Fw::Buffer &recvBuffer, const Drv::RecvStatus &recvStatus)
Invoke output port recv.
Fw::Buffer allocate_out(FwIndexType portNum, U32 size)
Invoke output port allocate.
SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds=SOCKET_SEND_TIMEOUT_SECONDS, const U32 send_timeout_microseconds=SOCKET_SEND_TIMEOUT_MICROSECONDS, FwSizeType buffer_size=1024)
Configures the TcpClient settings but does not open the connection.
TcpClientComponentImpl(const char *const compName)
construct the TcpClient component.
void connected()
called when the IPv4 system has been connected
void sendBuffer(Fw::Buffer buffer, SocketIpStatus status)
sends a buffer to be filled with data
IpSocket & getSocketHandler()
returns a reference to the socket handler
Fw::Buffer getBuffer()
returns a buffer to fill with data
~TcpClientComponentImpl()
Destroy the component.
U8 * getData() const
Definition: Buffer.cpp:68
U32 getSize() const
Definition: Buffer.cpp:72
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:23
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:24
@ SOCK_INTERRUPTED_TRY_AGAIN
Interrupted status for retries.
Definition: IpSocket.hpp:30