Debian Codegraph @

contrib/epee/include/net/network_throttle.hpp

raw symbols SCIP
1 /// @file
2 /// @author rfree (current maintainer in monero.cc project)
3 /// @brief interface for throttling of connection (count and rate-limit speed etc)
4
5 // Copyright (c) 2014-2022, The Monero Project
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without modification, are
10 // permitted provided that the following conditions are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright notice, this list of
13 // conditions and the following disclaimer.
14 //
15 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
16 // of conditions and the following disclaimer in the documentation and/or other
17 // materials provided with the distribution.
18 //
19 // 3. Neither the name of the copyright holder nor the names of its contributors may be
20 // used to endorse or promote products derived from this software without specific
21 // prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
24 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 /* rfree: throttle basic interface */
34 /* rfree: also includes the manager for singeton/global such objects */
35
36
37 #ifndef INCLUDED_network_throttle_hpp
38 #define INCLUDED_network_throttle_hpp
39
40 #include <boost/asio.hpp>
41 #include <string>
42 #include <vector>
43 #include <boost/noncopyable.hpp>
44 #include <boost/shared_ptr.hpp>
45
46 #include <boost/array.hpp>
47 #include <boost/enable_shared_from_this.hpp>
48 #include <boost/thread/thread.hpp>
49
50 #include "syncobj.h"
51
52 #include "net/net_utils_base.h"
53 #include "misc_log_ex.h"
54 #include <boost/lambda/bind.hpp>
55 #include <boost/lambda/lambda.hpp>
56 #include <boost/uuid/random_generator.hpp>
57 #include <boost/chrono.hpp>
58 #include <boost/utility/value_init.hpp>
59 #include <boost/asio/deadline_timer.hpp>
60 #include <boost/date_time/posix_time/posix_time.hpp>
61 #include "misc_language.h"
62 #include <sstream>
63 #include <iomanip>
64 #include <algorithm>
65
66 #include <memory>
67 #include <mutex>
68 #include <fstream>
69
70 namespace epee
71 {
72 namespace net_utils
73 {
74
75 // just typedefs to in code define the units used. TODO later it will be enforced that casts to other numericals are only explicit to avoid mistakes? use boost::chrono?
76 typedef double network_speed_kbps; // externally, for parameters and return values, all defined in kilobytes per second
77 typedef double network_speed_bps; // throttle-internally, bytes per second
78 typedef double network_time_seconds;
79 typedef double network_MB;
80
81 class i_network_throttle;
82
83 /***
84 @brief All information about given throttle - speed calculations
85 */
86 struct calculate_times_struct {
87 double average;
88 double window;
89 double delay;
90 double recomendetDataSize;
91 };
92 typedef calculate_times_struct calculate_times_struct;
93
94
95 /***
96 @brief Access to simple throttles, with singlton to access global network limits
97 */
98 class network_throttle_manager {
99 // provides global (singleton) in/inreq/out throttle access
100
101 // [[note1]] see also http://www.nuonsoft.com/blog/2012/10/21/implementing-a-thread-safe-singleton-with-c11/
102 // [[note2]] _inreq is the requested in traffic - we anticipate we will get in-bound traffic soon as result of what we do (e.g. that we sent network downloads requests)
103
104 //protected:
105 public: // XXX
106
107 static boost::mutex m_lock_get_global_throttle_in;
108 static boost::mutex m_lock_get_global_throttle_inreq;
109 static boost::mutex m_lock_get_global_throttle_out;
110
111 friend class connection_basic; // FRIEND - to directly access global throttle-s. !! REMEMBER TO USE LOCKS!
112 friend class connection_basic_pimpl; // ditto
113
114 public:
115 static i_network_throttle & get_global_throttle_in(); ///< singleton ; for friend class ; caller MUST use proper locks! like m_lock_get_global_throttle_in
116 static i_network_throttle & get_global_throttle_inreq(); ///< ditto ; use lock ... use m_lock_get_global_throttle_inreq obviously
117 static i_network_throttle & get_global_throttle_out(); ///< ditto ; use lock ... use m_lock_get_global_throttle_out obviously
118 };
119
120
121
122 /***
123 @brief interface for the throttle, see the derivated class
124 */
125 class i_network_throttle {
126 public:
127 virtual void set_name(const std::string &name)=0;
128 virtual void set_target_speed( network_speed_kbps target )=0;
129 virtual network_speed_kbps get_target_speed()=0;
130
131 virtual void handle_trafic_exact(size_t packet_size) =0; // count the new traffic/packet; the size is exact considering all network costs
132 virtual void handle_trafic_tcp(size_t packet_size) =0; // count the new traffic/packet; the size is as TCP, we will consider MTU etc
133 virtual void tick() =0; // poke and update timers/history
134
135 // time calculations:
136
137 virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const =0; // assuming sending new package (or 0), calculate:
138 // Average, Window, Delay, Recommended data size ; also gets dbg=debug flag, and forced widnow size if >0 or -1 for not forcing window size
139
140 // Average speed, Window size, recommended Delay to sleep now, Recommended size of data to send now
141
142 virtual network_time_seconds get_sleep_time(size_t packet_size) const =0; // gets the D (recommended Delay time) from calc
143 virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size) =0; // ditto, but first tick the timer
144
145 virtual size_t get_recommended_size_of_planned_transport() const =0; // what should be the recommended limit of data size that we can transport over current network_throttle in near future
146
147 virtual double get_time_seconds() const =0; // a timer
148 virtual void logger_handle_net(const std::string &filename, double time, size_t size)=0;
149 virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const =0;
150
151
152 };
153
154
155 // ... more in the -advanced.h file
156
157
158 } // namespace net_utils
159 } // namespace epee
160
161
162 #endif
163
164
165