Description
ChSocket for TCP (i.e.
stream) communications. Three modes for sending|receiving are available:
- full-manual mode:
- user owns a buffer of type std::vector<char>
- user owns a message of type std::string
- single call to SendString(const std::string&) | ReceiveString(std::string&)
- templated mode:
- for sending:
- multiple calls to EnqueueData()
- single call to SendDataBuffer()
- for receiving:
- single call to ReceiveDataBuffer(int)
- multiple calls to ParseReceivedData()
- for sending:
- full-auto mode
- single call to SendData() | ReceiveData(), with all the variables involved
#include <ChSocket.h>


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 () |
| Bind the socket to an address and a port number [SERVER SIDE]. 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 (const std::string &server_name_or_addr, HostType host_type) |
| Connect to a server using the port number configured for this socket [CLIENT SIDE]. More... | |
| int | SendString (const std::string &message_out) |
| Send a string message to the connected host. More... | |
| int | ReceiveString (std::string &message_in) |
| 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) |
| Add data to the internal sending buffer. More... | |
| void | ClearInternalSendDataBuffer () |
| Clear the internal sending buffer populated by EnqueueData(). | |
| int | SendDataBuffer () |
| Send the data collected into the internal sending buffer, then 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 |
| Get the system errors. More... | |
| 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]
|
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 id.
◆ 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 | ( | ) |
Bind the socket to an address and a port number [SERVER SIDE].
Return true on success.
◆ ConnectToServer()
| bool chrono::utils::ChSocketTCP::ConnectToServer | ( | const std::string & | server_name_or_addr, |
| HostType | host_type | ||
| ) |
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()
|
inline |
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()
|
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 ReceiveDataBuffer().
◆ 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()
|
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_in | ) |
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()
|
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, then clear it.
Return the number of bytes sent.
◆ SendString()
| int chrono::utils::ChSocketTCP::SendString | ( | const std::string & | message_out | ) |
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
Public Member Functions inherited from