Electroneum
network_throttle-detail.hpp
Go to the documentation of this file.
1 
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 details, implementing rate limiting */
35 
36 
37 #ifndef INCLUDED_throttle_detail_hpp
38 #define INCLUDED_throttle_detail_hpp
39 
40 #include <boost/circular_buffer.hpp>
41 #include "network_throttle.hpp"
42 
43 namespace epee
44 {
45 namespace net_utils
46 {
47 
48 
50  private:
51  struct packet_info {
52  size_t m_size; // octets sent. Summary for given small-window (e.g. for all packaged in 1 second)
53  packet_info();
54  };
55 
56 
57  network_speed_bps m_target_speed;
58  size_t m_network_add_cost; // estimated add cost of headers
59  size_t m_network_minimal_segment; // estimated minimal cost of sending 1 byte to round up to
60  size_t m_network_max_segment; // recommended max size of 1 TCP transmission
61 
62  const size_t m_window_size; // the number of samples to average over
63  network_time_seconds m_slot_size; // the size of one slot. TODO: now hardcoded for 1 second e.g. in time_to_slot()
64  // TODO for big window size, for performance better the substract on change of m_last_sample_time instead of recalculating average of eg >100 elements
65 
66  boost::circular_buffer< packet_info > m_history; // the history of bw usage
67  network_time_seconds m_last_sample_time; // time of last history[0] - so we know when to rotate the buffer
68  network_time_seconds m_start_time; // when we were created
69  bool m_any_packet_yet; // did we yet got any packet to count
70  uint64_t m_total_packets;
71  uint64_t m_total_bytes;
72 
73  std::string m_name; // my name for debug and logs
74  std::string m_nameshort; // my name for debug and logs (used in log file name)
75 
76  // each sample is now 1 second
77  public:
78  network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1);
79  virtual ~network_throttle();
80  virtual void set_name(const std::string &name);
81  virtual void set_target_speed( network_speed_kbps target );
83 
84  // add information about events:
85  virtual void handle_trafic_exact(size_t packet_size);
86  virtual void handle_trafic_tcp(size_t packet_size);
87 
88  virtual void tick();
89 
90  virtual double get_time_seconds() const ;
91 
92  // time calculations:
93  virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const;
94 
95  virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size);
96  virtual network_time_seconds get_sleep_time(size_t packet_size) const;
97 
98  virtual size_t get_recommended_size_of_planned_transport() const;
99  virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const;
100  virtual double get_current_speed() const;
101  virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const;
102 
103  private:
104  virtual network_time_seconds time_to_slot(network_time_seconds t) const { return std::floor( t ); } // convert exact time eg 13.7 to rounded time for slot number in history 13
105  virtual void _handle_trafic_exact(size_t packet_size, size_t orginal_size);
106  virtual void logger_handle_net(const std::string &filename, double time, size_t size);
107 };
108 
109 /***
110  * The complete set of traffic throttle for one typical connection
111 */
113  public:
117 
118  public:
119  network_throttle_bw(const std::string &name1);
120 };
121 
122 
123 
124 } // namespace net_utils
125 } // namespace epee
126 
127 
128 #endif
129 
130 
virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const
ditto, but for given windows time frame
::std::string string
Definition: gtest-port.h:1097
network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1)
virtual network_speed_kbps get_target_speed()
const char * name
virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const
MAIN LOGIC (see base class for info)
time_t time
Definition: blockchain.cpp:93
virtual size_t get_recommended_size_of_planned_transport() const
what should be the size (bytes) of next data block to be transported
network_throttle m_inreq
for requesting incomming traffic (this is exact usually)
virtual void set_target_speed(network_speed_kbps target)
network_throttle_bw(const std::string &name1)
interface for throttling of connection (count and rate-limit speed etc)
virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const
virtual void handle_trafic_tcp(size_t packet_size)
count the new traffic/packet; the size is as TCP, we will consider MTU etc
unsigned __int64 uint64_t
Definition: stdint.h:136
virtual void handle_trafic_exact(size_t packet_size)
count the new traffic/packet; the size is exact considering all network costs
network_throttle m_out
for outgoing traffic that we just sent (this is exact usually)
virtual void tick()
poke and update timers/history (recalculates, moves the history if needed, checks the real clock etc)...
virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size)
increase the timer if needed, and get the package size
virtual network_time_seconds get_sleep_time(size_t packet_size) const
gets the Delay (recommended Delay time) from calc. (not safe: only if time didnt change?) TODO
virtual double get_time_seconds() const
timer that we use, time in seconds, monotionic
network_throttle m_in
for incomming traffic (this we can not controll directly as it depends of what others send to us - us...
virtual void set_name(const std::string &name)