OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
rate_limiting_protocol.hpp
1#pragma once
3#include <ossia/detail/flat_map.hpp>
4#include <ossia/network/base/message_queue.hpp>
5#include <ossia/network/base/parameter_data.hpp>
6#include <ossia/network/base/protocol.hpp>
7
8#include <readerwriterqueue.h>
9
10#include <chrono>
11#include <thread>
12#include <vector>
13
14namespace ossia::net
15{
16struct rate_limiter;
17class OSSIA_EXPORT rate_limiting_protocol final : public ossia::net::protocol_base
18{
19public:
20 using clock = std::chrono::high_resolution_clock;
21 using duration = clock::duration;
22 rate_limiting_protocol(duration d, std::unique_ptr<protocol_base> arg);
23 ~rate_limiting_protocol() override;
24
25 void set_duration(duration d);
26
27private:
28 bool pull(ossia::net::parameter_base&) override;
29 bool push(const ossia::net::parameter_base& addr, const ossia::value& v) override;
30 bool push_raw(const full_parameter_data&) override;
31 bool observe(ossia::net::parameter_base&, bool) override;
32 bool update(ossia::net::node_base& node_base) override;
33
34 void set_logger(const network_logger& l) override;
35 const network_logger& get_logger() const noexcept override;
36
37 void stop() override;
38 void set_device(ossia::net::device_base& dev) override;
39
40 void parameter_removed(const ossia::net::parameter_base& b);
41
42 rate_limiting_protocol() = delete;
43 rate_limiting_protocol(const rate_limiting_protocol&) = delete;
44 rate_limiting_protocol(rate_limiting_protocol&&) = delete;
45 rate_limiting_protocol& operator=(const rate_limiting_protocol&) = delete;
46 rate_limiting_protocol& operator=(rate_limiting_protocol&&) = delete;
47
48 friend struct rate_limiter;
49
50 std::atomic<duration> m_duration{};
51 std::unique_ptr<ossia::net::protocol_base> m_protocol;
52 ossia::net::device_base* m_device{};
53
54 std::atomic_bool m_running{true};
55 std::thread m_thread;
56
57 clock::time_point m_lastTime;
58 using map_t = ossia::flat_map<
59 const ossia::net::parameter_base*, std::pair<ossia::value, clock::time_point>>;
60 map_t m_userMessages;
61 map_t m_buffer;
62 map_t m_threadMessages;
63 std::mutex m_msgMutex;
64};
65
66template <typename Protocol, typename... Args>
67auto limit_output_rate(std::chrono::milliseconds ms, Args&&... args)
68{
69 return std::make_unique<rate_limiting_protocol>(
70 ms, std::make_unique<Protocol>(std::forward<Args>(args)...));
71}
72
73}
Root of a device tree.
Definition ossia/network/base/device.hpp:58
The node_base class.
Definition node.hpp:48
The parameter_base class.
Definition ossia/network/base/parameter.hpp:48
The protocol_base class.
Definition protocol.hpp:40
The value class.
Definition value.hpp:173
Definition git_info.h:7