libfilezilla
rate_limited_layer.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_RATE_LIMITED_LAYER_HEADER
2 #define LIBFILEZILLA_RATE_LIMITED_LAYER_HEADER
3 
8 #include "rate_limiter.hpp"
9 #include "socket.hpp"
10 
11 namespace fz {
12 
18 class FZ_PUBLIC_SYMBOL rate_limited_layer final : public socket_layer, private bucket
19 {
20 public:
21  rate_limited_layer(event_handler* handler, socket_interface& next_layer, rate_limiter * limiter = nullptr);
22  virtual ~rate_limited_layer();
23 
24  virtual int read(void* buffer, unsigned int size, int& error) override;
25  virtual int write(void const* buffer, unsigned int size, int& error) override;
26 
27  virtual socket_state get_state() const override {
28  return next_layer_.get_state();
29  }
30 
31  virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) override {
32  return next_layer_.connect(host, port, family);
33  }
34 
35  virtual int shutdown() override {
36  return next_layer_.shutdown();
37  }
38 
39  virtual void set_event_handler(event_handler* handler, socket_event_flag retrigger_block = socket_event_flag{}) override;
40 
41 protected:
42  virtual void wakeup(direction::type d) override;
43 };
44 
50 class FZ_PUBLIC_SYMBOL compound_rate_limited_layer final : public socket_layer
51 {
52 public:
54  virtual ~compound_rate_limited_layer();
55 
56  void add_limiter(rate_limiter * limiter);
57  void remove_limiter(rate_limiter * limiter);
58 
59  virtual int read(void* buffer, unsigned int size, int& error) override;
60  virtual int write(void const* buffer, unsigned int size, int& error) override;
61 
62  virtual socket_state get_state() const override {
63  return next_layer_.get_state();
64  }
65 
66  virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) override {
67  return next_layer_.connect(host, port, family);
68  }
69 
70  virtual int shutdown() override {
71  return next_layer_.shutdown();
72  }
73 
74  virtual void set_event_handler(event_handler* handler, fz::socket_event_flag retrigger_block = socket_event_flag{}) override;
75 
76 protected:
77  class crll_bucket;
78  friend class crll_bucket;
79  std::vector<std::unique_ptr<crll_bucket>> buckets_;
80 
81  fz::mutex mtx_{false};
82 };
83 
84 
85 }
86 
87 #endif
fz::buffer
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:25
fz::socket_state
socket_state
State transitions are monotonically increasing.
Definition: socket.hpp:326
fz::compound_rate_limited_layer
A compound rate-limited socket layer.
Definition: rate_limited_layer.hpp:50
fz::rate_limited_layer
A rate-limited socket layer.
Definition: rate_limited_layer.hpp:18
fz::rate_limited_layer::shutdown
virtual int shutdown() override
Signals peers that we want to close the connections.
Definition: rate_limited_layer.hpp:35
fz::mutex
Lean replacement for std::(recursive_)mutex.
Definition: mutex.hpp:27
fz::socket_event_flag::write
@ write
rate_limiter.hpp
Classes for rate-limiting.
fz::socket_event_flag::read
@ read
fz::socket_layer
A base class for socket layers.
Definition: socket.hpp:567
fz::socket_event_flag
socket_event_flag
The type of a socket event.
Definition: socket.hpp:33
fz::compound_rate_limited_layer::shutdown
virtual int shutdown() override
Signals peers that we want to close the connections.
Definition: rate_limited_layer.hpp:70
socket.hpp
Socket classes for networking.
fz::native_string
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:33
fz::rate_limiter
A limiter for the attached buckets.
Definition: rate_limiter.hpp:184
fz::socket_interface
Interface for sockets.
Definition: socket.hpp:358
fz::event_handler
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:54
fz::bucket
A rate-limited token bucket.
Definition: rate_limiter.hpp:251
fz
The namespace used by libfilezilla.
Definition: apply.hpp:17