Electroneum
epee::levin::async_protocol_handler< t_connection_context > Class Template Reference

#include <levin_protocol_handler_async.h>

Collaboration diagram for epee::levin::async_protocol_handler< t_connection_context >:

Classes

struct  anvoke_handler
 
struct  invoke_response_handler_base
 

Public Types

enum  stream_state { stream_state_head, stream_state_body }
 
typedef t_connection_context connection_context
 
typedef async_protocol_handler_config< t_connection_context > config_type
 

Public Member Functions

template<class callback_t >
bool add_invoke_response_handler (const callback_t &cb, uint64_t timeout, async_protocol_handler &con, int command)
 
 async_protocol_handler (net_utils::i_service_endpoint *psnd_hndlr, config_type &config, t_connection_context &conn_context)
 
virtual ~async_protocol_handler ()
 
bool start_outer_call ()
 
bool finish_outer_call ()
 
bool release_protocol ()
 
bool close ()
 
void update_connection_context (const connection_context &contxt)
 
void request_callback ()
 
void handle_qued_callback ()
 
virtual bool handle_recv (const void *ptr, size_t cb)
 
bool after_init_connection ()
 
template<class callback_t >
bool async_invoke (int command, const epee::span< const uint8_t > in_buff, const callback_t &cb, size_t timeout=LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED)
 
int invoke (int command, const epee::span< const uint8_t > in_buff, std::string &buff_out)
 
int notify (int command, const epee::span< const uint8_t > in_buff)
 
boost::uuids::uuid get_connection_id ()
 
t_connection_context & get_context_ref ()
 

Public Attributes

std::atomic< boolm_deletion_initiated
 
std::atomic< boolm_protocol_released
 
volatile uint32_t m_invoke_buf_ready
 
volatile int m_invoke_result_code
 
critical_section m_local_inv_buff_lock
 
std::string m_local_inv_buff
 
critical_section m_send_lock
 
critical_section m_call_lock
 
volatile uint32_t m_wait_count
 
volatile uint32_t m_close_called
 
bucket_head2 m_current_head
 
net_utils::i_service_endpointm_pservice_endpoint
 
config_typem_config
 
t_connection_context & m_connection_context
 
net_utils::buffer m_cache_in_buffer
 
stream_state m_state
 
int32_t m_oponent_protocol_ver
 
bool m_connection_initialized
 
critical_section m_invoke_response_handlers_lock
 
std::list< boost::shared_ptr< invoke_response_handler_base > > m_invoke_response_handlers
 

Friends

template<class callback_t >
struct anvoke_handler
 

Detailed Description

template<class t_connection_context = net_utils::connection_context_base>
class epee::levin::async_protocol_handler< t_connection_context >

Definition at line 62 of file levin_protocol_handler_async.h.

Member Typedef Documentation

◆ config_type

template<class t_connection_context = net_utils::connection_context_base>
typedef async_protocol_handler_config<t_connection_context> epee::levin::async_protocol_handler< t_connection_context >::config_type

Definition at line 120 of file levin_protocol_handler_async.h.

◆ connection_context

template<class t_connection_context = net_utils::connection_context_base>
typedef t_connection_context epee::levin::async_protocol_handler< t_connection_context >::connection_context

Definition at line 119 of file levin_protocol_handler_async.h.

Member Enumeration Documentation

◆ stream_state

template<class t_connection_context = net_utils::connection_context_base>
enum epee::levin::async_protocol_handler::stream_state

Constructor & Destructor Documentation

◆ async_protocol_handler()

template<class t_connection_context = net_utils::connection_context_base>
epee::levin::async_protocol_handler< t_connection_context >::async_protocol_handler ( net_utils::i_service_endpoint psnd_hndlr,
config_type config,
t_connection_context &  conn_context 
)
inline

Definition at line 262 of file levin_protocol_handler_async.h.

264  :
265  m_current_head(bucket_head2()),
266  m_pservice_endpoint(psnd_hndlr),
267  m_config(config),
268  m_connection_context(conn_context),
269  m_cache_in_buffer(4 * 1024),
271  {
272  m_close_called = 0;
273  m_deletion_initiated = false;
274  m_protocol_released = false;
275  m_wait_count = 0;
277  m_connection_initialized = false;
278  m_invoke_buf_ready = 0;
280  }
net_utils::i_service_endpoint * m_pservice_endpoint
#define LEVIN_ERROR_CONNECTION
Definition: levin_base.h:94

