29 #ifndef _LEVIN_PROTOCOL_HANDLER_H_ 30 #define _LEVIN_PROTOCOL_HANDLER_H_ 32 #include <boost/uuid/uuid_generators.hpp> 36 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY 37 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net" 43 template<
class t_connection_context = net_utils::connection_context_base>
51 template<
class t_connection_context = net_utils::connection_context_base>
61 virtual bool handle_recv(
const void* ptr,
size_t cb);
65 enum connection_data_state
67 conn_state_reading_head,
68 conn_state_reading_body
73 t_connection_context& m_conn_context;
76 connection_data_state m_state;
77 bucket_head m_current_head;
80 template<
class t_connection_context>
83 m_conn_context(conn_context),
84 m_psnd_hndlr(psnd_hndlr),
85 m_state(conn_state_reading_head),
89 template<
class t_connection_context>
94 LOG_ERROR_CC(m_conn_context,
"Command handler not set!");
97 m_cach_in_buffer.append((
const char*)ptr, cb);
99 bool is_continue =
true;
104 case conn_state_reading_head:
109 LOG_ERROR_CC(m_conn_context,
"Signature mismatch on accepted connection");
116 #if BYTE_ORDER == LITTLE_ENDIAN 129 LOG_ERROR_CC(m_conn_context,
"Signature mismatch on accepted connection");
132 m_current_head = phead;
135 m_state = conn_state_reading_body;
137 case conn_state_reading_body:
138 if(m_cach_in_buffer.size() < m_current_head.m_cb)
145 if(m_cach_in_buffer.size() == m_current_head.m_cb)
146 buff_to_invoke.swap(m_cach_in_buffer);
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);
154 if(m_current_head.m_have_to_return_data)
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;
163 if(!m_psnd_hndlr->do_send(send_buff.data(), send_buff.size()))
168 m_config.m_pcommands_handler->notify(m_current_head.m_command, buff_to_invoke, m_conn_context);
170 m_state = conn_state_reading_head;
173 LOG_ERROR_CC(m_conn_context,
"Undefined state in levin_server_impl::connection_handler, m_state=" << m_state);
193 #endif //_LEVIN_PROTOCOL_HANDLER_H_
protocl_handler_config< t_connection_context > config_type
void(* m_pcommands_handler_destroy)(levin_commands_handler< t_connection_context > *)
~protocl_handler_config()
protocol_handler(net_utils::i_service_endpoint *psnd_hndlr, config_type &config, t_connection_context &conn_context)
unsigned __int64 uint64_t
levin_commands_handler< t_connection_context > * m_pcommands_handler
#define LOG_ERROR_CC(ct, message)
bool after_init_connection()
TProtocol::config_type m_config
virtual ~protocol_handler()
t_connection_context connection_context
virtual bool handle_recv(const void *ptr, size_t cb)