PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
profinet::UdpSocket Class Referencefinal

RAII wrapper around a portable IPv4 UDP socket. More...

#include <udpSocket.h>

+ Inheritance diagram for profinet::UdpSocket:
+ Collaboration diagram for profinet::UdpSocket:

Public Member Functions

 UdpSocket (const std::string &host, std::uint16_t port, std::chrono::duration< double > timeout=std::chrono::duration< double >(defaultTimeout))
 Resolve a host and connect a UDP socket to it.
 
 UdpSocket (std::uint16_t localPort, std::chrono::duration< double > timeout=std::chrono::duration< double >(defaultTimeout))
 Create and bind a UDP listener socket.
 
 ~UdpSocket () override
 Destroy the socket and release all owned resources.
 
 UdpSocket (const UdpSocket &)=delete
 Not copyable.
 
UdpSocket & operator= (const UdpSocket &)=delete
 Not copyable.
 
 UdpSocket (UdpSocket &&other) noexcept
 Move-construct and transfer socket ownership.
 
UdpSocket & operator= (UdpSocket &&other) noexcept
 Move-assign and transfer socket ownership.
 
void Send (const std::vector< std::uint8_t > &data) const
 Send a UDP datagram to the connected peer.
 
std::vector< std::uint8_t > Recv (std::size_t maxLen=defaultReceiveBufferSize) const
 Receive a UDP datagram from the connected peer.
 
UdpDatagram RecvFrom (std::size_t maxLen) override
 Receive a datagram together with its source endpoint.
 
void SendTo (const std::vector< std::uint8_t > &data, const UdpEndpoint &) override
 Send a datagram to a specific IPv4 endpoint.
 
void SetTimeout (std::chrono::milliseconds timeout) override
 Set the receive timeout used by subsequent receive operations.
 
UdpEndpoint LocalEndpoint () const
 Return the local IPv4 endpoint assigned to the socket.
 
- Public Member Functions inherited from profinet::IUdpSocket
virtual ~IUdpSocket ()=default
 Virtual destructor for polymorphic use.
 

Static Public Attributes

static constexpr double defaultTimeout = 5.0
 Default receive timeout in seconds.
 
static constexpr int defaultReceiveBufferSize = 65536
 Default receive buffer size.
 

Private Attributes

std::unique_ptr< asio::io_context > ioContext
 Asio execution context owned by this socket.
 
std::unique_ptr< asio::ip::udp::socket > socket
 Underlying portable Asio UDP socket.
 
std::chrono::milliseconds receiveTimeout
 Receive timeout applied to synchronous receive operations.
 

Detailed Description

RAII wrapper around a portable IPv4 UDP socket.

Uses standalone Asio for socket creation, name resolution, sending, receiving, binding, and receive timeout handling.

A socket created with the host/port constructor is connected to the specified peer.

A socket created with the local-port constructor is bound to all local IPv4 interfaces and can receive datagrams from arbitrary peers.

Note
The operating-system socket representation is intentionally not exposed by this class.

Definition at line 39 of file udpSocket.h.

Constructor & Destructor Documentation

◆ UdpSocket() [1/4]

profinet::UdpSocket::UdpSocket ( const std::string &  host,
std::uint16_t  port,
std::chrono::duration< double >  timeout = std::chrono::duration<double>(defaultTimeout) 
)

Resolve a host and connect a UDP socket to it.

Parameters
hostHostname or IPv4 address of the remote peer.
portRemote UDP port.
timeoutReceive timeout.
Exceptions
PermissionDeniedErrorWhen the operating system denies access.
SocketErrorWhen resolution, socket creation, or connection fails.

Definition at line 48 of file udpSocket.cpp.

References ioContext, SetTimeout(), and socket.

+ Here is the call graph for this function:

◆ UdpSocket() [2/4]

profinet::UdpSocket::UdpSocket ( std::uint16_t  localPort,
std::chrono::duration< double >  timeout = std::chrono::duration<double>(defaultTimeout) 
)
explicit

Create and bind a UDP listener socket.

Binds the socket to 0.0.0.0 on the supplied local UDP port.

Parameters
localPortLocal UDP port.
timeoutReceive timeout.
Exceptions
PermissionDeniedErrorWhen the operating system denies access.
SocketErrorWhen socket creation, configuration, or binding fails.

Definition at line 90 of file udpSocket.cpp.

References SetTimeout(), and socket.

+ Here is the call graph for this function:

◆ ~UdpSocket()

profinet::UdpSocket::~UdpSocket ( )
override

Destroy the socket and release all owned resources.

Definition at line 130 of file udpSocket.cpp.

References socket.

◆ UdpSocket() [3/4]

profinet::UdpSocket::UdpSocket ( const UdpSocket &  )
delete

Not copyable.

◆ UdpSocket() [4/4]

profinet::UdpSocket::UdpSocket ( UdpSocket &&  other)
noexcept

Move-construct and transfer socket ownership.

Parameters
otherSocket to move from.

Definition at line 140 of file udpSocket.cpp.

Member Function Documentation

◆ LocalEndpoint()

UdpEndpoint profinet::UdpSocket::LocalEndpoint ( ) const

