Electroneum
epee::net_utils::blocked_mode_client Class Reference

#include <net_helper.h>

Inheritance diagram for epee::net_utils::blocked_mode_client:
Collaboration diagram for epee::net_utils::blocked_mode_client:

Public Types

using connect_func = boost::unique_future< boost::asio::ip::tcp::socket >(const std::string &, const std::string &, boost::asio::steady_timer &)
 

Public Member Functions

 blocked_mode_client ()
 
 ~blocked_mode_client ()
 
void set_ssl (ssl_options_t ssl_options)
 
bool connect (const std::string &addr, int port, std::chrono::milliseconds timeout)
 
try_connect_result_t try_connect (const std::string &addr, const std::string &port, std::chrono::milliseconds timeout, epee::net_utils::ssl_support_t ssl_support)
 
bool connect (const std::string &addr, const std::string &port, std::chrono::milliseconds timeout)
 
void set_connector (std::function< connect_func > connector)
 Change the connection routine (proxy, etc.) More...
 
bool disconnect ()
 
bool send (const std::string &buff, std::chrono::milliseconds timeout)
 
bool send (const void *data, size_t sz)
 
bool is_connected (bool *ssl=NULL)
 
bool recv (std::string &buff, std::chrono::milliseconds timeout)
 
bool recv_n (std::string &buff, int64_t sz, std::chrono::milliseconds timeout)
 
bool shutdown ()
 
boost::asio::io_service & get_io_service ()
 
boost::asio::ip::tcp::socket & get_socket ()
 
uint64_t get_bytes_sent () const
 
uint64_t get_bytes_received () const
 

Protected Member Functions

bool write (const void *data, size_t sz, boost::system::error_code &ec)
 
void async_write (const void *data, size_t sz, boost::system::error_code &ec)
 
void async_read (char *buff, size_t sz, boost::asio::detail::transfer_at_least_t transfer_at_least, handler_obj &hndlr)
 

Protected Attributes

boost::asio::io_service m_io_service
 
boost::asio::ssl::context m_ctx
 
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
 
std::function< connect_funcm_connector
 
ssl_options_t m_ssl_options
 
bool m_initialized
 
bool m_connected
 
boost::asio::steady_timer m_deadline
 
volatile uint32_t m_shutdowned
 
std::atomic< uint64_tm_bytes_sent
 
std::atomic< uint64_tm_bytes_received
 

Detailed Description

Definition at line 70 of file net_helper.h.

Member Typedef Documentation

◆ connect_func

using epee::net_utils::blocked_mode_client::connect_func = boost::unique_future<boost::asio::ip::tcp::socket>(const std::string&, const std::string&, boost::asio::steady_timer&)

The first/second parameters are host/port respectively. The third parameter is for setting the timeout callback - the timer is already set by the caller, the callee only needs to set the behavior.

Additional asynchronous operations should be queued using the io_service from the timer. The implementation should assume multi-threaded I/O processing.

If the callee cannot start an asynchronous operation, an exception should be thrown to signal an immediate failure.

The return value is a future to a connected socket. Asynchronous failures should use the set_exception method.

Definition at line 131 of file net_helper.h.

Constructor & Destructor Documentation

◆ blocked_mode_client()

epee::net_utils::blocked_mode_client::blocked_mode_client ( )
inline

Definition at line 102 of file net_helper.h.

102  :
103  m_io_service(),
104  m_ctx(boost::asio::ssl::context::tlsv12),
105  m_connector(direct_connect{}),
106  m_ssl_socket(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(m_io_service, m_ctx)),
108  m_initialized(true),
109  m_connected(false),
111  m_shutdowned(0),
112  m_bytes_sent(0),
114  {
115  }
boost::asio::ssl::context m_ctx
Definition: net_helper.h:678
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679
boost::asio::io_service m_io_service
Definition: net_helper.h:677
boost::asio::steady_timer m_deadline
Definition: net_helper.h:684
std::function< connect_func > m_connector
Definition: net_helper.h:680
std::atomic< uint64_t > m_bytes_received
Definition: net_helper.h:687
std::atomic< uint64_t > m_bytes_sent
Definition: net_helper.h:686

