|
PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
|
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. | |
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.
Definition at line 39 of file udpSocket.h.
| 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.
| host | Hostname or IPv4 address of the remote peer. |
| port | Remote UDP port. |
| timeout | Receive timeout. |
| PermissionDeniedError | When the operating system denies access. |
| SocketError | When 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:
|
explicit |
Create and bind a UDP listener socket.
Binds the socket to 0.0.0.0 on the supplied local UDP port.
| localPort | Local UDP port. |
| timeout | Receive timeout. |
| PermissionDeniedError | When the operating system denies access. |
| SocketError | When 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:
|
override |
Destroy the socket and release all owned resources.
Definition at line 130 of file udpSocket.cpp.
References socket.
|
delete |
Not copyable.
|
noexcept |
Move-construct and transfer socket ownership.
| other | Socket to move from. |
Definition at line 140 of file udpSocket.cpp.
| UdpEndpoint profinet::UdpSocket::LocalEndpoint | ( | ) | const |
Return the local IPv4 endpoint assigned to the socket.
| SocketError | When 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:Move-assign and transfer socket ownership.
| other | Socket to move from. |
Definition at line 147 of file udpSocket.cpp.
References ioContext.
| std::vector< std::uint8_t > profinet::UdpSocket::Recv | ( | std::size_t | maxLen = defaultReceiveBufferSize | ) | const |
Receive a UDP datagram from the connected peer.
| maxLen | Maximum payload size. |
| SocketError | When 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:
|
overridevirtual |
Receive a datagram together with its source endpoint.
| maxLen | Maximum payload size. |
| SocketError | When 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:| void profinet::UdpSocket::Send | ( | const std::vector< std::uint8_t > & | data | ) | const |
Send a UDP datagram to the connected peer.
| data | Datagram payload. |
| SocketError | When 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:
|
overridevirtual |
Send a datagram to a specific IPv4 endpoint.
| data | Datagram payload. |
| destination | Destination IPv4 endpoint. |
| SocketError | When 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:
|
overridevirtual |
Set the receive timeout used by subsequent receive operations.
| timeout | Maximum 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:
|
staticconstexpr |
Default receive buffer size.
Definition at line 46 of file udpSocket.h.
|
staticconstexpr |
Default receive timeout in seconds.
Definition at line 43 of file udpSocket.h.
|
private |
Asio execution context owned by this socket.
Definition at line 136 of file udpSocket.h.
Referenced by UdpSocket(), Recv(), and RecvFrom().
|
private |
Receive timeout applied to synchronous receive operations.
Definition at line 142 of file udpSocket.h.
Referenced by Recv(), RecvFrom(), and SetTimeout().
|
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().