waterlinkedsonar
C++ client library for the Water Linked Sonar 3D-15
Loading...
Searching...
No Matches
receiver.hpp
Go to the documentation of this file.
1
3
4#ifndef WATERLINKEDSONAR_UDP_RECEIVER_HPP
5#define WATERLINKEDSONAR_UDP_RECEIVER_HPP
6
7#include <atomic>
8#include <chrono>
9#include <cstdint>
10#include <functional>
11#include <optional>
12#include <string>
13#include <system_error>
14#include <thread>
18
19namespace waterlinked::sonar {
20
25 std::optional<std::string> source_ip;
26};
27
32 std::uint64_t datagrams_received{0};
33 std::uint64_t range_images{0};
34 std::uint64_t bitmap_images{0};
35 std::uint64_t imu_batches{0};
36 std::uint64_t unknown_type{0};
37 std::uint64_t decode_errors{0};
38 std::uint64_t filtered_source{0};
40 std::uint32_t last_sequence_id{0};
43 std::chrono::steady_clock::time_point last_datagram_time;
44};
45
51class Receiver {
52 public:
55 explicit Receiver(UdpSocket socket, ReceiverOptions options = {});
56
60
61 Receiver(const Receiver&) = delete;
62 Receiver& operator=(const Receiver&) = delete;
63 Receiver(Receiver&&) = delete;
64 Receiver& operator=(Receiver&&) = delete;
65
69 void on_range_image(std::function<void(const RangeImageView&)> callback);
70
72 void on_bitmap_image(std::function<void(const BitmapImageView&)> callback);
73
75 void on_imu_batch(std::function<void(const ImuBatchView&)> callback);
76
79 void on_decode_error(std::function<void(rip::DecodeStatus)> callback);
80
83 void on_socket_error(std::function<void(const std::error_code&)> callback);
84
89 void start();
90
93 void stop();
94
97 [[nodiscard]] bool running() const noexcept;
98
100 [[nodiscard]] ReceiverStats stats() const noexcept;
101
102 private:
103 void require_not_started() const;
104 void receive_loop();
105 void dispatch(const rip::Decoded& decoded);
106
107 UdpSocket socket_;
108 ReceiverOptions options_;
109 rip::Decoder decoder_;
110
111 std::function<void(const RangeImageView&)> on_range_image_;
112 std::function<void(const BitmapImageView&)> on_bitmap_image_;
113 std::function<void(const ImuBatchView&)> on_imu_batch_;
114 std::function<void(rip::DecodeStatus)> on_decode_error_;
115 std::function<void(const std::error_code&)> on_socket_error_;
116
117 std::thread thread_;
118 std::atomic<std::thread::id> loop_thread_;
119 std::atomic<bool> running_{false};
120 bool started_{false};
121
122 std::atomic<std::uint64_t> datagrams_received_{0};
123 std::atomic<std::uint64_t> range_images_{0};
124 std::atomic<std::uint64_t> bitmap_images_{0};
125 std::atomic<std::uint64_t> imu_batches_{0};
126 std::atomic<std::uint64_t> unknown_type_{0};
127 std::atomic<std::uint64_t> decode_errors_{0};
128 std::atomic<std::uint64_t> filtered_source_{0};
129 std::atomic<std::uint32_t> last_sequence_id_{0};
130 std::atomic<std::chrono::steady_clock::rep> last_datagram_ticks_{0};
131};
132
133} // namespace waterlinked::sonar
134
135#endif // WATERLINKEDSONAR_UDP_RECEIVER_HPP
Receives datagrams on a thread of its own, decodes them and passes the messages to the registered cal...
Definition receiver.hpp:51
void on_range_image(std::function< void(const RangeImageView &)> callback)
Sets the callback for range images, replacing any earlier one.
ReceiverStats stats() const noexcept
Current counters; callable from any thread.
void on_socket_error(std::function< void(const std::error_code &)> callback)
Sets the callback for the socket error that ends the receive thread; see on_range_image().
void on_bitmap_image(std::function< void(const BitmapImageView &)> callback)
Sets the callback for bitmap images; see on_range_image().
void start()
Starts the receive thread.
void on_imu_batch(std::function< void(const ImuBatchView &)> callback)
Sets the callback for IMU batches; see on_range_image().
bool running() const noexcept
Whether the receive thread is running; false once a socket error has ended it.
void on_decode_error(std::function< void(rip::DecodeStatus)> callback)
Sets the callback for datagrams that fail to decode; see on_range_image().
Receiver(UdpSocket socket, ReceiverOptions options={})
Takes over socket.
void stop()
Stops the receive thread and joins it unless called from a callback.
IPv4 UDP socket bound for the sonar's multicast or unicast stream.
Definition socket.hpp:42
Decoder for Range Image Protocol packets.
DecodeStatus
Outcome of decoding one packet.
Definition decoder.hpp:21
Views of the messages carried by the Range Image Protocol.
Receive-only UDP socket for the sonar's data stream.
8-bit greyscale image that can be displayed as is.
Definition messages.hpp:52
IMU samples taken at about 100 Hz while acoustics is enabled.
Definition messages.hpp:69
Distance to the strongest reflection for each pixel.
Definition messages.hpp:26
Filtering applied to datagrams before decoding.
Definition receiver.hpp:22
std::optional< std::string > source_ip
Sender to accept; datagrams from other senders are dropped.
Definition receiver.hpp:25
Counters accumulated since Receiver::start().
Definition receiver.hpp:31
std::uint64_t datagrams_received
Datagrams that passed the filter.
Definition receiver.hpp:32
std::uint64_t range_images
Decoded range images.
Definition receiver.hpp:33
std::uint64_t unknown_type
Valid packets of unpublished types.
Definition receiver.hpp:36
std::chrono::steady_clock::time_point last_datagram_time
Arrival of the latest datagram that passed the filter; the epoch before the first one.
Definition receiver.hpp:43
std::uint64_t bitmap_images
Decoded bitmap images.
Definition receiver.hpp:34
std::uint64_t decode_errors
Datagrams that failed to decode.
Definition receiver.hpp:37
std::uint32_t last_sequence_id
Sequence ID of the latest range or bitmap image.
Definition receiver.hpp:40
std::uint64_t filtered_source
Dropped by ReceiverOptions::source_ip.
Definition receiver.hpp:38
std::uint64_t imu_batches
Decoded IMU batches.
Definition receiver.hpp:35