◆ ~blocked_mode_client()

epee::net_utils::blocked_mode_client::~blocked_mode_client ( )
inline

Definition at line 134 of file net_helper.h.

135  {
136  //profile_tools::local_coast lc("~blocked_mode_client()", 3);
137  try { shutdown(); }
138  catch(...) { /* ignore */ }
139  }
Here is the call graph for this function:

Member Function Documentation

◆ async_read()

void epee::net_utils::blocked_mode_client::async_read ( char *  buff,
size_t  sz,
boost::asio::detail::transfer_at_least_t  transfer_at_least,
handler_obj &  hndlr 
)
inlineprotected

Definition at line 667 of file net_helper.h.

668  {
670  boost::asio::async_read(m_ssl_socket->next_layer(), boost::asio::buffer(buff, sz), transfer_at_least, hndlr);
671  else
672  boost::asio::async_read(*m_ssl_socket, boost::asio::buffer(buff, sz), transfer_at_least, hndlr);
673 
674  }
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679
Here is the caller graph for this function:

◆ async_write()

void epee::net_utils::blocked_mode_client::async_write ( const void *  data,
size_t  sz,
boost::system::error_code &  ec 
)
inlineprotected

Definition at line 659 of file net_helper.h.

660  {
662  boost::asio::async_write(*m_ssl_socket, boost::asio::buffer(data, sz), boost::lambda::var(ec) = boost::lambda::_1);
663  else
664  boost::asio::async_write(m_ssl_socket->next_layer(), boost::asio::buffer(data, sz), boost::lambda::var(ec) = boost::lambda::_1);
665  }
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679
Here is the caller graph for this function:

◆ connect() [1/2]

bool epee::net_utils::blocked_mode_client::connect ( const std::string &  addr,
int  port,
std::chrono::milliseconds  timeout 
)
inline

Definition at line 151 of file net_helper.h.

152  {
153  return connect(addr, std::to_string(port), timeout);
154  }
bool connect(const std::string &addr, int port, std::chrono::milliseconds timeout)
Definition: net_helper.h:151
boost::endian::big_uint16_t port
Definition: socks.cpp:60
std::string to_string(t_connection_type type)
Here is the call graph for this function:

◆ connect() [2/2]

bool epee::net_utils::blocked_mode_client::connect ( const std::string &  addr,
const std::string &  port,
std::chrono::milliseconds  timeout 
)
inline

Definition at line 207 of file net_helper.h.

208  {
209  m_connected = false;
210  try
211  {
212  m_ssl_socket->next_layer().close();
213 
214  // Set SSL options
215  // disable sslv2
216  m_ssl_socket.reset(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(m_io_service, m_ctx));
217 
218  // Get a list of endpoints corresponding to the server name.
219 
220  try_connect_result_t try_connect_result = try_connect(addr, port, timeout, m_ssl_options.support);
221  if (try_connect_result == CONNECT_FAILURE)
222  return false;
224  {
225  if (try_connect_result == CONNECT_NO_SSL)
226  {
227  MERROR("SSL handshake failed on an autodetect connection, reconnecting without SSL");
229  if (try_connect(addr, port, timeout, m_ssl_options.support) != CONNECT_SUCCESS)
230  return false;
231  }
232  }
233  }
234  catch(const boost::system::system_error& er)
235  {
236  MDEBUG("Some problems at connect, message: " << er.what());
237  return false;
238  }
239  catch(...)
240  {
241  MDEBUG("Some fatal problems.");
242  return false;
243  }
244 
245  return true;
246  }
#define MERROR(x)
Definition: misc_log_ex.h:73
boost::asio::ssl::context m_ctx
Definition: net_helper.h:678
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679
#define MDEBUG(x)
Definition: misc_log_ex.h:76
boost::asio::io_service m_io_service
Definition: net_helper.h:677
try_connect_result_t try_connect(const std::string &addr, const std::string &port, std::chrono::milliseconds timeout, epee::net_utils::ssl_support_t ssl_support)
Definition: net_helper.h:157
boost::endian::big_uint16_t port
Definition: socks.cpp:60
Here is the call graph for this function:

