Electroneum
connection_basic.cpp
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: implementation for the non-template base, can be used by connection<> template class in abstract_tcp_server2 file */
35 
36 #include "net/connection_basic.hpp"
37 
38 #include "net/net_utils_base.h"
39 #include "misc_log_ex.h"
40 #include <boost/date_time/posix_time/posix_time.hpp>
41 #include <boost/thread/thread.hpp>
42 #include "misc_language.h"
43 #include "pragma_comp_defs.h"
44 #include <iomanip>
45 
46 #include <boost/asio/basic_socket.hpp>
47 
48 // TODO:
50 
51 #if BOOST_VERSION >= 107000
52 #define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
53 #else
54 #define GET_IO_SERVICE(s) ((s).get_io_service())
55 #endif
56 
57 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
58 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net.conn"
59 
60 // ################################################################################################
61 // local (TU local) headers
62 // ################################################################################################
63 
64 namespace epee
65 {
66 namespace net_utils
67 {
68 
69 namespace
70 {
71  boost::asio::ssl::context& get_context(connection_basic_shared_state* state)
72  {
73  CHECK_AND_ASSERT_THROW_MES(state != nullptr, "state shared_ptr cannot be null");
74  return state->ssl_context;
75  }
76 }
77 
79  {
80  if (type == e_connection_type_NET)
81  return std::string("NET");
82  else if (type == e_connection_type_RPC)
83  return std::string("RPC");
84  else if (type == e_connection_type_P2P)
85  return std::string("P2P");
86 
87  return std::string("UNKNOWN");
88  }
89 
90 
91 /* ============================================================================ */
92 
94  public:
96 
97  static int m_default_tos;
98 
101 
102  int m_peer_number; // e.g. for debug/stats
103 };
104 
105 
106 } // namespace
107 } // namespace
108 
109 // ################################################################################################
110 // The implementation part
111 // ################################################################################################
112 
113 namespace epee
114 {
115 namespace net_utils
116 {
117 
118 // ================================================================================================
119 // connection_basic_pimpl
120 // ================================================================================================
121 
122 connection_basic_pimpl::connection_basic_pimpl(const std::string &name) : m_throttle(name), m_peer_number(0) { }
123 
124 // ================================================================================================
125 // connection_basic
126 // ================================================================================================
127 
128 // static variables:
130 
131 // methods:
132 connection_basic::connection_basic(boost::asio::ip::tcp::socket&& sock, boost::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support)
133  :
134  m_state(std::move(state)),
135  mI( new connection_basic_pimpl("peer") ),
136  strand_(GET_IO_SERVICE(sock)),
137  socket_(GET_IO_SERVICE(sock), get_context(m_state.get())),
138  m_want_close_connection(false),
139  m_was_shutdown(false),
140  m_ssl_support(ssl_support)
141 {
142  // add nullptr checks if removed
143  assert(m_state != nullptr); // release runtime check in get_context
144 
145  socket_.next_layer() = std::move(sock);
146 
147  ++(m_state->sock_count); // increase the global counter
148  mI->m_peer_number = m_state->sock_number.fetch_add(1); // use, and increase the generated number
149 
150  std::string remote_addr_str = "?";
151  try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ;
152 
153  _note("Spawned connection #"<<mI->m_peer_number<<" to " << remote_addr_str << " currently we have sockets count:" << m_state->sock_count);
154 }
155 
156 connection_basic::connection_basic(boost::asio::io_service &io_service, boost::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support)
157  :
158  m_state(std::move(state)),
159  mI( new connection_basic_pimpl("peer") ),
160  strand_(io_service),
161  socket_(io_service, get_context(m_state.get())),
162  m_want_close_connection(false),
163  m_was_shutdown(false),
164  m_ssl_support(ssl_support)
165 {
166  // add nullptr checks if removed
167  assert(m_state != nullptr); // release runtime check in get_context
168 
169  ++(m_state->sock_count); // increase the global counter
170  mI->m_peer_number = m_state->sock_number.fetch_add(1); // use, and increase the generated number
171 
172  std::string remote_addr_str = "?";
173  try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ;
174 
175  _note("Spawned connection #"<<mI->m_peer_number<<" to " << remote_addr_str << " currently we have sockets count:" << m_state->sock_count);
176 }
177 
179  --(m_state->sock_count);
180 
181  std::string remote_addr_str = "?";
182  try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ;
183  _note("Destructing connection #"<<mI->m_peer_number << " to " << remote_addr_str);
184 }
185 
187  {
190  }
191  save_limit_to_file(limit);
192 }
193 
195  {
198  }
199 
200  {
203  }
204  save_limit_to_file(limit);
205 }
206 
208  uint64_t limit;
209  {
212  }
213  return limit;
214 }
215 
217  uint64_t limit;
218  {
221  }
222  return limit;
223 }
224 
226 }
227 
230 }
231 
234 }
235 
236 void connection_basic::sleep_before_packet(size_t packet_size, int phase, int q_len) {
237  double delay=0; // will be calculated
238  do
239  { // rate limiting
240  if (m_was_shutdown) {
241  _dbg2("m_was_shutdown - so abort sleep");
242  return;
243  }
244 
245  {
248  }
249 
250  delay *= 0.50;
251  if (delay > 0) {
252  long int ms = (long int)(delay * 1000);
253  MTRACE("Sleeping in " << __FUNCTION__ << " for " << ms << " ms before packet_size="<<packet_size); // debug sleep
254  boost::this_thread::sleep(boost::posix_time::milliseconds( ms ) );
255  }
256  } while(delay > 0);
257 
258 // XXX LATER XXX
259  {
261  network_throttle_manager::get_global_throttle_out().handle_trafic_exact( packet_size ); // increase counter - global
262  }
263 
264 }
265 
266 void connection_basic::do_send_handler_write(const void* ptr , size_t cb ) {
267  // No sleeping here; sleeping is done once and for all in connection<t_protocol_handler>::handle_write
268  MTRACE("handler_write (direct) - before ASIO write, for packet="<<cb<<" B (after sleep)");
269 }
270 
271 void connection_basic::do_send_handler_write_from_queue( const boost::system::error_code& e, size_t cb, int q_len ) {
272  // No sleeping here; sleeping is done once and for all in connection<t_protocol_handler>::handle_write
273  MTRACE("handler_write (after write, from queue="<<q_len<<") - before ASIO write, for packet="<<cb<<" B (after sleep)");
274 }
275 
276 void connection_basic::logger_handle_net_read(size_t size) { // network data read
277 }
278 
280 }
281 
283  CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::network_throttle_manager::m_lock_get_global_throttle_out);
285  return t;
286 }
287 
288 void connection_basic::set_save_graph(bool save_graph) {
289 }
290 
291 
292 } // namespace
293 } // namespace
294 
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
#define MTRACE(x)
Definition: misc_log_ex.h:77
::std::string string
Definition: gtest-port.h:1097
std::unique_ptr< connection_basic_pimpl > mI
static i_network_throttle & get_global_throttle_in()
singleton ; for friend class ; caller MUST use proper locks! like m_lock_get_global_throttle_in ...
#define _dbg2(x)
Definition: misc_log_ex.h:106
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:101
STL namespace.
virtual network_speed_kbps get_target_speed()=0
const char * name
void sleep_before_packet(size_t packet_size, int phase, int q_len)
void do_send_handler_write(const void *ptr, size_t cb)
static void set_save_graph(bool save_graph)
static void save_limit_to_file(int limit)
for dr-electroneum
boost::asio::ip::tcp::socket & socket()
static void set_rate_up_limit(uint64_t limit)
void do_send_handler_write_from_queue(const boost::system::error_code &e, size_t cb, int q_len)
virtual network_time_seconds get_sleep_time(size_t packet_size) const =0
unsigned __int64 uint64_t
Definition: stdint.h:136
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:228
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:98
#define _note(x)
Definition: misc_log_ex.h:109
virtual void set_target_speed(network_speed_kbps target)=0
connection_basic(boost::asio::ip::tcp::socket &&socket, boost::shared_ptr< connection_basic_shared_state > state, ssl_support_t ssl_support)
#define false
Definition: stdbool.h:38
static i_network_throttle & get_global_throttle_inreq()
ditto ; use lock ... use m_lock_get_global_throttle_inreq obviously
virtual ~connection_basic() noexcept(false)
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > socket_
Socket for the connection.
const T & move(const T &t)
Definition: gtest-port.h:1317
Definition: blake256.h:37
static void set_rate_down_limit(uint64_t limit)
connection_basic_pimpl(const std::string &name)
#define GET_IO_SERVICE(s)
static i_network_throttle & get_global_throttle_out()
ditto ; use lock ... use m_lock_get_global_throttle_out obviously
std::string to_string(t_connection_type type)
implementaion for throttling of connection (count and rate-limit speed etc)
virtual void handle_trafic_exact(size_t packet_size)=0
void get(std::istream &input, bool &res)
Definition: io.h:62
static double get_sleep_time(size_t cb)
base for connection, contains e.g. the ratelimit hooks
virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size)=0