◆ ~async_protocol_handler()

template<class t_connection_context = net_utils::connection_context_base>
virtual epee::levin::async_protocol_handler< t_connection_context >::~async_protocol_handler ( )
inlinevirtual

Definition at line 281 of file levin_protocol_handler_async.h.

282  {
283  try
284  {
285 
286  m_deletion_initiated = true;
288  {
289  m_config.del_connection(this);
290  }
291 
292  for (size_t i = 0; i < 60 * 1000 / 100 && 0 != boost::interprocess::ipcdetail::atomic_read32(&m_wait_count); ++i)
293  {
295  }
296  CHECK_AND_ASSERT_MES_NO_RET(0 == boost::interprocess::ipcdetail::atomic_read32(&m_wait_count), "Failed to wait for operation completion. m_wait_count = " << m_wait_count);
297 
298  MTRACE(m_connection_context << "~async_protocol_handler()");
299 
300  }
301  catch (...) { /* ignore */ }
302  }
#define MTRACE(x)
Definition: misc_log_ex.h:77
#define CHECK_AND_ASSERT_MES_NO_RET(expr, message)
Definition: misc_log_ex.h:198
bool sleep_no_w(long ms)
Here is the call graph for this function:

Member Function Documentation

◆ add_invoke_response_handler()

template<class t_connection_context = net_utils::connection_context_base>
template<class callback_t >
bool epee::levin::async_protocol_handler< t_connection_context >::add_invoke_response_handler ( const callback_t &  cb,
uint64_t  timeout,
async_protocol_handler< t_connection_context > &  con,
int  command 
)
inline

Definition at line 253 of file levin_protocol_handler_async.h.

254  {
256  boost::shared_ptr<invoke_response_handler_base> handler(boost::make_shared<anvoke_handler<callback_t>>(cb, timeout, con, command));
257  m_invoke_response_handlers.push_back(handler);
258  return handler->is_timer_started();
259  }
std::list< boost::shared_ptr< invoke_response_handler_base > > m_invoke_response_handlers
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:228
Here is the caller graph for this function:

◆ after_init_connection()

template<class t_connection_context = net_utils::connection_context_base>
bool epee::levin::async_protocol_handler< t_connection_context >::after_init_connection ( )
inline

Definition at line 549 of file levin_protocol_handler_async.h.

550  {
552  {
554  m_config.add_connection(this);
555  }
556  return true;
557  }

◆ async_invoke()

template<class t_connection_context = net_utils::connection_context_base>
template<class callback_t >
bool epee::levin::async_protocol_handler< t_connection_context >::async_invoke ( int  command,
const epee::span< const uint8_t in_buff,
const callback_t &  cb,
size_t  timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED 
)
inline

Definition at line 560 of file levin_protocol_handler_async.h.

561  {
563  boost::bind(&async_protocol_handler::finish_outer_call, this));
564 
566  timeout = m_config.m_invoke_timeout;
567 
568  int err_code = LEVIN_OK;
569  do
570  {
572  {
574  break;
575  }
576 
578 
580  {
582  break;
583  }
584 
585  bucket_head2 head = {0};
586  head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
587  head.m_cb = SWAP64LE(in_buff.size());
588  head.m_have_to_return_data = true;
589 
591  head.m_command = SWAP32LE(command);
592  head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
593 
594  boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0);
597  if(!m_pservice_endpoint->do_send(&head, sizeof(head)))
598  {
599  LOG_ERROR_CC(m_connection_context, "Failed to do_send");
600  err_code = LEVIN_ERROR_CONNECTION;
601  break;
602  }
603 
604  if(!m_pservice_endpoint->do_send(in_buff.data(), in_buff.size()))
605  {
606  LOG_ERROR_CC(m_connection_context, "Failed to do_send");
607  err_code = LEVIN_ERROR_CONNECTION;
608  break;
609  }
610 
611  if(!add_invoke_response_handler(cb, timeout, *this, command))
612  {
614  break;
615  }
617  } while (false);
618 
619  if (LEVIN_OK != err_code)
620  {
621  epee::span<const uint8_t> stub_buff{(const uint8_t*)"", 0};
622  // Never call callback inside critical section, that can cause deadlock
623  cb(err_code, stub_buff, m_connection_context);
624  return false;
625  }
626 
627  return true;
628  }
#define CRITICAL_REGION_LOCAL1(x)
Definition: syncobj.h:230
#define LEVIN_SIGNATURE
Definition: levin_base.h:34
net_utils::i_service_endpoint * m_pservice_endpoint
#define LEVIN_PACKET_REQUEST
Definition: levin_base.h:73
auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f)
Non-owning sequence of data. Does not deep copy.
Definition: span.h:56
bool add_invoke_response_handler(const callback_t &cb, uint64_t timeout, async_protocol_handler &con, int command)
unsigned char uint8_t
Definition: stdint.h:124
#define CRITICAL_REGION_END()
Definition: syncobj.h:233
constexpr std::size_t size() const noexcept
Definition: span.h:111
#define CRITICAL_REGION_BEGIN(x)
Definition: syncobj.h:229
boost::shared_ptr< call_befor_die_base > auto_scope_leave_caller
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:228
#define LOG_ERROR_CC(ct, message)
#define LEVIN_ERROR_CONNECTION
Definition: levin_base.h:94
#define SWAP64LE
Definition: int-util.h:252
#define LEVIN_PROTOCOL_VER_1
Definition: levin_base.h:78
struct rule_list head
#define LEVIN_ERROR_CONNECTION_DESTROYED
Definition: levin_base.h:96
virtual bool do_send(const void *ptr, size_t cb)=0
#define LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED
Definition: levin_base.h:70
#define SWAP32LE
Definition: int-util.h:244
#define LEVIN_OK
Definition: levin_base.h:93
constexpr pointer data() const noexcept
Definition: span.h:110
Here is the call graph for this function:

