chrono::utils::ChSocketTCP Class Reference

Description

ChSocket for TCP communications.

Three modes for sending|receiving are available:

#include <ChSocket.h>

Inheritance diagram for chrono::utils::ChSocketTCP:
Collaboration diagram for chrono::utils::ChSocketTCP:

Public Member Functions

 ChSocketTCP ()
 Constructor used for creating instances dedicated to client communication. More...
 
 ChSocketTCP (int port_number)
 Create a TCP socket and assign it a unique ID. More...
 
 ~ChSocketTCP ()
 Destroy TCP socket.
 
bool BindSocket ()
 Server-side functionalities -----------------------------------------—. More...
 
bool ListenToClient (int max_pending_connections=5)
 Configure the socket to listen for incoming client connections [SERVER SIDE]. More...
 
ChSocketTCP * AcceptClient (std::string &client_host_name)
 Accept an incoming client connection [SERVER SIDE]. More...
 
bool ConnectToServer (std::string &server_name_or_addr, HostType host_type)
 Client-side functionalities -----------------------------------------—. More...
 
int SendString (const std::string &message)
 Manual communication functionalities --------------------------------—. More...
 
int ReceiveString (std::string &message)
 Receive a string message from the connected host into the given argument. More...
 
int SendBuffer (const std::vector< char > &source_buffer)
 Send a buffer of raw bytes to the connected host. More...
 
int ReceiveBuffer (std::vector< char > &destination_buffer, int byte_size)
 Receive a buffer of raw bytes from the connected host into the given argument. More...
 
template<typename T >
void EnqueueData (const T &data_to_send)
 Buffered communication functionalities ------------------------------—. More...
 
void ClearInternalSendDataBuffer ()
 Clear the internal sending buffer populated by EnqueueData().
 
int SendDataBuffer ()
 Send the data collected into the internal sending buffer and clear it. More...
 
int ReceiveDataBuffer (int data_in_size)
 Receive a given amount of bytes and store them into the internal receiving buffer. More...
 
template<typename T >
void ParseReceivedData (T &data_read, int byte_offset=0)
 Copy data from the internal receiving buffer, at the specified byte offset, into the given variable. More...
 
template<typename DataInType , typename... DataInTypes>
int SendData (DataInType data_in_1, DataInTypes... data_ins)
 Send the given variables. More...
 
template<typename DataInType , typename... DataInTypes>
int ReceiveData (DataInType &data_in_1, DataInTypes &... data_ins)
 Receive the given variables. More...
 