◆ disconnect()

bool epee::net_utils::blocked_mode_client::disconnect ( )
inline

Definition at line 254 of file net_helper.h.

255  {
256  try
257  {
258  if(m_connected)
259  {
260  m_connected = false;
261  if(m_ssl_options)
262  shutdown_ssl();
263  m_ssl_socket->next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
264  }
265  }
266  catch(const boost::system::system_error& /*er*/)
267  {
268  //LOG_ERROR("Some problems at disconnect, message: " << er.what());
269  return false;
270  }
271  catch(...)
272  {
273  //LOG_ERROR("Some fatal problems.");
274  return false;
275  }
276  return true;
277  }
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679

◆ get_bytes_received()

uint64_t epee::net_utils::blocked_mode_client::get_bytes_received ( ) const
inline

Definition at line 596 of file net_helper.h.

597  {
598  return m_bytes_received;
599  }
std::atomic< uint64_t > m_bytes_received
Definition: net_helper.h:687

◆ get_bytes_sent()

uint64_t epee::net_utils::blocked_mode_client::get_bytes_sent ( ) const
inline

Definition at line 591 of file net_helper.h.

592  {
593  return m_bytes_sent;
594  }
std::atomic< uint64_t > m_bytes_sent
Definition: net_helper.h:686

◆ get_io_service()

boost::asio::io_service& epee::net_utils::blocked_mode_client::get_io_service ( )
inline

Definition at line 581 of file net_helper.h.

582  {
583  return m_io_service;
584  }
boost::asio::io_service m_io_service
Definition: net_helper.h:677

◆ get_socket()

boost::asio::ip::tcp::socket& epee::net_utils::blocked_mode_client::get_socket ( )
inline

Definition at line 586 of file net_helper.h.

587  {
588  return m_ssl_socket->next_layer();
589  }
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679

◆ is_connected()

bool epee::net_utils::blocked_mode_client::is_connected ( bool ssl = NULL)
inline

Definition at line 392 of file net_helper.h.

393  {
394  if (!m_connected || !m_ssl_socket->next_layer().is_open())
395  return false;
396  if (ssl)
398  return true;
399  }
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679

◆ recv()

bool epee::net_utils::blocked_mode_client::recv ( std::string &  buff,
std::chrono::milliseconds  timeout 
)
inline

Definition at line 402 of file net_helper.h.