◆ close()

template<class t_connection_context = net_utils::connection_context_base>
bool epee::levin::async_protocol_handler< t_connection_context >::close ( )
inline

Definition at line 340 of file levin_protocol_handler_async.h.

341  {
342  boost::interprocess::ipcdetail::atomic_inc32(&m_close_called);
343 
345  return true;
346  }
net_utils::i_service_endpoint * m_pservice_endpoint
Here is the call graph for this function:
Here is the caller graph for this function:

◆ finish_outer_call()

template<class t_connection_context = net_utils::connection_context_base>
bool epee::levin::async_protocol_handler< t_connection_context >::finish_outer_call ( )
inline

Definition at line 315 of file levin_protocol_handler_async.h.

316  {
317  MTRACE(m_connection_context << "[levin_protocol] <<-- finish_outer_call");
318  boost::interprocess::ipcdetail::atomic_dec32(&m_wait_count);
320  return true;
321  }
net_utils::i_service_endpoint * m_pservice_endpoint
#define MTRACE(x)
Definition: misc_log_ex.h:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_connection_id()

template<class t_connection_context = net_utils::connection_context_base>
boost::uuids::uuid epee::levin::async_protocol_handler< t_connection_context >::get_connection_id ( )
inline

Definition at line 747 of file levin_protocol_handler_async.h.

747 {return m_connection_context.m_connection_id;}

◆ get_context_ref()

template<class t_connection_context = net_utils::connection_context_base>
t_connection_context& epee::levin::async_protocol_handler< t_connection_context >::get_context_ref ( )
inline

Definition at line 749 of file levin_protocol_handler_async.h.

Here is the caller graph for this function:

◆ handle_qued_callback()

template<class t_connection_context = net_utils::connection_context_base>
void epee::levin::async_protocol_handler< t_connection_context >::handle_qued_callback ( )
inline

Definition at line 361 of file levin_protocol_handler_async.h.

362  {
363  m_config.m_pcommands_handler->callback(m_connection_context);
364  }

◆ handle_recv()

template<class t_connection_context = net_utils::connection_context_base>
virtual bool epee::levin::async_protocol_handler< t_connection_context >::handle_recv ( const void *  ptr,
size_t  cb 
)
inlinevirtual

Definition at line 366 of file levin_protocol_handler_async.h.