- Public Member Functions inherited from chrono::utils::ChSocket
 ChSocket (int port_number)
 Create a socket and assign it a unique ID (aka. More...
 
virtual ~ChSocket ()
 Close and destroy socket.
 
int GetSocketId ()
 Get socket ID (aka. file descriptor).
 
int GetPortNumber ()
 Get socket port number.
 
void SetDebug (int debug_toggle)
 Toggle socket options (either ON or OFF).
 
void SetReuseAddr (int reuse_toggle)
 
void SetKeepAlive (int alive_toggle)
 
void SetLingerOnOff (bool linger_on)
 
void SetSocketBlocking (int blocking_toggle)
 
void SetLingerSeconds (int seconds)
 Set linger timeout on socket close (seconds).
 
int GetDebug ()
 Retrieve socket options.
 
int GetReuseAddr ()
 
int GetKeepAlive ()
 
bool GetLingerOnOff ()
 
int GetLingerSeconds ()
 
int GetSocketBlocking ()
 
void SetSendBufSize (int sendbuff_size)
 Set the size of the socket send and receive buffer (bytes).
 
void SetReceiveBufSize (int recvbuff_size)
 
int GetSendBufSize ()
 Get the size of the socket send and receive buffer (bytes).
 
int GetReceiveBufSize ()
 

Additional Inherited Members

- Protected Member Functions inherited from chrono::utils::ChSocket
 ChSocket ()
 Only used when the socket is used for client communication. More...
 
void SetSocketId (int socketFd)
 
- Protected Attributes inherited from chrono::utils::ChSocket
int socketId
 socket file descriptor
 
int portNumber
 socket port number
 
int blocking
 blocking flag
 
int bindFlag
 binding flag
 
sockaddr_in clientAddr
 address of the client that sent data
 

Constructor & Destructor Documentation

◆ ChSocketTCP() [1/2]

chrono::utils::ChSocketTCP::ChSocketTCP ( )
inline

Constructor used for creating instances dedicated to client communication.

When accept() is successful, a socketId is generated and returned. This socket id is then used to build a new socket using the following constructor. Therefore, the next necessary call should be SetSocketId() using this newly generated socket fd.

◆ ChSocketTCP() [2/2]

chrono::utils::ChSocketTCP::ChSocketTCP ( int  port_number)

Create a TCP socket and assign it a unique ID.

Store port number for later use.

Member Function Documentation

◆ AcceptClient()

ChSocketTCP * chrono::utils::ChSocketTCP::AcceptClient ( std::string &  client_host_name)

Accept an incoming client connection [SERVER SIDE].

If successful, return a new heap-allocated socket with the ID of the accepted connection (which must be managed by the user). If unsuccessful, return a null pointer. The resolved host name of the connected client is returned as function argument.

◆ BindSocket()

bool chrono::utils::ChSocketTCP::BindSocket ( )

Server-side functionalities -----------------------------------------—.

Bind the socket to an address and a port number [SERVER SIDE]. Return true on success.

◆ ConnectToServer()

bool chrono::utils::ChSocketTCP::ConnectToServer ( std::string &  server_name_or_addr,
HostType  host_type 
)

Client-side functionalities -----------------------------------------—.

Connect to a server using the port number configured for this socket [CLIENT SIDE]. Target server can be specified either by host name or IP address. Return true on success.

◆ EnqueueData()

template<typename T >
void chrono::utils::ChSocketTCP::EnqueueData ( const T &  data_to_send)
inline

Buffered communication functionalities ------------------------------—.

Add data to the internal sending buffer. When operation is completed, collected data can be sent at once using SendDataBuffer().

◆ ListenToClient()

bool chrono::utils::ChSocketTCP::ListenToClient ( int  max_pending_connections = 5)

Configure the socket to listen for incoming client connections [SERVER SIDE].

Optionally specify the maximum length of the pending connections queue (backlog). Subsequent calls to AcceptClient() will actually perform the connection. Return true on success.

◆ ParseReceivedData()

template<typename T >
void chrono::utils::ChSocketTCP::ParseReceivedData ( T &  data_read,
int  byte_offset = 0 
)
inline

Copy data from the internal receiving buffer, at the specified byte offset, into the given variable.

This method should be called (potentially multiple times) to access data received by ReceiveBuffer().

◆ ReceiveBuffer()

int chrono::utils::ChSocketTCP::ReceiveBuffer ( std::vector< char > &  destination_buffer,
int  byte_size 
)

Receive a buffer of raw bytes from the connected host into the given argument.

This method does not receive any header, so the caller must know in advance the length of the buffer. The destination buffer is resized to the specified size in bytes before receiving. Return the number of bytes received.

◆ ReceiveData()

template<typename DataInType , typename... DataInTypes>
int chrono::utils::ChSocketTCP::ReceiveData ( DataInType &  data_in_1,
DataInTypes &...  data_ins 
)
inline

Receive the given variables.

The receiving order follows the order of the arguments. It is not possible to receive only part of the streamed content. To this purpose use ReceiveBuffer(int) together with ParseReceivedData(). Return the number of bytes received.

◆ ReceiveDataBuffer()

int chrono::utils::ChSocketTCP::ReceiveDataBuffer ( int  data_in_size)

Receive a given amount of bytes and store them into the internal receiving buffer.

Received data can be subsequently read using ParseReceivedData(). This method clears the internal reading buffer before operating. Return the number of bytes received.

◆ ReceiveString()

int chrono::utils::ChSocketTCP::ReceiveString ( std::string &  message)

Receive a string message from the connected host into the given argument.

This method automatically determines the message size from a fixed-length header, which is not exposed to the user. Return the number of bytes received.

◆ SendBuffer()

int chrono::utils::ChSocketTCP::SendBuffer ( const std::vector< char > &  source_buffer)

Send a buffer of raw bytes to the connected host.

This method does not send any header, so the receiver must know in advance the length of the buffer. Return the number of bytes sent.

◆ SendData()

template<typename DataInType , typename... DataInTypes>
int chrono::utils::ChSocketTCP::SendData ( DataInType  data_in_1,
DataInTypes...  data_ins 
)
inline

Send the given variables.

The sending order respects the order of the arguments. Return the number of bytes sent.

◆ SendDataBuffer()

int chrono::utils::ChSocketTCP::SendDataBuffer ( )

Send the data collected into the internal sending buffer and clear it.

Return the number of bytes sent.

◆ SendString()

int chrono::utils::ChSocketTCP::SendString ( const std::string &  message)

Manual communication functionalities --------------------------------—.

Send a string message to the connected host. This method automatically prepends a fixed-length header, specifying the message size in bytes. Return the number of bytes sent.


The documentation for this class was generated from the following files:
  • /builds/uwsbel/chrono/src/chrono/utils/ChSocket.h
  • /builds/uwsbel/chrono/src/chrono/utils/ChSocket.cpp