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-2021, The Electroneum Project
12 // Copyrights(c) 2014-2019, 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 <string>
47 #include <atomic>
48 #include <memory>
49 
50 #include <boost/asio.hpp>
51 #include <boost/asio/ssl.hpp>
52 
53 #include "net/net_utils_base.h"
54 #include "net/net_ssl.h"
55 #include "syncobj.h"
56 
57 namespace epee
58 {
59 namespace net_utils
60 {
61 
63  {
64  ssl_options_t ssl_options_;
65  public:
67  std::atomic<long> sock_count;
68  std::atomic<long> sock_number;
69 
71  : ssl_options_(ssl_support_t::e_ssl_support_disabled),
72  ssl_context(boost::asio::ssl::context::tlsv12),
73  sock_count(0),
74  sock_number(0)
75  {}
76 
78  {
79  ssl_options_ = std::move(src);
80  ssl_context = ssl_options_.create_context();
81  }
82 
83  const ssl_options_t& ssl_options() const noexcept { return ssl_options_; }
84  };
85 
86  /************************************************************************/
87  /* */
88  /************************************************************************/
90 
91 class connection_basic_pimpl; // PIMPL for this class
92 
93  enum t_connection_type { // type of the connection (of this server), e.g. so that we will know how to limit it
94  e_connection_type_NET = 0, // default (not used?)
95  e_connection_type_RPC = 1, // the rpc commands (probably not rate limited, not chunked, etc)
96  e_connection_type_P2P = 2 // to other p2p node (probably limited)
97  };
98 
100 
101 class connection_basic { // not-templated base class for rapid developmet of some code parts
102  // beware of removing const, net_utils::connection is sketchily doing a cast to prevent storing ptr twice
103  const boost::shared_ptr<connection_basic_shared_state> m_state;
104  public:
105 
106  std::unique_ptr< connection_basic_pimpl > mI; // my Implementation
107 
108  // moved here from orginal connecton<> - common member variables that do not depend on template in connection<>
110  std::atomic<bool> m_was_shutdown;
112  std::list<std::string> m_send_que;
113  volatile bool m_is_multithreaded;
115  boost::asio::io_service::strand strand_;
117  boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
119 
120  public:
121  // first counter is the ++/-- count of current sockets, the other socket_number is only-increasing ++ number generator
122  connection_basic(boost::asio::ip::tcp::socket&& socket, boost::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support);
123  connection_basic(boost::asio::io_service &io_service, boost::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support);
124 
125  virtual ~connection_basic() noexcept(false);
126 
128  connection_basic_shared_state& get_state() noexcept { return *m_state; /* verified in constructor */ }
129  connection_basic(boost::asio::io_service& io_service, std::atomic<long> &ref_sock_count, std::atomic<long> &sock_number, ssl_support_t ssl);
130 
131  boost::asio::ip::tcp::socket& socket() { return socket_.next_layer(); }
134 
135  bool handshake(boost::asio::ssl::stream_base::handshake_type type)
136  {
137  //m_state != nullptr verified in constructor
138  return m_state->ssl_options().handshake(socket_, type);
139  }
140 
141  template<typename MutableBufferSequence, typename ReadHandler>
142  void async_read_some(const MutableBufferSequence &buffers, ReadHandler &&handler)
143  {
145  socket_.async_read_some(buffers, std::forward<ReadHandler>(handler));
146  else
147  socket().async_read_some(buffers, std::forward<ReadHandler>(handler));
148  }
149 
150  template<typename ConstBufferSequence, typename WriteHandler>
151  void async_write_some(const ConstBufferSequence &buffers, WriteHandler &&handler)
152  {
154  socket_.async_write_some(buffers, std::forward<WriteHandler>(handler));
155  else
156  socket().async_write_some(buffers, std::forward<WriteHandler>(handler));
157  }
158 
159  template<typename ConstBufferSequence, typename WriteHandler>
160  void async_write(const ConstBufferSequence &buffers, WriteHandler &&handler)
161  {
163  boost::asio::async_write(socket_, buffers, std::forward<WriteHandler>(handler));
164  else
165  boost::asio::async_write(socket(), buffers, std::forward<WriteHandler>(handler));
166  }
167 
168  // various handlers to be called from connection class:
169  void do_send_handler_write(const void * ptr , size_t cb);
170  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
171 
172  void logger_handle_net_write(size_t size); // network data written
173  void logger_handle_net_read(size_t size); // network data read
174 
175  // config for rate limit
176 
177  static void set_rate_up_limit(uint64_t limit);
178  static void set_rate_down_limit(uint64_t limit);
179  static uint64_t get_rate_up_limit();
180  static uint64_t get_rate_down_limit();
181 
182  // config misc
183  static void set_tos_flag(int tos); // ToS / QoS flag
184  static int get_tos_flag();
185 
186  // handlers and sleep
187  void sleep_before_packet(size_t packet_size, int phase, int q_len); // execute a sleep ; phase is not really used now(?)
188  static void save_limit_to_file(int limit);
189  static double get_sleep_time(size_t cb);
190 
191  static void set_save_graph(bool save_graph);
192 };
193 
194 } // nameserver
195 } // nameserver
196 
197 #endif
198 
199 
const ssl_options_t & ssl_options() const noexcept
::std::string string
Definition: gtest-port.h:1097
std::unique_ptr< connection_basic_pimpl > mI
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:101
connection_basic_shared_state & get_state() noexcept
void sleep_before_packet(size_t packet_size, int phase, int q_len)
void do_send_handler_write(const void *ptr, size_t cb)
void async_write(const ConstBufferSequence &buffers, WriteHandler &&handler)
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()
unsigned int uint32_t
Definition: stdint.h:126
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)
unsigned __int64 uint64_t
Definition: stdint.h:136
bool handshake(boost::asio::ssl::stream_base::handshake_type type)
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:98
std::list< std::string > m_send_que
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
boost::asio::io_service::strand strand_
Strand to ensure the connection&#39;s handlers are not called concurrently.
void async_write_some(const ConstBufferSequence &buffers, WriteHandler &&handler)
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)
void async_read_some(const MutableBufferSequence &buffers, ReadHandler &&handler)
std::string to_string(t_connection_type type)
boost::asio::ssl::context create_context() const
Definition: net_ssl.cpp:283
static double get_sleep_time(size_t cb)