403  {
404 
405  try
406  {
407  // Set a deadline for the asynchronous operation. Since this function uses
408  // a composed operation (async_read_until), the deadline applies to the
409  // entire operation, rather than individual reads from the socket.
410  m_deadline.expires_from_now(timeout);
411 
412  // Set up the variable that receives the result of the asynchronous
413  // operation. The error code is set to would_block to signal that the
414  // operation is incomplete. Asio guarantees that its asynchronous
415  // operations will never fail with would_block, so any other value in
416  // ec indicates completion.
417  //boost::system::error_code ec = boost::asio::error::would_block;
418 
419  // Start the asynchronous operation itself. The boost::lambda function
420  // object is used as a callback and will update the ec variable when the
421  // operation completes. The blocking_udp_client.cpp example shows how you
422  // can use boost::bind rather than boost::lambda.
423 
424  boost::system::error_code ec = boost::asio::error::would_block;
425  size_t bytes_transfered = 0;
426 
427  handler_obj hndlr(ec, bytes_transfered);
428 
429  static const size_t max_size = 16384;
430  buff.resize(max_size);
431 
432  async_read(&buff[0], max_size, boost::asio::transfer_at_least(1), hndlr);
433 
434  // Block until the asynchronous operation has completed.
435  while (ec == boost::asio::error::would_block && !boost::interprocess::ipcdetail::atomic_read32(&m_shutdowned))
436  {
437  m_io_service.reset();
438  m_io_service.run_one();
439  }
440 
441 
442  if (ec)
443  {
444  MTRACE("READ ENDS: Connection err_code " << ec.value());
445  if(ec == boost::asio::error::eof)
446  {
447  MTRACE("Connection err_code eof.");
448  //connection closed there, empty
449  buff.clear();
450  return true;
451  }
452 
453  MDEBUG("Problems at read: " << ec.message());
454  m_connected = false;
455  return false;
456  }else
457  {
458  MTRACE("READ ENDS: Success. bytes_tr: " << bytes_transfered);
459  m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
460  }
461 
462  /*if(!bytes_transfered)
463  return false;*/
464 
465  m_bytes_received += bytes_transfered;
466  buff.resize(bytes_transfered);
467  return true;
468  }
469 
470  catch(const boost::system::system_error& er)
471  {
472  LOG_ERROR("Some problems at read, message: " << er.what());
473  m_connected = false;
474  return false;
475  }
476  catch(...)
477  {
478  LOG_ERROR("Some fatal problems at read.");
479  return false;
480  }
481 
482 
483 
484  return false;
485 
486  }
#define MTRACE(x)
Definition: misc_log_ex.h:77
#define MDEBUG(x)
Definition: misc_log_ex.h:76
boost::asio::io_service m_io_service
Definition: net_helper.h:677
boost::asio::steady_timer m_deadline
Definition: net_helper.h:684
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
void async_read(char *buff, size_t sz, boost::asio::detail::transfer_at_least_t transfer_at_least, handler_obj &hndlr)
Definition: net_helper.h:667
std::atomic< uint64_t > m_bytes_received
Definition: net_helper.h:687
Here is the call graph for this function:

◆ recv_n()

bool epee::net_utils::blocked_mode_client::recv_n ( std::string &  buff,
int64_t  sz,
std::chrono::milliseconds  timeout 
)
inline

Definition at line 488 of file net_helper.h.

489  {
490 
491  try
492  {
493  // Set a deadline for the asynchronous operation. Since this function uses
494  // a composed operation (async_read_until), the deadline applies to the
495  // entire operation, rather than individual reads from the socket.
496  m_deadline.expires_from_now(timeout);
497 
498  // Set up the variable that receives the result of the asynchronous
499  // operation. The error code is set to would_block to signal that the
500  // operation is incomplete. Asio guarantees that its asynchronous
501  // operations will never fail with would_block, so any other value in
502  // ec indicates completion.
503  //boost::system::error_code ec = boost::asio::error::would_block;
504 
505  // Start the asynchronous operation itself. The boost::lambda function
506  // object is used as a callback and will update the ec variable when the
507  // operation completes. The blocking_udp_client.cpp example shows how you
508  // can use boost::bind rather than boost::lambda.
509 
510  buff.resize(static_cast<size_t>(sz));
511  boost::system::error_code ec = boost::asio::error::would_block;
512  size_t bytes_transfered = 0;
513 
514 
515  handler_obj hndlr(ec, bytes_transfered);
516  async_read((char*)buff.data(), buff.size(), boost::asio::transfer_at_least(buff.size()), hndlr);
517 
518  // Block until the asynchronous operation has completed.
519  while (ec == boost::asio::error::would_block && !boost::interprocess::ipcdetail::atomic_read32(&m_shutdowned))
520  {
521  m_io_service.run_one();
522  }
523 
524  if (ec)
525  {
526  LOG_PRINT_L3("Problems at read: " << ec.message());
527  m_connected = false;
528  return false;
529  }else
530  {
531  m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
532  }
533 
534  m_bytes_received += bytes_transfered;
535  if(bytes_transfered != buff.size())
536  {
537  LOG_ERROR("Transferred mismatch with transfer_at_least value: m_bytes_transferred=" << bytes_transfered << " at_least value=" << buff.size());
538  return false;
539  }
540 
541  return true;
542  }
543 
544  catch(const boost::system::system_error& er)
545  {
546  LOG_ERROR("Some problems at read, message: " << er.what());
547  m_connected = false;
548  return false;
549  }
550  catch(...)
551  {
552  LOG_ERROR("Some fatal problems at read.");
553  return false;
554  }
555 
556 
557 
558  return false;
559  }
boost::asio::io_service m_io_service
Definition: net_helper.h:677
boost::asio::steady_timer m_deadline
Definition: net_helper.h:684
#define LOG_PRINT_L3(x)
Definition: misc_log_ex.h:102
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
void async_read(char *buff, size_t sz, boost::asio::detail::transfer_at_least_t transfer_at_least, handler_obj &hndlr)
Definition: net_helper.h:667
std::atomic< uint64_t > m_bytes_received
Definition: net_helper.h:687
Here is the call graph for this function:

