libfilezilla
Loading...
Searching...
No Matches
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
7
8#include "rate_limiter.hpp"
9#include "socket.hpp"
10
11namespace fz {
12
18class FZ_PUBLIC_SYMBOL rate_limited_layer final : public socket_layer, private bucket
19{
20public:
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 void set_event_handler(event_handler* handler, socket_event_flag retrigger_block = socket_event_flag{}) override;
28
29protected:
30 virtual void wakeup(direction::type d) override;
31};
32
38class FZ_PUBLIC_SYMBOL compound_rate_limited_layer final : public socket_layer
39{
40public:
41 compound_rate_limited_layer(event_handler* handler, socket_interface& next_layer);
42 virtual ~compound_rate_limited_layer();
43
44 void add_limiter(rate_limiter * limiter);
45 void remove_limiter(rate_limiter * limiter);
46
47 virtual int read(void* buffer, unsigned int size, int& error) override;
48 virtual int write(void const* buffer, unsigned int size, int& error) override;
49
50 virtual socket_state get_state() const override {
51 return next_layer_.get_state();
52 }
53
54 virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) override {
55 return next_layer_.connect(host, port, family);
56 }
57
58 virtual int shutdown() override {
59 return next_layer_.shutdown();
60 }
61
62 virtual void set_event_handler(event_handler* handler, fz::socket_event_flag retrigger_block = socket_event_flag{}) override;
63
64protected:
65 class crll_bucket;
66 friend class crll_bucket;
67 std::vector<std::unique_ptr<crll_bucket>> buckets_;
68
69 fz::mutex mtx_{false};
70};
71
72
73}
74
75#endif
A rate-limited token bucket.
Definition rate_limiter.hpp:253
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:27
virtual void set_event_handler(event_handler *handler, fz::socket_event_flag retrigger_block=socket_event_flag{}) override
The handler for any events generated (or forwarded) by this layer.
virtual int shutdown() override
Signals peers that we want to close the connections.
Definition rate_limited_layer.hpp:58
Simple handler for asynchronous event processing.
Definition event_handler.hpp:55
Lean replacement for std::(recursive_)mutex.
Definition mutex.hpp:52
virtual void wakeup(direction::type d) override
Called in response to unlock_tree if tokens have become available.
virtual void set_event_handler(event_handler *handler, socket_event_flag retrigger_block=socket_event_flag{}) override
The handler for any events generated (or forwarded) by this layer.
A limiter for the attached buckets.
Definition rate_limiter.hpp:185
Interface for sockets.
Definition socket.hpp:375
The namespace used by libfilezilla.
Definition apply.hpp:17
@ error
Operationf failed.
Definition aio.hpp:199
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition string.hpp:36
socket_state
State transitions are monotonically increasing.
Definition socket.hpp:343
socket_event_flag
The type of a socket event.
Definition socket.hpp:35
Classes for rate-limiting.
Socket classes for networking.