waterlinkedsonar
C++ client library for the Water Linked Sonar 3D-15
Loading...
Searching...
No Matches
udp_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
18 enum class Mode : std::uint8_t { MULTICAST, UNICAST };
22 std::uint16_t port{MULTICAST_PORT};
24 std::string interface_ip{"0.0.0.0"};
25 int receive_buffer_bytes{4 << 20};
26};
27
30class UdpSocket {
31 public:
33 struct Datagram {
34 std::size_t size;
35 std::string source_ip;
36 };
37
44 explicit UdpSocket(UdpSocketConfig config);
46
47 UdpSocket(const UdpSocket&) = delete;
48 UdpSocket& operator=(const UdpSocket&) = delete;
49
54 std::optional<Datagram> recv(std::uint8_t* buf, std::size_t buf_size,
55 std::chrono::milliseconds timeout);
56
58 [[nodiscard]] std::uint16_t local_port() const;
59
60 private:
61 int fd_{-1};
62};
63
64} // namespace waterlinked::sonar
65
66#endif // WATERLINKEDSONAR_UDP_SOCKET_HPP
Receive-only UDP socket for the sonar's data stream.
Definition udp_socket.hpp:30
std::optional< Datagram > recv(std::uint8_t *buf, std::size_t buf_size, std::chrono::milliseconds timeout)
Blocks up to timeout for one datagram, written to buf.
std::uint16_t local_port() const
The bound port; differs from the configured one only when it was 0.
UdpSocket(const UdpSocket &)=delete
UdpSocket & operator=(const UdpSocket &)=delete
UdpSocket(UdpSocketConfig config)
Opens and binds the socket, requests SO_RCVBUF, and in multicast mode sets SO_REUSEADDR and joins the...
Definition client.hpp:14
constexpr const char * MULTICAST_GROUP
Multicast group address for sonar data streams.
Definition types.hpp:19
constexpr std::uint16_t MULTICAST_PORT
Multicast port for sonar data streams.
Definition types.hpp:21
Socket setup for the sonar's data stream.
Definition udp_socket.hpp:17
int receive_buffer_bytes
SO_RCVBUF request.
Definition udp_socket.hpp:25
std::string interface_ip
Interface for the multicast join, or the bind address for unicast.
Definition udp_socket.hpp:24
std::uint16_t port
For unicast this must match the destination port configured on the sonar.
Definition udp_socket.hpp:22
Mode mode
Definition udp_socket.hpp:19
Mode
Definition udp_socket.hpp:18
std::string multicast_group
Joined in MULTICAST mode.
Definition udp_socket.hpp:20
Byte count and sender address of one received datagram.
Definition udp_socket.hpp:33
std::string source_ip
Definition udp_socket.hpp:35
std::size_t size
Definition udp_socket.hpp:34
Type definitions for the Sonar 3D-15 integration API.