chrono::utils::ChSocketUDP Class Reference

Description

ChSocket for UDP (i.e.

datagram) communications. Three modes for sending|receiving are available:

#include <ChSocket.h>

Inheritance diagram for chrono::utils::ChSocketUDP:
Collaboration diagram for chrono::utils::ChSocketUDP:

Public Member Functions

 ChSocketUDP ()
 Default empty constructor.
 
 ChSocketUDP (int port_number)
 Create a UDP socket and assign it a unique ID. More...
 
 ~ChSocketUDP ()
 Destroy UDP socket.
 
bool BindSocket ()
 Bind the socket to a local address and port number [SERVER SIDE]. More...
 
bool SetDestinationHost (const std::string &server_name_or_addr, HostType host_type)
 Set destination host for subsequent communication [CLIENT SIDE]. More...
 
void SetDestinationPort (int port_number)
 Set destination port for subsequent UDP communication [CLIENT SIDE].
 
int SendString (const std::string &message_out)
 Send a string message to the configured destination. More...
 
int ReceiveString (std::string &message_in, std::string &sender_host_name, int &sender_port_number)
 Receive a string message from any sender. More...
 
int SendBuffer (const std::vector< char > &source_buffer)
 Send a buffer of raw bytes to the configured destination. More...
 
int ReceiveBuffer (std::vector< char > &destination_buffer, int byte_size, std::string &sender_host_name, int &sender_port_number)
 Receive a buffer of raw bytes from any sender. 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, std::string &sender_host_name, int &sender_port_number)
 Receive one UDP datagram and store it, up to data_in_size bytes, 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 (const DataInType &data_in_1, const DataInTypes &... data_ins)
 Send the given variables as a single UDP datagram. More...
 
template<typename DataInType , typename... DataInTypes>
int ReceiveData (DataInType &data_in_1, DataInTypes &... data_ins)
 Receive the given variables from one UDP datagram. 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

◆ ChSocketUDP()

chrono::utils::ChSocketUDP::ChSocketUDP ( int  port_number)

Create a UDP socket and assign it a unique ID.

Store the local port number for later use.

Member Function Documentation

◆ BindSocket()

bool chrono::utils::ChSocketUDP::BindSocket ( )

Bind the socket to a local address and port number [SERVER SIDE].

Return true on success.

◆ EnqueueData()

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

Add data to the internal sending buffer.

When operation is completed, collected data can be sent at once using SendDataBuffer().

◆ ParseReceivedData()

template<typename T >
void chrono::utils::ChSocketUDP::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 ReceiveDataBuffer().

◆ ReceiveBuffer()

int chrono::utils::ChSocketUDP::ReceiveBuffer ( std::vector< char > &  destination_buffer,
int  byte_size,
std::string &  sender_host_name,
int &  sender_port_number 
)

Receive a buffer of raw bytes from any sender.

Sender host name and port number are additionally returned in provided arguments. 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::ChSocketUDP::ReceiveData ( DataInType &  data_in_1,
DataInTypes &...  data_ins 
)
inline

Receive the given variables from one UDP datagram.

The receiving order follows the order of the arguments. Return the number of bytes received.

◆ ReceiveDataBuffer()

int chrono::utils::ChSocketUDP::ReceiveDataBuffer ( int  data_in_size,
std::string &  sender_host_name,
int &  sender_port_number 
)

Receive one UDP datagram and store it, up to data_in_size bytes, into the internal receiving buffer.

Received data can be subsequently read using ParseReceivedData(). Sender host name and port number are additionally returned in provided arguments. This method clears the internal receiving buffer before operating. Return the number of bytes received.

◆ ReceiveString()

int chrono::utils::ChSocketUDP::ReceiveString ( std::string &  message_in,
std::string &  sender_host_name,
int &  sender_port_number 
)

Receive a string message from any sender.

Sender host name and port number are additionally returned in provided arguments. Return the number of bytes received.

◆ SendBuffer()

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

Send a buffer of raw bytes to the configured destination.

Return the number of bytes sent.

◆ SendData()

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

Send the given variables as a single UDP datagram.

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

◆ SendDataBuffer()

int chrono::utils::ChSocketUDP::SendDataBuffer ( )

Send the data collected into the internal sending buffer, then clear it.

Return the number of bytes sent.

◆ SendString()

int chrono::utils::ChSocketUDP::SendString ( const std::string &  message_out)

Send a string message to the configured destination.

Return the number of bytes sent.

◆ SetDestinationHost()

bool chrono::utils::ChSocketUDP::SetDestinationHost ( const std::string &  server_name_or_addr,
HostType  host_type 
)

Set destination host for subsequent communication [CLIENT SIDE].

Target can be specified either by host name or IP address. Return true on success.


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