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

A connected UDP socket with a timeout. More...

#include <util.h>

+ 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 ()
 Close the underlying socket.
 
 UdpSocket (const UdpSocket &)=delete
 Not copyable (owns a socket file descriptor).
 
UdpSocketoperator= (const UdpSocket &)=delete
 Not copyable (owns a socket file descriptor).
 
 UdpSocket (UdpSocket &&other) noexcept
 Move-construct, transferring ownership of the underlying socket.
 
UdpSocketoperator= (UdpSocket &&other) noexcept
 Move-assign, transferring ownership of the underlying socket.
 
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.
 
int Fd () const
 The underlying file descriptor.
 

Static Public Attributes

static constexpr double defaultTimeout = 5.0
 Default Timeout.
 
static constexpr int defaultReceiveBufferSize = 65536
 Default receive buffer size.
 

Private Attributes

int fd = -1
 Underlying socket file descriptor, or -1 if closed/moved-from.
 

Detailed Description

A connected UDP socket with a timeout.

Works cross-platform; no raw-socket privileges required.

Definition at line 253 of file util.h.

Constructor & Destructor Documentation

◆ UdpSocket() [1/3]

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 IP address to connect to.
portUDP port to connect to.
timeoutReceive timeout.

Definition at line 402 of file util.cpp.

References fd.

◆ ~UdpSocket()

profinet::UdpSocket::~UdpSocket ( )

Close the underlying socket.

Definition at line 442 of file util.cpp.

References fd.

◆ UdpSocket() [2/3]

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

Not copyable (owns a socket file descriptor).

◆ UdpSocket() [3/3]

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

Move-construct, transferring ownership of the underlying socket.

Parameters
otherSocket to move from; left closed.

Definition at line 450 of file util.cpp.

Member Function Documentation

◆ Fd()

int profinet::UdpSocket::Fd ( ) const
inline

The underlying file descriptor.

Returns
The raw socket file descriptor.

Definition at line 298 of file util.h.

References fd.

◆ operator=() [1/2]

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

Not copyable (owns a socket file descriptor).

◆ operator=() [2/2]

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

Move-assign, transferring ownership of the underlying socket.

Parameters
otherSocket to move from; left closed.
Returns
Reference to this socket.

Definition at line 456 of file util.cpp.

◆ 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 number of bytes to receive.
Returns
The received datagram, or an empty vector on timeout.

Definition at line 479 of file util.cpp.

References fd.

◆ Send()

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

Send a UDP datagram to the connected peer.

Parameters
dataDatagram payload.

Definition at line 470 of file util.cpp.

References fd.

Member Data Documentation

◆ defaultReceiveBufferSize

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

Default receive buffer size.

Definition at line 260 of file util.h.

◆ defaultTimeout

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

Default Timeout.

Definition at line 257 of file util.h.

◆ fd

int profinet::UdpSocket::fd = -1
private

Underlying socket file descriptor, or -1 if closed/moved-from.

Definition at line 305 of file util.h.

Referenced by UdpSocket(), ~UdpSocket(), Fd(), Recv(), and Send().


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