367  {
368  if(boost::interprocess::ipcdetail::atomic_read32(&m_close_called))
369  return false; //closing connections
370 
371  if(!m_config.m_pcommands_handler)
372  {
373  MERROR(m_connection_context << "Commands handler not set!");
374  return false;
375  }
376 
378  {
379  MWARNING(m_connection_context << "Maximum packet size exceed!, m_max_packet_size = " << m_config.m_max_packet_size
380  << ", packet received " << m_cache_in_buffer.size() + cb
381  << ", connection will be closed.");
382  return false;
383  }
384 
385  m_cache_in_buffer.append((const char*)ptr, cb);
386 
387  bool is_continue = true;
388  while(is_continue)
389  {
390  switch(m_state)
391  {
392  case stream_state_body:
394  {
395  is_continue = false;
396  if(cb >= MIN_BYTES_WANTED)
397  {
399  if (!m_invoke_response_handlers.empty())
400  {
401  //async call scenario
402  boost::shared_ptr<invoke_response_handler_base> response_handler = m_invoke_response_handlers.front();
403  response_handler->reset_timer();
404  MDEBUG(m_connection_context << "LEVIN_PACKET partial msg received. len=" << cb);
405  }
406  }
407  break;
408  }
409  {
410  epee::span<const uint8_t> buff_to_invoke = m_cache_in_buffer.carve((std::string::size_type)m_current_head.m_cb);
411 
413 
414  MDEBUG(m_connection_context << "LEVIN_PACKET_RECEIVED. [len=" << m_current_head.m_cb
415  << ", flags" << m_current_head.m_flags
417  <<", cmd = " << m_current_head.m_command
418  << ", v=" << m_current_head.m_protocol_version);
419 
420  if(is_response)
421  {//response to some invoke
422 
424  if(!m_invoke_response_handlers.empty())
425  {//async call scenario
426  boost::shared_ptr<invoke_response_handler_base> response_handler = m_invoke_response_handlers.front();
427  bool timer_cancelled = response_handler->cancel_timer();
428  // Don't pop handler, to avoid destroying it
429  if(timer_cancelled)
430  m_invoke_response_handlers.pop_front();
431  invoke_response_handlers_guard.unlock();
432 
433  if(timer_cancelled)
434  response_handler->handle(m_current_head.m_return_code, buff_to_invoke, m_connection_context);
435  }
436  else
437  {
438  invoke_response_handlers_guard.unlock();
439  //use sync call scenario
440  if(!boost::interprocess::ipcdetail::atomic_read32(&m_wait_count) && !boost::interprocess::ipcdetail::atomic_read32(&m_close_called))
441  {
442  MERROR(m_connection_context << "no active invoke when response came, wtf?");
443  return false;
444  }else
445  {
447  m_local_inv_buff = std::string((const char*)buff_to_invoke.data(), buff_to_invoke.size());
448  buff_to_invoke = epee::span<const uint8_t>((const uint8_t*)NULL, 0);
451  boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 1);
452  }
453  }
454  }else
455  {
457  {
458  std::string return_buff;
459  m_current_head.m_return_code = m_config.m_pcommands_handler->invoke(
461  buff_to_invoke,
462  return_buff,
464  m_current_head.m_cb = return_buff.size();
468 #if BYTE_ORDER == LITTLE_ENDIAN
469  std::string send_buff((const char*)&m_current_head, sizeof(m_current_head));
470 #else
471  bucket_head2 head = m_current_head;
472  head.m_signature = SWAP64LE(head.m_signature);
473  head.m_cb = SWAP64LE(head.m_cb);
474  head.m_command = SWAP32LE(head.m_command);
475  head.m_return_code = SWAP32LE(head.m_return_code);
476  head.m_flags = SWAP32LE(head.m_flags);
477  head.m_protocol_version = SWAP32LE(head.m_protocol_version);
478  std::string send_buff((const char*)&head, sizeof(head));
479 #endif
480  send_buff += return_buff;
482  if(!m_pservice_endpoint->do_send(send_buff.data(), send_buff.size()))
483  return false;
485  MDEBUG(m_connection_context << "LEVIN_PACKET_SENT. [len=" << m_current_head.m_cb
486  << ", flags" << m_current_head.m_flags
488  <<", cmd = " << m_current_head.m_command
489  << ", ver=" << m_current_head.m_protocol_version);
490  }
491  else
492  m_config.m_pcommands_handler->notify(m_current_head.m_command, buff_to_invoke, m_connection_context);
493  }
494  }
496  break;
497  case stream_state_head:
498  {
499  if(m_cache_in_buffer.size() < sizeof(bucket_head2))
500  {
502  {
503  MWARNING(m_connection_context << "Signature mismatch, connection will be closed");
504  return false;
505  }
506  is_continue = false;
507  break;
508  }
509 
510 #if BYTE_ORDER == LITTLE_ENDIAN
511  bucket_head2& phead = *(bucket_head2*)m_cache_in_buffer.span(sizeof(bucket_head2)).data();
512 #else
513  bucket_head2 phead = *(bucket_head2*)m_cache_in_buffer.span(sizeof(bucket_head2)).data();
514  phead.m_signature = SWAP64LE(phead.m_signature);
515  phead.m_cb = SWAP64LE(phead.m_cb);
516  phead.m_command = SWAP32LE(phead.m_command);
517  phead.m_return_code = SWAP32LE(phead.m_return_code);
518  phead.m_flags = SWAP32LE(phead.m_flags);
519  phead.m_protocol_version = SWAP32LE(phead.m_protocol_version);
520 #endif
521  if(LEVIN_SIGNATURE != phead.m_signature)
522  {
523  LOG_ERROR_CC(m_connection_context, "Signature mismatch, connection will be closed");
524  return false;
525  }
526  m_current_head = phead;
527 
528  m_cache_in_buffer.erase(sizeof(bucket_head2));
532  {
533  LOG_ERROR_CC(m_connection_context, "Maximum packet size exceed!, m_max_packet_size = " << m_config.m_max_packet_size
534  << ", packet header received " << m_current_head.m_cb
535  << ", connection will be closed.");
536  return false;
537  }
538  }
539  break;
540  default:
541  LOG_ERROR_CC(m_connection_context, "Undefined state in levin_server_impl::connection_handler, m_state=" << m_state);
542  return false;
543  }
544  }
545 
546  return true;
547  }
#define MERROR(x)
Definition: misc_log_ex.h:73
void append(const void *data, size_t sz)
Definition: buffer.cpp:40
#define LEVIN_SIGNATURE
Definition: levin_base.h:34
net_utils::i_service_endpoint * m_pservice_endpoint
::std::string string
Definition: gtest-port.h:1097
#define LEVIN_PACKET_RESPONSE
Definition: levin_base.h:74
#define MIN_BYTES_WANTED
void erase(size_t sz)
Definition: buffer.h:51
Non-owning sequence of data. Does not deep copy.
Definition: span.h:56
unsigned char uint8_t
Definition: stdint.h:124
#define CRITICAL_REGION_END()
Definition: syncobj.h:233
#define MDEBUG(x)
Definition: misc_log_ex.h:76
constexpr std::size_t size() const noexcept
Definition: span.h:111
size_t size() const
Definition: buffer.h:55
std::list< boost::shared_ptr< invoke_response_handler_base > > m_invoke_response_handlers
#define CRITICAL_REGION_BEGIN(x)
Definition: syncobj.h:229
epee::span< const uint8_t > carve(size_t sz)
Definition: buffer.h:54
unsigned __int64 uint64_t
Definition: stdint.h:136
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:228
#define MWARNING(x)
Definition: misc_log_ex.h:74
#define LOG_ERROR_CC(ct, message)
#define SWAP64LE
Definition: int-util.h:252
epee::span< const uint8_t > span(size_t sz) const
Definition: buffer.h:52
#define LEVIN_PROTOCOL_VER_1
Definition: levin_base.h:78
struct rule_list head
virtual bool do_send(const void *ptr, size_t cb)=0
#define SWAP32LE
Definition: int-util.h:244
constexpr pointer data() const noexcept
Definition: span.h:110
Here is the call graph for this function:

