libstp
Robotics library for Botball
Loading...
Searching...
No Matches
shared_transport.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "raccoon/Transport.h"
5
6#include <atomic>
7#include <cstdint>
8#include <functional>
9#include <mutex>
10#include <string>
11#include <unordered_map>
12#include <unordered_set>
13#include <vector>
14
16{
18 {
19 public:
20 using RawHandler = std::function<void(const void*, int)>;
21
23
25 const std::string& channel,
26 const void* data,
27 int data_len,
28 bool reliable,
29 bool retained,
30 int retry_interval_ms,
31 std::uint32_t max_retries);
32
33 template <raccoon::TransportMessage T>
34 bool publish(
35 const std::string& channel,
36 const T& message,
37 const raccoon::PublishOptions& options = {})
38 {
39 ensure_started();
40 return transport_.publish<T>(channel, message, options);
41 }
42
43 std::uint64_t subscribe_raw(
44 const std::string& channel,
45 RawHandler handler,
46 bool reliable,
47 bool request_retained);
48
49 template <raccoon::TransportMessage T>
50 std::uint64_t subscribe(
51 const std::string& channel,
52 std::function<void(const T&)> handler,
53 const raccoon::SubscribeOptions& options = {})
54 {
55 // We forward `options.reliable` as a no-op (kept for binary
56 // compatibility with the LCM-era callers). Silence the
57 // template-instantiation cascade of deprecation warnings; the
58 // attribute still fires for user code that writes
59 // `opts.reliable = true` outside this header.
60#if defined(__GNUC__) || defined(__clang__)
61# pragma GCC diagnostic push
62# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
63#endif
64 const bool reliable_passthrough = options.reliable;
65#if defined(__GNUC__) || defined(__clang__)
66# pragma GCC diagnostic pop
67#endif
68 return subscribe_raw(
69 channel,
70 [handler = std::move(handler)](const void* data, int data_len)
71 {
72 T msg;
73 if (msg.decode(static_cast<const uint8_t*>(data), data_len) >= 0)
74 {
75 handler(msg);
76 }
77 },
78 reliable_passthrough,
79 options.requestRetained);
80 }
81
82 void unsubscribe(std::uint64_t subscription_id);
83 [[nodiscard]] raccoon::TransportStats get_and_reset_stats();
84 [[nodiscard]] std::size_t active_subscription_count() const;
85 void shutdown();
86
89
90 private:
93
94 void ensure_started();
95
96 raccoon::Transport transport_;
97 mutable std::mutex mu_;
98 std::unordered_map<std::string, std::unordered_map<std::uint64_t, RawHandler>> channel_handlers_;
99 std::unordered_map<std::uint64_t, std::string> subscription_channels_;
100 std::unordered_set<std::string> subscribed_channels_;
101 std::atomic<std::uint64_t> next_subscription_id_{1};
103 };
104}
static SharedTransport & instance()
raccoon::TransportStats get_and_reset_stats()
bool publish_raw(const std::string &channel, const void *data, int data_len, bool reliable, bool retained, int retry_interval_ms, std::uint32_t max_retries)
void unsubscribe(std::uint64_t subscription_id)
std::uint64_t subscribe(const std::string &channel, std::function< void(const T &)> handler, const raccoon::SubscribeOptions &options={})
std::uint64_t subscribe_raw(const std::string &channel, RawHandler handler, bool reliable, bool request_retained)
SharedTransport(const SharedTransport &)=delete
bool publish(const std::string &channel, const T &message, const raccoon::PublishOptions &options={})
SharedTransport & operator=(const SharedTransport &)=delete
std::function< void(const void *, int)> RawHandler
std::size_t active_subscription_count() const