Woman, Life, Freedom


Async BSD Sockets Library
Library protected and public interface header documentation
AsyncBsdSocketLib::UdpClient Class Reference

UDP client which can play both client and listener roles. More...

#include <udp_client.h>

Inheritance diagram for AsyncBsdSocketLib::UdpClient:
AsyncBsdSocketLib::NetworkSocket AsyncBsdSocketLib::Communicator

Public Member Functions

 UdpClient (std::string ipAddress, uint16_t port)
 Unicast UDP socket constructor. More...
 
 UdpClient (std::string ipAddress, uint16_t port, std::string nicIpAddress, std::string multicastIpAddress, bool shareAddress=true)
 Multicast UDP socket constructor. More...
 
int Connection () const noexcept override
 Connection descriptor for sending and receiving. More...
 
bool TrySetup () noexcept override
 Try to setup the communicator. More...
 
template<std::size_t N>
ssize_t Send (const std::array< uint8_t, N > &buffer, std::string ipAddress, uint16_t port) const noexcept
 Send a byte array to a destination. More...
 
template<std::size_t N>
ssize_t Receive (std::array< uint8_t, N > &buffer, std::string &ipAddress, uint16_t &port) const noexcept
 Receive a byte array from a source. More...
 
- Public Member Functions inherited from AsyncBsdSocketLib::Communicator
int Descriptor () const noexcept
 File descriptor. More...
 
virtual bool TryClose () noexcept
 Try to close the communicator. More...
 

Additional Inherited Members

- Protected Member Functions inherited from AsyncBsdSocketLib::NetworkSocket
 NetworkSocket (std::string ipAddress, uint16_t port)
 Constructor. More...
 
- Protected Attributes inherited from AsyncBsdSocketLib::NetworkSocket
const std::string IpAddress
 Listening/Connecting IP address for server/client.
 
const uint16_t Port
 Listening/Connecting port number for server/client.
 
- Protected Attributes inherited from AsyncBsdSocketLib::Communicator
int FileDescriptor
 File descriptor.
 

Detailed Description

UDP client which can play both client and listener roles.

Constructor & Destructor Documentation

◆ UdpClient() [1/2]

AsyncBsdSocketLib::UdpClient::UdpClient ( std::string  ipAddress,
uint16_t  port 
)

Unicast UDP socket constructor.

Parameters
ipAddressBinding IP address
portBinding port number

◆ UdpClient() [2/2]

AsyncBsdSocketLib::UdpClient::UdpClient ( std::string  ipAddress,
uint16_t  port,
std::string  nicIpAddress,
std::string  multicastIpAddress,
bool  shareAddress = true 
)

Multicast UDP socket constructor.

Parameters
ipAddressBinding IP address
portBinding port number
nicIpAddressMulticast traffic network interface IP address
multicastIpAddressMulticast group traffic IP address
shareAddressIndicates whether to share the multicast listening address or not

Member Function Documentation

◆ Connection()

int AsyncBsdSocketLib::UdpClient::Connection ( ) const
overridevirtualnoexcept

Connection descriptor for sending and receiving.

Returns
A non-negative value if there is a valid connection; otherwise -1

Implements AsyncBsdSocketLib::Communicator.

◆ Receive()

template<std::size_t N>
ssize_t AsyncBsdSocketLib::UdpClient::Receive ( std::array< uint8_t, N > &  buffer,
std::string &  ipAddress,
uint16_t &  port 
) const
inlinenoexcept

Receive a byte array from a source.

Template Parameters
NReceive buffer size
Parameters
bufferReceive buffer byte array
ipAddressSource IP address
portSource port number
Returns
Size of received bytes (-1 in case of receiving failed)
Warning
Due to edge-triggered polling, starvation can occur

◆ Send()

template<std::size_t N>
ssize_t AsyncBsdSocketLib::UdpClient::Send ( const std::array< uint8_t, N > &  buffer,
std::string  ipAddress,
uint16_t  port 
) const
inlinenoexcept

Send a byte array to a destination.

Template Parameters
NSend buffer size
Parameters
bufferSend buffer byte array
ipAddressDestination IP address
portDestination port number
Returns
Size of sent bytes (-1 in case of sending failed)

◆ TrySetup()

bool AsyncBsdSocketLib::UdpClient::TrySetup ( )
overridevirtualnoexcept

Try to setup the communicator.

Returns
True if the communicator is successfully set up; otherwise false

Implements AsyncBsdSocketLib::Communicator.