◆ invoke()

template<class t_connection_context = net_utils::connection_context_base>
int epee::levin::async_protocol_handler< t_connection_context >::invoke ( int  command,
const epee::span< const uint8_t in_buff,
std::string &  buff_out 
)
inline

Definition at line 630 of file levin_protocol_handler_async.h.

631  {
633  boost::bind(&async_protocol_handler::finish_outer_call, this));
634 
637 
639 
642 
643  bucket_head2 head = {0};
644  head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
645  head.m_cb = SWAP64LE(in_buff.size());
646  head.m_have_to_return_data = true;
647 
649  head.m_command = SWAP32LE(command);
650  head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
651 
652  boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0);
654  if(!m_pservice_endpoint->do_send(&head, sizeof(head)))
655  {
656  LOG_ERROR_CC(m_connection_context, "Failed to do_send");
657  return LEVIN_ERROR_CONNECTION;
658  }
659 
660  if(!m_pservice_endpoint->do_send(in_buff.data(), in_buff.size()))
661  {
662  LOG_ERROR_CC(m_connection_context, "Failed to do_send");
663  return LEVIN_ERROR_CONNECTION;
664  }
666 
667  MDEBUG(m_connection_context << "LEVIN_PACKET_SENT. [len=" << head.m_cb
668  << ", f=" << head.m_flags
669  << ", r?=" << head.m_have_to_return_data
670  << ", cmd = " << head.m_command
671  << ", ver=" << head.m_protocol_version);
672 
673  uint64_t ticks_start = misc_utils::get_tick_count();
674  size_t prev_size = 0;
675 
676  while(!boost::interprocess::ipcdetail::atomic_read32(&m_invoke_buf_ready) && !m_deletion_initiated && !m_protocol_released)
677  {
678  if(m_cache_in_buffer.size() - prev_size >= MIN_BYTES_WANTED)
679  {
680  prev_size = m_cache_in_buffer.size();
681  ticks_start = misc_utils::get_tick_count();
682  }
684  {
685  MWARNING(m_connection_context << "invoke timeout (" << m_config.m_invoke_timeout << "), closing connection ");
686  close();
688  }
691  }
692 
695 
697  buff_out.swap(m_local_inv_buff);
698  m_local_inv_buff.clear();
700 
701  return m_invoke_result_code;
702  }
#define LEVIN_ERROR_CONNECTION_TIMEDOUT
Definition: levin_base.h:97
#define LEVIN_SIGNATURE
Definition: levin_base.h:34
net_utils::i_service_endpoint * m_pservice_endpoint
uint64_t get_tick_count()
#define LEVIN_PACKET_REQUEST
Definition: levin_base.h:73
auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f)
#define MIN_BYTES_WANTED
#define CRITICAL_REGION_END()
Definition: syncobj.h:233
#define MDEBUG(x)
Definition: misc_log_ex.h:76
constexpr std::size_t size() const noexcept
Definition: span.h:111
size_t size() const
Definition: buffer.h:55
#define CRITICAL_REGION_BEGIN(x)
Definition: syncobj.h:229
boost::shared_ptr< call_befor_die_base > auto_scope_leave_caller
unsigned __int64 uint64_t
Definition: stdint.h:136
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:228
#define MWARNING(x)
Definition: misc_log_ex.h:74
#define LOG_ERROR_CC(ct, message)
#define LEVIN_ERROR_CONNECTION
Definition: levin_base.h:94
#define SWAP64LE
Definition: int-util.h:252
virtual bool call_run_once_service_io()=0
#define LEVIN_PROTOCOL_VER_1
Definition: levin_base.h:78
struct rule_list head
#define LEVIN_ERROR_CONNECTION_DESTROYED
Definition: levin_base.h:96
virtual bool do_send(const void *ptr, size_t cb)=0
#define SWAP32LE
Definition: int-util.h:244
constexpr pointer data() const noexcept
Definition: span.h:110
Here is the call graph for this function:

