waterlinkedsonar
C++ client library for the Water Linked Sonar 3D-15
Loading...
Searching...
No Matches
socket.hpp
Go to the documentation of this file.
1
3
4#ifndef WATERLINKEDSONAR_UDP_SOCKET_HPP
5#define WATERLINKEDSONAR_UDP_SOCKET_HPP
6
7#include <chrono>
8#include <cstddef>
9#include <cstdint>
10#include <optional>
11#include <string>
13
14namespace waterlinked::sonar {
15
17inline constexpr const char* MULTICAST_GROUP = "224.0.0.96";
19inline constexpr std::uint16_t MULTICAST_PORT = 4747;
20
24 enum class Mode : std::uint8_t {
25 MULTICAST,
26 UNICAST,
27 };
28
33 std::uint16_t port{MULTICAST_PORT};
36 std::string interface_ip{"0.0.0.0"};
39};
40
42class UdpSocket {
43 public:
45 struct Datagram {
46 std::size_t size{0};
47 std::string source_ip;
48 };
49
55 explicit UdpSocket(const UdpSocketConfig& config);
56
61 UdpSocket(UdpSocket&& other) noexcept;
63 UdpSocket& operator=(UdpSocket&& other) noexcept;
64 UdpSocket(const UdpSocket&) = delete;
65 UdpSocket& operator=(const UdpSocket&) = delete;
66
72 std::optional<Datagram> receive(Span<std::uint8_t> buffer,
73 std::chrono::milliseconds timeout);
74
78 [[nodiscard]] std::uint16_t local_port() const;
79
80 private:
81 int fd_{-1};
82};
83
84} // namespace waterlinked::sonar
85
86#endif // WATERLINKEDSONAR_UDP_SOCKET_HPP
IPv4 UDP socket bound for the sonar's multicast or unicast stream.
Definition socket.hpp:42
UdpSocket & operator=(UdpSocket &&other) noexcept
Closes this socket and takes over the socket of other.
std::uint16_t local_port() const
Port the socket is bound to; useful when the configured port was 0.
std::optional< Datagram > receive(Span< std::uint8_t > buffer, std::chrono::milliseconds timeout)
Waits up to timeout for a datagram and copies it into buffer, truncating it to the buffer size.
UdpSocket(const UdpSocketConfig &config)
Binds the socket and, in MULTICAST mode, joins the group with SO_REUSEADDR set so that several receiv...
UdpSocket(UdpSocket &&other) noexcept
Takes over the socket of other, which may only be destroyed or assigned to afterwards.
~UdpSocket()
Closes the socket.
constexpr const char * MULTICAST_GROUP
Multicast group the sonar streams to by default.
Definition socket.hpp:17
constexpr std::uint16_t MULTICAST_PORT
Port the sonar streams to by default.
Definition socket.hpp:19
Non-owning view over a contiguous sequence.
tcb::span< T > Span
Non-owning view over a contiguous sequence, with the interface of C++20 std::span.
Definition span.hpp:17
Addressing, port and receive buffer of a UdpSocket.
Definition socket.hpp:22
int receive_buffer_bytes
Requested SO_RCVBUF size. The kernel caps it at net.core.rmem_max.
Definition socket.hpp:38
std::string interface_ip
Interface that joins the group in MULTICAST mode; bind address in UNICAST mode.
Definition socket.hpp:36
std::uint16_t port
Local port. In UNICAST mode, the destination port set on the sonar.
Definition socket.hpp:33
Mode mode
Multicast or unicast.
Definition socket.hpp:29
Mode
Addressing of the received stream.
Definition socket.hpp:24
@ UNICAST
Receive datagrams addressed to interface_ip.
std::string multicast_group
Group joined in MULTICAST mode.
Definition socket.hpp:31
Size and sender of a received datagram.
Definition socket.hpp:45
std::string source_ip
IPv4 address of the sender.
Definition socket.hpp:47
std::size_t size
Bytes written to the buffer.
Definition socket.hpp:46