◆ send() [1/2]

bool epee::net_utils::blocked_mode_client::send ( const std::string &  buff,
std::chrono::milliseconds  timeout 
)
inline

Definition at line 281 of file net_helper.h.

282  {
283 
284  try
285  {
286  m_deadline.expires_from_now(timeout);
287 
288  // Set up the variable that receives the result of the asynchronous
289  // operation. The error code is set to would_block to signal that the
290  // operation is incomplete. Asio guarantees that its asynchronous
291  // operations will never fail with would_block, so any other value in
292  // ec indicates completion.
293  boost::system::error_code ec = boost::asio::error::would_block;
294 
295  // Start the asynchronous operation itself. The boost::lambda function
296  // object is used as a callback and will update the ec variable when the
297  // operation completes. The blocking_udp_client.cpp example shows how you
298  // can use boost::bind rather than boost::lambda.
299  async_write(buff.c_str(), buff.size(), ec);
300 
301  // Block until the asynchronous operation has completed.
302  while (ec == boost::asio::error::would_block)
303  {
304  m_io_service.reset();
305  m_io_service.run_one();
306  }
307 
308  if (ec)
309  {
310  LOG_PRINT_L3("Problems at write: " << ec.message());
311  m_connected = false;
312  return false;
313  }else
314  {
315  m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
316  m_bytes_sent += buff.size();
317  }
318  }
319 
320  catch(const boost::system::system_error& er)
321  {
322  LOG_ERROR("Some problems at connect, message: " << er.what());
323  return false;
324  }
325  catch(...)
326  {
327  LOG_ERROR("Some fatal problems.");
328  return false;
329  }
330 
331  return true;
332  }
void async_write(const void *data, size_t sz, boost::system::error_code &ec)
Definition: net_helper.h:659
boost::asio::io_service m_io_service
Definition: net_helper.h:677
boost::asio::steady_timer m_deadline
Definition: net_helper.h:684
#define LOG_PRINT_L3(x)
Definition: misc_log_ex.h:102
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
std::atomic< uint64_t > m_bytes_sent
Definition: net_helper.h:686
Here is the call graph for this function:

◆ send() [2/2]

bool epee::net_utils::blocked_mode_client::send ( const void *  data,
size_t  sz 
)
inline

Definition at line 335 of file net_helper.h.