◆ notify()

template<class t_connection_context = net_utils::connection_context_base>
int epee::levin::async_protocol_handler< t_connection_context >::notify ( int  command,
const epee::span< const uint8_t in_buff 
)
inline

Definition at line 704 of file levin_protocol_handler_async.h.

705  {
707  boost::bind(&async_protocol_handler::finish_outer_call, this));
708 
711 
713 
716 
717  bucket_head2 head = {0};
718  head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
719  head.m_have_to_return_data = false;
720  head.m_cb = SWAP64LE(in_buff.size());
721 
722  head.m_command = SWAP32LE(command);
723  head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
726  if(!m_pservice_endpoint->do_send(&head, sizeof(head)))
727  {
728  LOG_ERROR_CC(m_connection_context, "Failed to do_send()");
729  return -1;
730  }
731 
732  if(!m_pservice_endpoint->do_send(in_buff.data(), in_buff.size()))
733  {
734  LOG_ERROR_CC(m_connection_context, "Failed to do_send()");
735  return -1;
736  }
738  LOG_DEBUG_CC(m_connection_context, "LEVIN_PACKET_SENT. [len=" << head.m_cb <<
739  ", f=" << head.m_flags <<
740  ", r?=" << head.m_have_to_return_data <<
741  ", cmd = " << head.m_command <<
742  ", ver=" << head.m_protocol_version);
743 
744  return 1;
745  }
#define LEVIN_SIGNATURE
Definition: levin_base.h:34
net_utils::i_service_endpoint * m_pservice_endpoint
#define LOG_DEBUG_CC(ct, message)
#define LEVIN_PACKET_REQUEST
Definition: levin_base.h:73
auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f)
#define CRITICAL_REGION_END()
Definition: syncobj.h:233
constexpr std::size_t size() const noexcept
Definition: span.h:111
#define CRITICAL_REGION_BEGIN(x)
Definition: syncobj.h:229
boost::shared_ptr< call_befor_die_base > auto_scope_leave_caller
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:228
#define LOG_ERROR_CC(ct, message)
#define SWAP64LE
Definition: int-util.h:252
#define LEVIN_PROTOCOL_VER_1
Definition: levin_base.h:78
struct rule_list head
#define LEVIN_ERROR_CONNECTION_DESTROYED
Definition: levin_base.h:96
virtual bool do_send(const void *ptr, size_t cb)=0
#define SWAP32LE
Definition: int-util.h:244
constexpr pointer data() const noexcept
Definition: span.h:110
Here is the call graph for this function:

