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