336  {
337  try
338  {
339  /*
340  m_deadline.expires_from_now(boost::posix_time::milliseconds(m_reciev_timeout));
341 
342  // Set up the variable that receives the result of the asynchronous
343  // operation. The error code is set to would_block to signal that the
344  // operation is incomplete. Asio guarantees that its asynchronous
345  // operations will never fail with would_block, so any other value in
346  // ec indicates completion.
347  boost::system::error_code ec = boost::asio::error::would_block;
348 
349  // Start the asynchronous operation itself. The boost::lambda function
350  // object is used as a callback and will update the ec variable when the
351  // operation completes. The blocking_udp_client.cpp example shows how you
352  // can use boost::bind rather than boost::lambda.
353  boost::asio::async_write(m_socket, boost::asio::buffer(data, sz), boost::lambda::var(ec) = boost::lambda::_1);
354 
355  // Block until the asynchronous operation has completed.
356  while (ec == boost::asio::error::would_block)
357  {
358  m_io_service.run_one();
359  }
360  */
361  boost::system::error_code ec;
362 
363  size_t writen = write(data, sz, ec);
364 
365  if (!writen || ec)
366  {
367  LOG_PRINT_L3("Problems at write: " << ec.message());
368  m_connected = false;
369  return false;
370  }else
371  {
372  m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
373  m_bytes_sent += sz;
374  }
375  }
376 
377  catch(const boost::system::system_error& er)
378  {
379  LOG_ERROR("Some problems at send, message: " << er.what());
380  m_connected = false;
381  return false;
382  }
383  catch(...)
384  {
385  LOG_ERROR("Some fatal problems.");
386  return false;
387  }
388 
389  return true;
390  }
boost::asio::steady_timer m_deadline
Definition: net_helper.h:684
bool write(const void *data, size_t sz, boost::system::error_code &ec)
Definition: net_helper.h:649
#define LOG_PRINT_L3(x)
Definition: misc_log_ex.h:102
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
std::atomic< uint64_t > m_bytes_sent
Definition: net_helper.h:686
Here is the call graph for this function:

◆ set_connector()

void epee::net_utils::blocked_mode_client::set_connector ( std::function< connect_func connector)
inline

Change the connection routine (proxy, etc.)

Definition at line 248 of file net_helper.h.

249  {
250  m_connector = std::move(connector);
251  }
const T & move(const T &t)
Definition: gtest-port.h:1317
std::function< connect_func > m_connector
Definition: net_helper.h:680
Here is the call graph for this function:

◆ set_ssl()

void epee::net_utils::blocked_mode_client::set_ssl ( ssl_options_t  ssl_options)
inline

Definition at line 141 of file net_helper.h.

142  {
143  if (ssl_options)
144  m_ctx = ssl_options.create_context();
145  else
146  m_ctx = boost::asio::ssl::context(boost::asio::ssl::context::tlsv12);
147  m_ssl_options = std::move(ssl_options);
148  }
boost::asio::ssl::context m_ctx
Definition: net_helper.h:678
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:98
const T & move(const T &t)
Definition: gtest-port.h:1317
Here is the call graph for this function:

◆ shutdown()

bool epee::net_utils::blocked_mode_client::shutdown ( )
inline

Definition at line 561 of file net_helper.h.

562  {
563  m_deadline.cancel();
564  boost::system::error_code ec;
566  shutdown_ssl();
567  m_ssl_socket->next_layer().cancel(ec);
568  if(ec)
569  MDEBUG("Problems at cancel: " << ec.message());
570  m_ssl_socket->next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
571  if(ec)
572  MDEBUG("Problems at shutdown: " << ec.message());
573  m_ssl_socket->next_layer().close(ec);
574  if(ec)
575  MDEBUG("Problems at close: " << ec.message());
576  boost::interprocess::ipcdetail::atomic_write32(&m_shutdowned, 1);
577  m_connected = false;
578  return true;
579  }
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679
#define MDEBUG(x)
Definition: misc_log_ex.h:76
boost::asio::steady_timer m_deadline
Definition: net_helper.h:684
Here is the caller graph for this function:

◆ try_connect()

try_connect_result_t epee::net_utils::blocked_mode_client::try_connect ( const std::string &  addr,
const std::string &  port,
std::chrono::milliseconds  timeout,
epee::net_utils::ssl_support_t  ssl_support 
)
inline

Definition at line 157 of file net_helper.h.