◆ release_protocol()

template<class t_connection_context = net_utils::connection_context_base>
bool epee::levin::async_protocol_handler< t_connection_context >::release_protocol ( )
inline

Definition at line 323 of file levin_protocol_handler_async.h.

324  {
325  decltype(m_invoke_response_handlers) local_invoke_response_handlers;
327  local_invoke_response_handlers.swap(m_invoke_response_handlers);
328  m_protocol_released = true;
330 
331  // Never call callback inside critical section, that can cause deadlock. Callback can be called when
332  // invoke_response_handler_base is cancelled
333  std::for_each(local_invoke_response_handlers.begin(), local_invoke_response_handlers.end(), [](const boost::shared_ptr<invoke_response_handler_base>& pinv_resp_hndlr) {
334  pinv_resp_hndlr->cancel();
335  });
336 
337  return true;
338  }
#define CRITICAL_REGION_END()
Definition: syncobj.h:233
std::list< boost::shared_ptr< invoke_response_handler_base > > m_invoke_response_handlers
#define CRITICAL_REGION_BEGIN(x)
Definition: syncobj.h:229

◆ request_callback()

template<class t_connection_context = net_utils::connection_context_base>
void epee::levin::async_protocol_handler< t_connection_context >::request_callback ( )
inline

Definition at line 353 of file levin_protocol_handler_async.h.

354  {
356  boost::bind(&async_protocol_handler::finish_outer_call, this));
357 
359  }
net_utils::i_service_endpoint * m_pservice_endpoint
auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f)
boost::shared_ptr< call_befor_die_base > auto_scope_leave_caller
Here is the call graph for this function:

◆ start_outer_call()

template<class t_connection_context = net_utils::connection_context_base>
bool epee::levin::async_protocol_handler< t_connection_context >::start_outer_call ( )
inline

Definition at line 304 of file levin_protocol_handler_async.h.

305  {
306  MTRACE(m_connection_context << "[levin_protocol] -->> start_outer_call");
308  {
309  MERROR(m_connection_context << "[levin_protocol] -->> start_outer_call failed");
310  return false;
311  }
312  boost::interprocess::ipcdetail::atomic_inc32(&m_wait_count);
313  return true;
314  }
#define MERROR(x)
Definition: misc_log_ex.h:73
net_utils::i_service_endpoint * m_pservice_endpoint
#define MTRACE(x)
Definition: misc_log_ex.h:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_connection_context()

template<class t_connection_context = net_utils::connection_context_base>
void epee::levin::async_protocol_handler< t_connection_context >::update_connection_context ( const connection_context contxt)
inline

Definition at line 348 of file levin_protocol_handler_async.h.

349  {
350  m_connection_context = contxt;
351  }

Friends And Related Function Documentation

◆ anvoke_handler

template<class t_connection_context = net_utils::connection_context_base>
template<class callback_t >
friend struct anvoke_handler
friend

Definition at line 260 of file levin_protocol_handler_async.h.

Member Data Documentation

◆ m_cache_in_buffer

template<class t_connection_context = net_utils::connection_context_base>
net_utils::buffer epee::levin::async_protocol_handler< t_connection_context >::m_cache_in_buffer

Definition at line 147 of file levin_protocol_handler_async.h.

◆ m_call_lock

template<class t_connection_context = net_utils::connection_context_base>
critical_section epee::levin::async_protocol_handler< t_connection_context >::m_call_lock

Definition at line 138 of file levin_protocol_handler_async.h.

◆ m_close_called