Return the local IPv4 endpoint assigned to the socket.

Returns
Local IPv4 address and UDP port.
Exceptions
SocketErrorWhen the local endpoint cannot be queried.

Definition at line 372 of file udpSocket.cpp.

References socket.

Referenced by profinet::TEST(), and profinet::TEST().

+ Here is the caller graph for this function:

◆ operator=() [1/2]

UdpSocket & profinet::UdpSocket::operator= ( const UdpSocket &  )
delete

Not copyable.

◆ operator=() [2/2]

UdpSocket & profinet::UdpSocket::operator= ( UdpSocket &&  other)
noexcept

Move-assign and transfer socket ownership.

Parameters
otherSocket to move from.
Returns
Reference to this socket.

Definition at line 147 of file udpSocket.cpp.

References ioContext.

◆ Recv()

std::vector< std::uint8_t > profinet::UdpSocket::Recv ( std::size_t  maxLen = defaultReceiveBufferSize) const

Receive a UDP datagram from the connected peer.

Parameters
maxLenMaximum payload size.
Returns
Received payload, or an empty vector on timeout.
Exceptions
SocketErrorWhen reception fails.

Definition at line 191 of file udpSocket.cpp.

References buffer, ioContext, receivedBytes, receiveTimeout, socket, timedOut, and timer.

Referenced by profinet::TEST().

+ Here is the caller graph for this function:

◆ RecvFrom()

UdpDatagram profinet::UdpSocket::RecvFrom ( std::size_t  maxLen)
overridevirtual

Receive a datagram together with its source endpoint.

Parameters
maxLenMaximum payload size.
Returns
Received payload and source endpoint, or an empty datagram on timeout.
Exceptions
SocketErrorWhen reception fails.

Implements profinet::IUdpSocket.

Definition at line 254 of file udpSocket.cpp.

References profinet::UdpEndpoint::address, profinet::UdpDatagram::data, ioContext, profinet::UdpEndpoint::port, receivedBytes, receiveTimeout, socket, profinet::UdpDatagram::source, timedOut, and timer.

Referenced by profinet::TEST(), profinet::TEST(), and profinet::TEST().

+ Here is the caller graph for this function:

◆ Send()

void profinet::UdpSocket::Send ( const std::vector< std::uint8_t > &  data) const

Send a UDP datagram to the connected peer.

Parameters
dataDatagram payload.
Exceptions
SocketErrorWhen transmission fails.

Definition at line 166 of file udpSocket.cpp.

References socket.

Referenced by profinet::TEST(), and profinet::TEST().

+ Here is the caller graph for this function:

◆ SendTo()

void profinet::UdpSocket::SendTo ( const std::vector< std::uint8_t > &  data,
const UdpEndpoint &  destination 
)
overridevirtual

Send a datagram to a specific IPv4 endpoint.

Parameters
dataDatagram payload.
destinationDestination IPv4 endpoint.
Exceptions
SocketErrorWhen transmission fails.

Implements profinet::IUdpSocket.

Definition at line 325 of file udpSocket.cpp.

References profinet::UdpEndpoint::address, profinet::UdpEndpoint::port, and socket.

Referenced by profinet::TEST().

+ Here is the caller graph for this function:

◆ SetTimeout()

void profinet::UdpSocket::SetTimeout ( std::chrono::milliseconds  timeout)
overridevirtual

Set the receive timeout used by subsequent receive operations.

Parameters
timeoutMaximum receive wait time.

Implements profinet::IUdpSocket.

Definition at line 367 of file udpSocket.cpp.

References receiveTimeout.

Referenced by UdpSocket(), and UdpSocket().

+ Here is the caller graph for this function:

Member Data Documentation

◆ defaultReceiveBufferSize

constexpr int profinet::UdpSocket::defaultReceiveBufferSize = 65536
staticconstexpr

Default receive buffer size.

Definition at line 46 of file udpSocket.h.

◆ defaultTimeout

constexpr double profinet::UdpSocket::defaultTimeout = 5.0
staticconstexpr

Default receive timeout in seconds.

Definition at line 43 of file udpSocket.h.

◆ ioContext

std::unique_ptr<asio::io_context> profinet::UdpSocket::ioContext
private

Asio execution context owned by this socket.

Definition at line 136 of file udpSocket.h.

Referenced by UdpSocket(), Recv(), and RecvFrom().

◆ receiveTimeout

std::chrono::milliseconds profinet::UdpSocket::receiveTimeout
private
Initial value:
{
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::duration<double>(defaultTimeout))}
static constexpr double defaultTimeout
Default receive timeout in seconds.
Definition udpSocket.h:43

Receive timeout applied to synchronous receive operations.

Definition at line 142 of file udpSocket.h.

Referenced by Recv(), RecvFrom(), and SetTimeout().

◆ socket

std::unique_ptr<asio::ip::udp::socket> profinet::UdpSocket::socket
private

Underlying portable Asio UDP socket.

Definition at line 139 of file udpSocket.h.

Referenced by UdpSocket(), UdpSocket(), ~UdpSocket(), LocalEndpoint(), Recv(), RecvFrom(), Send(), and SendTo().


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