| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifndef _LEVIN_PROTOCOL_HANDLER_H_ |
| 30 |
#define _LEVIN_PROTOCOL_HANDLER_H_ |
| 31 |
|
| 32 |
#include <boost/uuid/uuid_generators.hpp> |
| 33 |
#include "levin_base.h" |
| 34 |
#include "int-util.h" |
| 35 |
|
| 36 |
#undef MONERO_DEFAULT_LOG_CATEGORY |
| 37 |
#define MONERO_DEFAULT_LOG_CATEGORY "net" |
| 38 |
|
| 39 |
namespace epee |
| 40 |
{ |
| 41 |
namespace levin |
| 42 |
{ |
| 43 |
template<class t_connection_context = net_utils::connection_context_base> |
| 44 |
struct protocl_handler_config |
| 45 |
{ |
| 46 |
levin_commands_handler<t_connection_context>* m_pcommands_handler; |
| 47 |
void (*m_pcommands_handler_destroy)(levin_commands_handler<t_connection_context>*); |
| 48 |
~protocl_handler_config() { if (m_pcommands_handler && m_pcommands_handler_destroy) (*m_pcommands_handler_destroy)(m_pcommands_handler); } |
| 49 |
}; |
| 50 |
|
| 51 |
template<class t_connection_context = net_utils::connection_context_base> |
| 52 |
class protocol_handler |
| 53 |
{ |
| 54 |
public: |
| 55 |
typedef t_connection_context connection_context; |
| 56 |
typedef protocl_handler_config<t_connection_context> config_type; |
| 57 |
|
| 58 |
protocol_handler(net_utils::i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context); |
| 59 |
virtual ~protocol_handler(){} |
| 60 |
|
| 61 |
virtual bool handle_recv(const void* ptr, size_t cb); |
| 62 |
|
| 63 |
bool after_init_connection(){return true;} |
| 64 |
private: |
| 65 |
enum connection_data_state |
| 66 |
{ |
| 67 |
conn_state_reading_head, |
| 68 |
conn_state_reading_body |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
config_type& m_config; |
| 73 |
t_connection_context& m_conn_context; |
| 74 |
net_utils::i_service_endpoint* m_psnd_hndlr; |
| 75 |
std::string m_cach_in_buffer; |
| 76 |
connection_data_state m_state; |
| 77 |
bucket_head m_current_head; |
| 78 |
}; |
| 79 |
|
| 80 |
template<class t_connection_context> |
| 81 |
protocol_handler<t_connection_context>::protocol_handler(net_utils::i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context): |
| 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 |
{} |
| 88 |
|
| 89 |
template<class t_connection_context> |
| 90 |
bool protocol_handler<t_connection_context>::handle_recv(const void* ptr, size_t cb) |
| 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 = 0; |
| 160 |
|
| 161 |
return_buff.insert(0, (const char*)&m_current_head, sizeof(m_current_head)); |
| 162 |
if(!m_psnd_hndlr->do_send(byte_slice{std::move(return_buff)})) |
| 163 |
return false; |
| 164 |
|
| 165 |
} |
| 166 |
else |
| 167 |
m_config.m_pcommands_handler->notify(m_current_head.m_command, buff_to_invoke, m_conn_context); |
| 168 |
} |
| 169 |
m_state = conn_state_reading_head; |
| 170 |
break; |
| 171 |
default: |
| 172 |
LOG_ERROR_CC(m_conn_context, "Undefined state in levin_server_impl::connection_handler, m_state=" << m_state); |
| 173 |
return false; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
#endif |
| 193 |
|