158  {
159  m_deadline.expires_from_now(timeout);
160  boost::unique_future<boost::asio::ip::tcp::socket> connection = m_connector(addr, port, m_deadline);
161  for (;;)
162  {
163  m_io_service.reset();
164  m_io_service.run_one();
165 
166  if (connection.is_ready())
167  break;
168  }
169 
170  m_ssl_socket->next_layer() = connection.get();
171  m_deadline.cancel();
172  if (m_ssl_socket->next_layer().is_open())
173  {
174  m_connected = true;
175  m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
176  // SSL Options
178  {
179  if (!m_ssl_options.handshake(*m_ssl_socket, boost::asio::ssl::stream_base::client, addr))
180  {
182  {
183  boost::system::error_code ignored_ec;
184  m_ssl_socket->next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
185  m_ssl_socket->next_layer().close();
186  m_connected = false;
187  return CONNECT_NO_SSL;
188  }
189  else
190  {
191  MWARNING("Failed to establish SSL connection");
192  m_connected = false;
193  return CONNECT_FAILURE;
194  }
195  }
196  }
197  return CONNECT_SUCCESS;
198  }else
199  {
200  MWARNING("Some problems at connect, expected open socket");
201  return CONNECT_FAILURE;
202  }
203 
204  }
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679
connection(typename TProtocol::config_type &ref_config)
boost::asio::io_service m_io_service
Definition: net_helper.h:677
bool handshake(boost::asio::ssl::stream< boost::asio::ip::tcp::socket > &socket, boost::asio::ssl::stream_base::handshake_type type, const std::string &host={}) const
Definition: net_ssl.cpp:459
boost::asio::steady_timer m_deadline
Definition: net_helper.h:684
#define MWARNING(x)
Definition: misc_log_ex.h:74
boost::endian::big_uint16_t port
Definition: socks.cpp:60
std::function< connect_func > m_connector
Definition: net_helper.h:680
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write()

bool epee::net_utils::blocked_mode_client::write ( const void *  data,
size_t  sz,
boost::system::error_code &  ec 
)
inlineprotected

Definition at line 649 of file net_helper.h.

650  {
651  bool success;
653  success = boost::asio::write(*m_ssl_socket, boost::asio::buffer(data, sz), ec);
654  else
655  success = boost::asio::write(m_ssl_socket->next_layer(), boost::asio::buffer(data, sz), ec);
656  return success;
657  }
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:679
expect< void > success() noexcept
Definition: expect.h:397
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_bytes_received

std::atomic<uint64_t> epee::net_utils::blocked_mode_client::m_bytes_received
protected

Definition at line 687 of file net_helper.h.

◆ m_bytes_sent

std::atomic<uint64_t> epee::net_utils::blocked_mode_client::m_bytes_sent
protected

Definition at line 686 of file net_helper.h.

◆ m_connected

bool epee::net_utils::blocked_mode_client::m_connected
protected

Definition at line 683 of file net_helper.h.

◆ m_connector

std::function<connect_func> epee::net_utils::blocked_mode_client::m_connector
protected

Definition at line 680 of file net_helper.h.

◆ m_ctx

boost::asio::ssl::context epee::net_utils::blocked_mode_client::m_ctx
protected

Definition at line 678 of file net_helper.h.

◆ m_deadline

boost::asio::steady_timer epee::net_utils::blocked_mode_client::m_deadline
protected

Definition at line 684 of file net_helper.h.

◆ m_initialized

bool epee::net_utils::blocked_mode_client::m_initialized
protected

Definition at line 682 of file net_helper.h.

◆ m_io_service

boost::asio::io_service epee::net_utils::blocked_mode_client::m_io_service
protected

Definition at line 677 of file net_helper.h.

◆ m_shutdowned

volatile uint32_t epee::net_utils::blocked_mode_client::m_shutdowned
protected

Definition at line 685 of file net_helper.h.

◆ m_ssl_options

ssl_options_t epee::net_utils::blocked_mode_client::m_ssl_options
protected

Definition at line 681 of file net_helper.h.

◆ m_ssl_socket

std::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket> > epee::net_utils::blocked_mode_client::m_ssl_socket
protected

Definition at line 679 of file net_helper.h.


The documentation for this class was generated from the following file: