PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
udpSocket.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <asio.hpp>
12#include <chrono>
13#include <cstddef>
14#include <cstdint>
15#include <memory>
16#include <string>
17#include <vector>
18
19#include "profinet/IUdpSocket.h"
20
21namespace profinet
22{
23
39class UdpSocket final : public IUdpSocket
40{
41 public:
43 static constexpr double defaultTimeout = 5.0;
44
46 static constexpr int defaultReceiveBufferSize = 65536;
47
57 const std::string& host,
58 std::uint16_t port,
59 std::chrono::duration<double> timeout =
60 std::chrono::duration<double>(defaultTimeout));
61
72 explicit UdpSocket(
73 std::uint16_t localPort,
74 std::chrono::duration<double> timeout =
75 std::chrono::duration<double>(defaultTimeout));
76
78 ~UdpSocket() override;
79
81 UdpSocket(const UdpSocket&) = delete;
82
84 UdpSocket& operator=(const UdpSocket&) = delete;
85
88 UdpSocket(UdpSocket&& other) noexcept;
89
93 UdpSocket& operator=(UdpSocket&& other) noexcept;
94
99 void Send(const std::vector<std::uint8_t>& data) const;
100
106 [[nodiscard]] std::vector<std::uint8_t> Recv(
107 std::size_t maxLen = defaultReceiveBufferSize) const;
108
114 [[nodiscard]] UdpDatagram RecvFrom(std::size_t maxLen) override;
115
121 void SendTo(const std::vector<std::uint8_t>& data, const UdpEndpoint&) override;
122
126 void SetTimeout(std::chrono::milliseconds timeout) override;
127
132 [[nodiscard]] UdpEndpoint LocalEndpoint() const;
133
134 private:
136 std::unique_ptr<asio::io_context> ioContext;
137
139 std::unique_ptr<asio::ip::udp::socket> socket;
140
142 std::chrono::milliseconds receiveTimeout{
143 std::chrono::duration_cast<std::chrono::milliseconds>(
144 std::chrono::duration<double>(defaultTimeout))};
145};
146
147} // namespace profinet
Abstraction for IPv4 UDP datagram transport.
Abstract interface for IPv4 UDP datagram transport.
Definition IUdpSocket.h:99
RAII wrapper around a portable IPv4 UDP socket.
Definition udpSocket.h:40
std::vector< std::uint8_t > Recv(std::size_t maxLen=defaultReceiveBufferSize) const
Receive a UDP datagram from the connected peer.
void SendTo(const std::vector< std::uint8_t > &data, const UdpEndpoint &) override
Send a datagram to a specific IPv4 endpoint.
std::chrono::milliseconds receiveTimeout
Receive timeout applied to synchronous receive operations.
Definition udpSocket.h:142
void Send(const std::vector< std::uint8_t > &data) const
Send a UDP datagram to the connected peer.
std::unique_ptr< asio::io_context > ioContext
Asio execution context owned by this socket.
Definition udpSocket.h:136
UdpDatagram RecvFrom(std::size_t maxLen) override
Receive a datagram together with its source endpoint.
std::unique_ptr< asio::ip::udp::socket > socket
Underlying portable Asio UDP socket.
Definition udpSocket.h:139
static constexpr int defaultReceiveBufferSize
Default receive buffer size.
Definition udpSocket.h:46
static constexpr double defaultTimeout
Default receive timeout in seconds.
Definition udpSocket.h:43
UdpSocket & operator=(const UdpSocket &)=delete
Not copyable.
UdpSocket(const UdpSocket &)=delete
Not copyable.
UdpEndpoint LocalEndpoint() const
Return the local IPv4 endpoint assigned to the socket.
~UdpSocket() override
Destroy the socket and release all owned resources.
void SetTimeout(std::chrono::milliseconds timeout) override
Set the receive timeout used by subsequent receive operations.
A UDP datagram together with the IPv4 address of its sender.
Definition IUdpSocket.h:65
Portable IPv4 UDP endpoint.
Definition IUdpSocket.h:42