Electroneum
Loading...
Searching...
No Matches
connection_basic.cpp
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: implementation for the non-template base, can be used by connection<> template class in abstract_tcp_server2 file */
35
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
64namespace epee
65{
66namespace net_utils
67{
68
69namespace
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
78 std::string to_string(t_connection_type type)
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:
95 connection_basic_pimpl(const std::string &name);
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
113namespace epee
114{
115namespace net_utils
116{
117
118// ================================================================================================
119// connection_basic_pimpl
120// ================================================================================================
121
123
124// ================================================================================================
125// connection_basic
126// ================================================================================================
127
128// static variables:
130
131// methods:
132connection_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())),
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
156connection_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())),
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
193
206
215
224
227
231
235
236void 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
266void 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
271void 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
276void connection_basic::logger_handle_net_read(size_t size) { // network data read
277}
278
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
288void connection_basic::set_save_graph(bool save_graph) {
289}
290
291
292} // namespace
293} // namespace
294
connection_basic_pimpl(const std::string &name)
boost::asio::io_service::strand strand_
Strand to ensure the connection's handlers are not called concurrently.
virtual ~connection_basic() noexcept(false)
static void set_rate_down_limit(uint64_t limit)
connection_basic(boost::asio::ip::tcp::socket &&socket, boost::shared_ptr< connection_basic_shared_state > state, ssl_support_t ssl_support)
void sleep_before_packet(size_t packet_size, int phase, int q_len)
void do_send_handler_write_from_queue(const boost::system::error_code &e, size_t cb, int q_len)
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > socket_
Socket for the connection.
static void save_limit_to_file(int limit)
for dr-electroneum
static double get_sleep_time(size_t cb)
static void set_save_graph(bool save_graph)
boost::asio::ip::tcp::socket & socket()
std::unique_ptr< connection_basic_pimpl > mI
static void set_rate_up_limit(uint64_t limit)
void do_send_handler_write(const void *ptr, size_t cb)
virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size)=0
virtual network_speed_kbps get_target_speed()=0
virtual void set_target_speed(network_speed_kbps target)=0
virtual network_time_seconds get_sleep_time(size_t packet_size) const =0
virtual void handle_trafic_exact(size_t packet_size)=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
base for connection, contains e.g. the ratelimit hooks
void get(std::istream &input, bool &res)
Definition io.h:62
#define _note(x)
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
#define MTRACE(x)
Definition misc_log_ex.h:77
#define _dbg2(x)
std::string to_string(t_connection_type type)
STL namespace.
#define GET_IO_SERVICE(s)
implementaion for throttling of connection (count and rate-limit speed etc)
#define false
unsigned __int64 uint64_t
Definition stdint.h:136
#define CRITICAL_REGION_LOCAL(x)
Definition syncobj.h:228