SatCat5
io_throttle.h
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Throttled I/O adapters
6 //
7 
8 #pragma once
9 
10 #include <satcat5/io_writeable.h>
11 #include <satcat5/polling.h>
12 #include <satcat5/timeref.h>
13 
14 namespace satcat5 {
15  namespace io {
21  , public satcat5::poll::Timer {
22  public:
24  satcat5::io::Writeable* dst, // Downstream object
25  unsigned rate_bps = 1000000); // Rate limit (bits/sec)
26 
28  inline void set_rate(unsigned rate_bps)
29  { m_rate_bps = rate_bps; }
30 
31  // Override required portions of the Writeable API.
32  unsigned get_write_space() const override;
33  bool write_finalize() override;
34 
35  protected:
36  unsigned calc_tokens(unsigned usec) const;
37  void timer_event() override; // Timer event handler
38 
39  // Internal state.
40  unsigned m_rate_bps; // Rate limit (bits per second)
41  unsigned m_tokens; // Accumulated credits (bytes)
42  satcat5::util::TimeVal m_tref; // Time of last update
43  };
44  }
45 }
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Wrapper class for forwarding writes to another object.
Definition: io_writeable.h:218
A rate-controlled io::WriteableRedirect.
Definition: io_throttle.h:21
void timer_event() override
Child class MUST override this method.
Definition: io_throttle.cc:49
unsigned get_write_space() const override
How many bytes can be written without blocking?
Definition: io_throttle.cc:25
bool write_finalize() override
Mark end of frame and release temporary working data.
Definition: io_throttle.cc:33
void set_rate(unsigned rate_bps)
Adjust the rate limit.
Definition: io_throttle.h:28
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
"Writeable" I/O interface core definitions
Core event-processing loop for SatCat5 software.
Timestamp for measuring elapsed time.
Definition: timeref.h:48
TimeRef and TimeVal define the API for monotonic timers.