template<class t_connection_context = net_utils::connection_context_base>
volatile uint32_t epee::levin::async_protocol_handler< t_connection_context >::m_close_called

Definition at line 141 of file levin_protocol_handler_async.h.

◆ m_config

template<class t_connection_context = net_utils::connection_context_base>
config_type& epee::levin::async_protocol_handler< t_connection_context >::m_config

Definition at line 144 of file levin_protocol_handler_async.h.

◆ m_connection_context

template<class t_connection_context = net_utils::connection_context_base>
t_connection_context& epee::levin::async_protocol_handler< t_connection_context >::m_connection_context

Definition at line 145 of file levin_protocol_handler_async.h.

◆ m_connection_initialized

template<class t_connection_context = net_utils::connection_context_base>
bool epee::levin::async_protocol_handler< t_connection_context >::m_connection_initialized

Definition at line 151 of file levin_protocol_handler_async.h.

◆ m_current_head

template<class t_connection_context = net_utils::connection_context_base>
bucket_head2 epee::levin::async_protocol_handler< t_connection_context >::m_current_head

Definition at line 142 of file levin_protocol_handler_async.h.

◆ m_deletion_initiated

template<class t_connection_context = net_utils::connection_context_base>
std::atomic<bool> epee::levin::async_protocol_handler< t_connection_context >::m_deletion_initiated

Definition at line 128 of file levin_protocol_handler_async.h.

◆ m_invoke_buf_ready

template<class t_connection_context = net_utils::connection_context_base>
volatile uint32_t epee::levin::async_protocol_handler< t_connection_context >::m_invoke_buf_ready

Definition at line 130 of file levin_protocol_handler_async.h.

◆ m_invoke_response_handlers

template<class t_connection_context = net_utils::connection_context_base>
std::list<boost::shared_ptr<invoke_response_handler_base> > epee::levin::async_protocol_handler< t_connection_context >::m_invoke_response_handlers

Definition at line 250 of file levin_protocol_handler_async.h.

◆ m_invoke_response_handlers_lock

template<class t_connection_context = net_utils::connection_context_base>
critical_section epee::levin::async_protocol_handler< t_connection_context >::m_invoke_response_handlers_lock

Definition at line 249 of file levin_protocol_handler_async.h.

◆ m_invoke_result_code

template<class t_connection_context = net_utils::connection_context_base>
volatile int epee::levin::async_protocol_handler< t_connection_context >::m_invoke_result_code

Definition at line 132 of file levin_protocol_handler_async.h.

◆ m_local_inv_buff

template<class t_connection_context = net_utils::connection_context_base>
std::string epee::levin::async_protocol_handler< t_connection_context >::m_local_inv_buff

Definition at line 135 of file levin_protocol_handler_async.h.

◆ m_local_inv_buff_lock

template<class t_connection_context = net_utils::connection_context_base>
critical_section epee::levin::async_protocol_handler< t_connection_context >::m_local_inv_buff_lock

Definition at line 134 of file levin_protocol_handler_async.h.

◆ m_oponent_protocol_ver

template<class t_connection_context = net_utils::connection_context_base>
int32_t epee::levin::async_protocol_handler< t_connection_context >::m_oponent_protocol_ver

Definition at line 150 of file levin_protocol_handler_async.h.

◆ m_protocol_released

template<class t_connection_context = net_utils::connection_context_base>
std::atomic<bool> epee::levin::async_protocol_handler< t_connection_context >::m_protocol_released

Definition at line 129 of file levin_protocol_handler_async.h.

◆ m_pservice_endpoint

template<class t_connection_context = net_utils::connection_context_base>
net_utils::i_service_endpoint* epee::levin::async_protocol_handler< t_connection_context >::m_pservice_endpoint

Definition at line 143 of file levin_protocol_handler_async.h.

◆ m_send_lock

template<class t_connection_context = net_utils::connection_context_base>
critical_section epee::levin::async_protocol_handler< t_connection_context >::m_send_lock

Definition at line 137 of file levin_protocol_handler_async.h.

◆ m_state

template<class t_connection_context = net_utils::connection_context_base>
stream_state epee::levin::async_protocol_handler< t_connection_context >::m_state

Definition at line 148 of file levin_protocol_handler_async.h.

◆ m_wait_count

template<class t_connection_context = net_utils::connection_context_base>
volatile uint32_t epee::levin::async_protocol_handler< t_connection_context >::m_wait_count

Definition at line 140 of file levin_protocol_handler_async.h.


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