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

#include <levin_protocol_handler.h>

Public Types

typedef t_connection_context connection_context
 
typedef protocl_handler_config< t_connection_context > config_type
 

Public Member Functions

 protocol_handler (net_utils::i_service_endpoint *psnd_hndlr, config_type &config, t_connection_context &conn_context)
 
virtual ~protocol_handler ()
 
virtual bool handle_recv (const void *ptr, size_t cb)
 
bool after_init_connection ()
 

Detailed Description

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

Definition at line 52 of file levin_protocol_handler.h.

Member Typedef Documentation

◆ config_type

template<class t_connection_context = net_utils::connection_context_base>
typedef protocl_handler_config<t_connection_context> epee::levin::protocol_handler< t_connection_context >::config_type

Definition at line 56 of file levin_protocol_handler.h.

◆ connection_context

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

Definition at line 55 of file levin_protocol_handler.h.

Constructor & Destructor Documentation

◆ protocol_handler()

template<class t_connection_context >
epee::levin::protocol_handler< t_connection_context >::protocol_handler ( net_utils::i_service_endpoint psnd_hndlr,
config_type config,
t_connection_context &  conn_context 
)

Definition at line 81 of file levin_protocol_handler.h.

81  :
82  m_config(config),
83  m_conn_context(conn_context),
84  m_psnd_hndlr(psnd_hndlr),
85  m_state(conn_state_reading_head),
86  m_current_head(bucket_head())
87  {}

◆ ~protocol_handler()

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

Definition at line 59 of file levin_protocol_handler.h.

59 {}

Member Function Documentation

◆ after_init_connection()

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

Definition at line 63 of file levin_protocol_handler.h.

63 {return true;}

◆ handle_recv()

template<class t_connection_context >
bool epee::levin::protocol_handler< t_connection_context >::handle_recv ( const void *  ptr,
size_t  cb 
)
virtual

Definition at line 90 of file levin_protocol_handler.h.

91  {
92  if(!m_config.m_pcommands_handler)
93  {
94  LOG_ERROR_CC(m_conn_context, "Command handler not set!");
95  return false;
96  }
97  m_cach_in_buffer.append((const char*)ptr, cb);
98 
99  bool is_continue = true;
100  while(is_continue)
101  {
102  switch(m_state)
103  {
104  case conn_state_reading_head:
105  if(m_cach_in_buffer.size() < sizeof(bucket_head))
106  {
107  if(m_cach_in_buffer.size() >= sizeof(uint64_t) && *((uint64_t*)m_cach_in_buffer.data()) != SWAP64LE(LEVIN_SIGNATURE))
108  {
109  LOG_ERROR_CC(m_conn_context, "Signature mismatch on accepted connection");
110  return false;
111  }
112  is_continue = false;
113  break;
114  }
115  {
116 #if BYTE_ORDER == LITTLE_ENDIAN
117  bucket_head &phead = *(bucket_head*)m_cach_in_buffer.data();
118 #else
119  bucket_head phead = *(bucket_head*)m_cach_in_buffer.data();
120  phead.m_signature = SWAP64LE(phead.m_signature);
121  phead.m_cb = SWAP64LE(phead.m_cb);
122  phead.m_command = SWAP32LE(phead.m_command);
123  phead.m_return_code = SWAP32LE(phead.m_return_code);
124  phead.m_reservedA = SWAP32LE(phead.m_reservedA);
125  phead.m_reservedB = SWAP32LE(phead.m_reservedB);
126 #endif
127  if(LEVIN_SIGNATURE != phead.m_signature)
128  {
129  LOG_ERROR_CC(m_conn_context, "Signature mismatch on accepted connection");
130  return false;
131  }
132  m_current_head = phead;
133  }
134  m_cach_in_buffer.erase(0, sizeof(bucket_head));
135  m_state = conn_state_reading_body;
136  break;
137  case conn_state_reading_body:
138  if(m_cach_in_buffer.size() < m_current_head.m_cb)
139  {
140  is_continue = false;
141  break;
142  }
143  {
144  std::string buff_to_invoke;
145  if(m_cach_in_buffer.size() == m_current_head.m_cb)
146  buff_to_invoke.swap(m_cach_in_buffer);
147  else
148  {
149  buff_to_invoke.assign(m_cach_in_buffer, 0, (std::string::size_type)m_current_head.m_cb);
150  m_cach_in_buffer.erase(0, (std::string::size_type)m_current_head.m_cb);
151  }
152 
153 
154  if(m_current_head.m_have_to_return_data)
155  {
156  std::string return_buff;
157  m_current_head.m_return_code = m_config.m_pcommands_handler->invoke(m_current_head.m_command, buff_to_invoke, return_buff, m_conn_context);
158  m_current_head.m_cb = return_buff.size();
159  m_current_head.m_have_to_return_data = false;
160  std::string send_buff((const char*)&m_current_head, sizeof(m_current_head));
161  send_buff += return_buff;
162 
163  if(!m_psnd_hndlr->do_send(send_buff.data(), send_buff.size()))
164  return false;
165 
166  }
167  else
168  m_config.m_pcommands_handler->notify(m_current_head.m_command, buff_to_invoke, m_conn_context);
169  }
170  m_state = conn_state_reading_head;
171  break;
172  default:
173  LOG_ERROR_CC(m_conn_context, "Undefined state in levin_server_impl::connection_handler, m_state=" << m_state);
174  return false;
175  }
176  }
177 
178  return true;
179  }
#define LEVIN_SIGNATURE
Definition: levin_base.h:34
::std::string string
Definition: gtest-port.h:1097
unsigned __int64 uint64_t
Definition: stdint.h:136
levin_commands_handler< t_connection_context > * m_pcommands_handler
#define LOG_ERROR_CC(ct, message)
#define SWAP64LE
Definition: int-util.h:252
virtual bool do_send(const void *ptr, size_t cb)=0
#define SWAP32LE
Definition: int-util.h:244

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