Project Chrono: chrono::utils::ChSocketTCP Class Reference

Public Member Functions | List of all members
chrono::utils::ChSocketTCP Class Reference

## Description

ChSocket for TCP communications.

Three modes for sending|receiving are available:

- full-manual mode:
  - the user owns its own buffer of type std::vector<char>
  - calls SendBuffer(std::vector<char>&) | ReceiveBuffer(std::vector<char>&, int) once
- templated mode (sending):
  - multiple calls to Enqueue()
  - call SendBuffer() once
- templated mode (receiving):
  - call ReceiveBuffer(int)
  - multiple calls to ParseReceivedData()
- full-auto mode
  - call SendData() | ReceiveData() with all the variables to be sent

`#include <ChSocket.h>`

Inheritance diagram for chrono::utils::ChSocketTCP:
[legend]
Collaboration diagram for chrono::utils::ChSocketTCP:
[legend]

## Public Member Functions
| ChSocketTCP ()
| Constructor used for creating instances dedicated to client communication.
| ChSocketTCP (int portId)
| Constructor. Used to create a new TCP socket given a port.
int | sendMessage (std::string &)
| Send a std::string to the connected host.
int | receiveMessage (std::string &)
| Receive a std::string from the connected host.
int | SendBuffer (std::vector< char > &source_buf)
| Send a std::vector<char> (a buffer of bytes) to the connected host, without the header as in SendMessage (so the receiver must know in advance the length of the buffer).
int | ReceiveBuffer (std::vector< char > &dest_buf, int bsize)
| Receive a std::vector<char> (a buffer of bytes) from the connected host, without the header as in SendMessage (so one must know in advance the length of the buffer).
template<typename T>
void | Enqueue (const T &data_to_send)
| Add data to an internal sending buffer.
void | ClearInternalSendBuffer ()
| Clear the internal send buffer.
int | SendBuffer ()
| Send data collected in internal buffer.
int | ReceiveBuffer (int data_in_size)
| Receive a given amount of bytes.
template<typename T>
void | ParseReceivedData (T &data_read, int offset=0)
| Read the received buffer, at the specified offset, into the given variable.
template<typename DataInType, typename... DataInTypes>
int | SendData (DataInType data_in_1, DataInTypes... data_ins)
| Send the given variables.
template<typename DataInType, typename... DataInTypes>
int | ReceiveData (DataInType &data_in_1, DataInTypes &... data_ins)
| Receive the given variables.
void | bindSocket ()
| Binds the socket to an address and port number (a server call).
ChSocketTCP * | acceptClient (std::string &)
| Accepts a connecting client.
void | listenToClient (int numPorts=5)
| Listens to connecting clients, (a server call).
virtual void | connectToServer (std::string &, hostType)
| Connects to the server, a client call.
Public Member Functions inherited from chrono::utils::ChSocket
| ChSocket (int)
void | setDebug (int)
void | setReuseAddr (int)
void | setKeepAlive (int)
void | setLingerOnOff (bool)
void | setLingerSeconds (int)
void | setSocketBlocking (int)
void | setSendBufSize (int)
void | setReceiveBufSize (int)
int | getDebug ()
int | getReuseAddr ()
int | getKeepAlive ()
int | getSendBufSize ()
int | getReceiveBufSize ()
int | getSocketBlocking ()
int | getLingerSeconds ()
bool | getLingerOnOff ()
int | getSocketId ()
int | getPortNumber ()

## Additional Inherited Members
Protected Member Functions inherited from chrono::utils::ChSocket
void | setSocketId (int socketFd)
Protected Attributes inherited from chrono::utils::ChSocket
int | portNumber
int | socketId
int | blocking
int | bindFlag
struct sockaddr_in | clientAddr

## Constructor & Destructor Documentation

## ◆ ChSocketTCP()

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.

## Member Function Documentation

## ◆ acceptClient()

ChSocketTCP * chrono::utils::ChSocketTCP::acceptClient | ( | std::string & | clientHost | ) |

Accepts a connecting client.

The address of the connected client is stored in the parameter (a server call)

## ◆ Enqueue()

template<typename T>

void chrono::utils::ChSocketTCP::Enqueue | ( | const T & | data_to_send | ) |
| inline

Add data to an internal sending buffer.

When operation is completed, send all data at once with SendBuffer().

## ◆ ReceiveBuffer()

int chrono::utils::ChSocketTCP::ReceiveBuffer | ( | std::vector< char > & | dest_buf,
|  | int | bsize )

Receive a std::vector<char> (a buffer of bytes) from the connected host, without the header as in SendMessage (so one must know in advance the length of the buffer).

If the receiving buffer size is not =bsize, it will be resized before receiving.

Parameters

  dest_buf | destination buffer - will be resized
  bsize | size in bytes of expected received buffer.

## ◆ 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()

## ◆ receiveMessage()

int chrono::utils::ChSocketTCP::receiveMessage | ( | std::string & | message | ) |

Receive a std::string from the connected host.

Note that the string size is handled automatically because there is a header that tells the length of the string in bytes.

## ◆ SendBuffer()

int chrono::utils::ChSocketTCP::SendBuffer | ( | std::vector< char > & | source_buf | ) |

Send a std::vector<char> (a buffer of bytes) to the connected host, without the header as in SendMessage (so the receiver must know in advance the length of the buffer).

Parameters

  source_buf | source buffer

## ◆ 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.

## ◆ sendMessage()

int chrono::utils::ChSocketTCP::sendMessage | ( | std::string & | message | ) |

Send a std::string to the connected host.

Note that the string size is handled automatically because there is a header that tells the length of the string in bytes.

The documentation for this class was generated from the following files:

- D:/W/B/src/chrono-9.0.1/src/chrono/utils/ChSocket.h
- D:/W/B/src/chrono-9.0.1/src/chrono/utils/ChSocket.cpp