Electroneum
connection_basic.hpp
Go to the documentation of this file.
1 
5 // ! This file might contain variable names same as in template class connection<>
6 // ! from files contrib/epee/include/net/abstract_tcp_server2.*
7 // ! I am not a lawyer; afaik APIs, var names etc are not copyrightable ;)
8 // ! (how ever if in some wonderful juristdictions that is not the case, then why not make another sub-class withat that members and licence it as epee part)
9 // ! Working on above premise, IF this is valid in your juristdictions, then consider this code as released as:
10 
11 // Copyrights(c) 2017-2019, The Electroneum Project
12 // Copyrights(c) 2014-2017, The Monero Project
13 //
14 // All rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without modification, are
17 // permitted provided that the following conditions are met:
18 //
19 // 1. Redistributions of source code must retain the above copyright notice, this list of
20 // conditions and the following disclaimer.
21 //
22 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
23 // of conditions and the following disclaimer in the documentation and/or other
24 // materials provided with the distribution.
25 //
26 // 3. Neither the name of the copyright holder nor the names of its contributors may be
27 // used to endorse or promote products derived from this software without specific
28 // prior written permission.
29 //
30 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
31 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
38 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 /* rfree: place for hanlers for the non-template base, can be used by connection<> template class in abstract_tcp_server2 file */
41 
42 #ifndef INCLUDED_p2p_connection_basic_hpp
43 #define INCLUDED_p2p_connection_basic_hpp
44 
45 
46 #include <boost/asio.hpp>
47 #include <string>
48 #include <vector>
49 #include <boost/noncopyable.hpp>
50 #include <boost/shared_ptr.hpp>
51 #include <atomic>
52 
53 #include <boost/asio.hpp>
54 #include <boost/array.hpp>
55 #include <boost/noncopyable.hpp>
56 #include <boost/shared_ptr.hpp>
57 #include <boost/enable_shared_from_this.hpp>
58 #include <boost/interprocess/detail/atomic.hpp>
59 #include <boost/thread/thread.hpp>
60 
61 #include <memory>
62 
63 #include "net/net_utils_base.h"
64 #include "syncobj.h"
65 
66 namespace epee
67 {
68 namespace net_utils
69 {
70 
71  /************************************************************************/
72  /* */
73  /************************************************************************/
75 
76 class connection_basic_pimpl; // PIMPL for this class
77 
78  enum t_connection_type { // type of the connection (of this server), e.g. so that we will know how to limit it
79  e_connection_type_NET = 0, // default (not used?)
80  e_connection_type_RPC = 1, // the rpc commands (probably not rate limited, not chunked, etc)
81  e_connection_type_P2P = 2 // to other p2p node (probably limited)
82  };
83 
84  std::string to_string(t_connection_type type);
85 
86 class connection_basic { // not-templated base class for rapid developmet of some code parts
87  public:
88  std::unique_ptr< connection_basic_pimpl > mI; // my Implementation
89 
90  // moved here from orginal connecton<> - common member variables that do not depend on template in connection<>
91  volatile uint32_t m_want_close_connection;
92  std::atomic<bool> m_was_shutdown;
93  critical_section m_send_que_lock;
94  std::list<std::string> m_send_que;
95  volatile bool m_is_multithreaded;
96  double m_start_time;
98  boost::asio::io_service::strand strand_;
100  boost::asio::ip::tcp::socket socket_;
101 
102  std::atomic<long> &m_ref_sock_count; // reference to external counter of existing sockets that we will ++/--
103  public:
104  // first counter is the ++/-- count of current sockets, the other socket_number is only-increasing ++ number generator
105  connection_basic(boost::asio::io_service& io_service, std::atomic<long> &ref_sock_count, std::atomic<long> &sock_number);
106 
107  virtual ~connection_basic() noexcept(false);
108 
109  // various handlers to be called from connection class:
110  void do_send_handler_write(const void * ptr , size_t cb);
111  void do_send_handler_write_from_queue(const boost::system::error_code& e, size_t cb , int q_len); // from handle_write, sending next part
112 
113  void logger_handle_net_write(size_t size); // network data written
114  void logger_handle_net_read(size_t size); // network data read
115 
116  void set_start_time();
117 
118  // config for rate limit
119 
120  static void set_rate_up_limit(uint64_t limit);
121  static void set_rate_down_limit(uint64_t limit);
122  static uint64_t get_rate_up_limit();
123  static uint64_t get_rate_down_limit();
124 
125  // config misc
126  static void set_tos_flag(int tos); // ToS / QoS flag
127  static int get_tos_flag();
128 
129  // handlers and sleep
130  void sleep_before_packet(size_t packet_size, int phase, int q_len); // execute a sleep ; phase is not really used now(?)
131  static void save_limit_to_file(int limit);
132  static double get_sleep_time(size_t cb);
133 
134  static void set_save_graph(bool save_graph);
135 };
136 
137 } // nameserver
138 } // nameserver
139 
140 #endif
141 
142 
double m_start_time
Definition: connection_basic.hpp:96
void logger_handle_net_write(size_t size)
Definition: connection_basic.cpp:281
static uint64_t get_rate_down_limit()
Definition: connection_basic.cpp:211
Definition: unordered_containers_boost_serialization.h:38
critical_section m_send_que_lock
Definition: connection_basic.hpp:93
std::unique_ptr< connection_basic_pimpl > mI
Definition: connection_basic.hpp:88
static void set_tos_flag(int tos)
Definition: connection_basic.cpp:223
boost::asio::ip::tcp::socket socket_
Socket for the connection.
Definition: connection_basic.hpp:100
void set_start_time()
Definition: connection_basic.cpp:260
void sleep_before_packet(size_t packet_size, int phase, int q_len)
Definition: connection_basic.cpp:231
std::atomic< long > & m_ref_sock_count
Definition: connection_basic.hpp:102
void do_send_handler_write(const void *ptr, size_t cb)
Definition: connection_basic.cpp:265
static void set_save_graph(bool save_graph)
Definition: connection_basic.cpp:290
static void save_limit_to_file(int limit)
for dr-electroneum
Definition: connection_basic.cpp:220
volatile uint32_t m_want_close_connection
Definition: connection_basic.hpp:91
static void set_rate_up_limit(uint64_t limit)
Definition: connection_basic.cpp:176
void do_send_handler_write_from_queue(const boost::system::error_code &e, size_t cb, int q_len)
Definition: connection_basic.cpp:271
volatile bool m_is_multithreaded
Definition: connection_basic.hpp:95
std::list< std::string > m_send_que
Definition: connection_basic.hpp:94
Definition: connection_basic.hpp:81
type
Definition: json.h:74
#define false
Definition: stdbool.h:38
void logger_handle_net_read(size_t size)
Definition: connection_basic.cpp:278
boost::asio::io_service::strand strand_
Strand to ensure the connection&#39;s handlers are not called concurrently.
Definition: connection_basic.hpp:98
static int get_tos_flag()
Definition: connection_basic.cpp:227
virtual ~connection_basic() noexcept(false)
Definition: connection_basic.cpp:169
Definition: connection_basic.cpp:91
connection_basic(boost::asio::io_service &io_service, std::atomic< long > &ref_sock_count, std::atomic< long > &sock_number)
Definition: connection_basic.cpp:150
Definition: connection_basic.hpp:80
static void set_rate_down_limit(uint64_t limit)
Definition: connection_basic.cpp:189
t_connection_type
Definition: connection_basic.hpp:78
std::string to_string(t_connection_type type)
Definition: connection_basic.cpp:96
Definition: connection_basic.hpp:79
std::atomic< bool > m_was_shutdown
Definition: connection_basic.hpp:92
static double get_sleep_time(size_t cb)
Definition: connection_basic.cpp:284
static uint64_t get_rate_up_limit()
Definition: connection_basic.cpp:202
Definition: connection_basic.hpp:86