Agora  1.2.0
Agora project
UDPComm Class Reference

#include <udp_comm.h>

Public Member Functions

 UDPComm (std::string local_addr, uint16_t local_port, size_t rx_buffer_size, size_t tx_buffer_size)
 
 UDPComm (std::string local_addr, const std::string &local_port, size_t rx_buffer_size, size_t tx_buffer_size)
 getaddrinfo() -> socket -> bind (for local address/port assignment) More...
 
UDPCommoperator= (const UDPComm &)=delete
 
 UDPComm (const UDPComm &)=delete
 
 ~UDPComm ()
 
ssize_t Connect (const std::string &remote_address, uint16_t remote_port) const
 The remote_address | remote_port is the address to which datagrams are sent. 1:1 association me<->remote. More...
 
ssize_t Connect (const std::string &remote_address, const std::string &remote_port) const
 Locate and connect to a remote address. More...
 
void Send (const std::string &rem_hostname, uint16_t rem_port, const std::byte *msg, size_t len)
 Send one UDP packet to a remote server. The client caches the the remote server's addrinfo after resolving it for the first time. After the first time, sending data does not require expensive addrinfo resolution. More...
 
void Send (const std::byte *msg, size_t len)
 Send one UDP packet to the connected remote server. More...
 
ssize_t Recv (std::byte *buf, size_t len) const
 Try to receive up to len bytes in buf by default this will not block. More...
 
ssize_t Recv (const std::string &src_address, uint16_t src_port, std::byte *buf, size_t len)
 Try once to receive up to len bytes in buf. More...
 
void EnableRecording ()
 
void MakeBlocking (size_t rx_timeout_sec=0) const
 Configures the socket in blocking mode. Any calls to recv / send will now block. More...
 

Static Public Attributes

static constexpr bool kDebugPrintUdpInit = false
 
static constexpr bool kDebugPrintUdpSend = false
 
static constexpr bool kDebugPrintUdpRecv = false
 

Private Attributes

int sock_fd_ = -1
 The raw socket file descriptor. More...
 
std::map< std::string, addrinfo * > addrinfo_map_
 A cache mapping hostname:udp_port to addrinfo. More...
 
std::mutex map_insert_access_
 Variable to control write access to the non-thread safe data structures. More...
 
std::vector< std::vector< uint8_t > > sent_vec_
 All packets sent, maintained if recording is enabled. More...
 
bool enable_recording_flag_ = false
 If set to ture, we record all sent packets, otherwise we dont. More...
 

Constructor & Destructor Documentation

◆ UDPComm() [1/3]

UDPComm::UDPComm ( std::string  local_addr,
uint16_t  local_port,
size_t  rx_buffer_size,
size_t  tx_buffer_size 
)
explicit

◆ UDPComm() [2/3]

UDPComm::UDPComm ( std::string  local_addr,
const std::string &  local_port,
size_t  rx_buffer_size,
size_t  tx_buffer_size 
)
explicit

getaddrinfo() -> socket -> bind (for local address/port assignment)

loop through all the results and bind to the first we can

NON blocking by default

Here is the call graph for this function:

◆ UDPComm() [3/3]

UDPComm::UDPComm ( const UDPComm )
delete

◆ ~UDPComm()

UDPComm::~UDPComm ( )
Here is the call graph for this function:

Member Function Documentation

◆ Connect() [1/2]

ssize_t UDPComm::Connect ( const std::string &  remote_address,
const std::string &  remote_port 
) const

Locate and connect to a remote address.

Returns
Connect for DGRAM sockets just indicates a 1:1 socket

Find the local interface

Here is the call graph for this function:

◆ Connect() [2/2]

ssize_t UDPComm::Connect ( const std::string &  remote_address,
uint16_t  remote_port 
) const

The remote_address | remote_port is the address to which datagrams are sent. 1:1 association me<->remote.

Parameters
remote_addressHostname or IP address of the endpoint
remote_portUDP port of the endpoint
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EnableRecording()

void UDPComm::EnableRecording ( )
inline
Here is the caller graph for this function:

◆ MakeBlocking()

void UDPComm::MakeBlocking ( size_t  rx_timeout_sec = 0) const

Configures the socket in blocking mode. Any calls to recv / send will now block.

Here is the caller graph for this function:

◆ operator=()

UDPComm& UDPComm::operator= ( const UDPComm )
delete

◆ Recv() [1/2]

ssize_t UDPComm::Recv ( const std::string &  src_address,
uint16_t  src_port,
std::byte *  buf,
size_t  len 
)

Try once to receive up to len bytes in buf.

Returns
Return the number of bytes received if non-zero bytes are received. If no bytes are received, return zero. If there was an error in receiving, return -1.
Here is the call graph for this function:

◆ Recv() [2/2]

ssize_t UDPComm::Recv ( std::byte *  buf,
size_t  len 
) const

Try to receive up to len bytes in buf by default this will not block.

Returns
Return the number of bytes received if non-zero bytes are received. If no bytes are received, return zero. If there was an error in receiving, return -1.
Here is the caller graph for this function:

◆ Send() [1/2]

void UDPComm::Send ( const std::byte *  msg,
size_t  len 
)

Send one UDP packet to the connected remote server.

Parameters
msgPointer to the message to send
lenLength in bytes of the message to send

◆ Send() [2/2]

void UDPComm::Send ( const std::string &  rem_hostname,
uint16_t  rem_port,
const std::byte *  msg,
size_t  len 
)

Send one UDP packet to a remote server. The client caches the the remote server's addrinfo after resolving it for the first time. After the first time, sending data does not require expensive addrinfo resolution.

Parameters
to_hostnameHostname or IP address of the remote server
to_portUDP port that the remote server is listening on
msgPointer to the message to send
lenLength in bytes of the message to send
rem_hostnameHostname or IP address of the remote server
rem_portUDP port that the remote server is listening on
msgPointer to the message to send
lenLength in bytes of the message to send
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ addrinfo_map_

std::map<std::string, addrinfo*> UDPComm::addrinfo_map_
private

A cache mapping hostname:udp_port to addrinfo.

◆ enable_recording_flag_

bool UDPComm::enable_recording_flag_ = false
private

If set to ture, we record all sent packets, otherwise we dont.

◆ kDebugPrintUdpInit

constexpr bool UDPComm::kDebugPrintUdpInit = false
staticconstexpr

◆ kDebugPrintUdpRecv

constexpr bool UDPComm::kDebugPrintUdpRecv = false
staticconstexpr

◆ kDebugPrintUdpSend

constexpr bool UDPComm::kDebugPrintUdpSend = false
staticconstexpr

◆ map_insert_access_

std::mutex UDPComm::map_insert_access_
private

Variable to control write access to the non-thread safe data structures.

◆ sent_vec_

std::vector<std::vector<uint8_t> > UDPComm::sent_vec_
private

All packets sent, maintained if recording is enabled.

◆ sock_fd_

int UDPComm::sock_fd_ = -1
private

The raw socket file descriptor.


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