Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
protocol_switcher.h
Go to the documentation of this file.
1
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above copyright
9
// notice, this list of conditions and the following disclaimer in the
10
// documentation and/or other materials provided with the distribution.
11
// * Neither the name of the Andrey N. Sabelnikov nor the
12
// names of its contributors may be used to endorse or promote products
13
// derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
//
26
27
28
29
#ifndef _PROTOCOL_SWITCHER_H_
30
#define _PROTOCOL_SWITCHER_H_
31
32
#include "
levin_base.h
"
33
#include "http_server.h"
34
#include "
levin_protocol_handler.h
"
35
//#include "abstract_tcp_server.h"
36
37
namespace
epee
38
{
39
namespace
net_utils
40
{
41
struct
protocl_switcher_config
42
{
43
http::http_custom_handler::config_type
m_http_config
;
44
levin::protocol_handler::config_type
m_levin_config
;
45
};
46
47
48
struct
i_protocol_handler
49
{
50
virtual
bool
handle_recv
(
const
void
* ptr,
size_t
cb)=0;
51
};
52
53
template
<
class
t>
54
class
t_protocol_handler
:
public
i_protocol_handler
55
{
56
public
:
57
typedef
t
t_type
;
58
t_protocol_handler
(
i_service_endpoint
* psnd_hndlr,
typename
t_type::config_type&
config
,
const
connection_context& conn_context):m_hadler(psnd_hndlr,
config
, conn_context)
59
{}
60
private
:
61
bool
handle_recv(
const
void
* ptr,
size_t
cb)
62
{
63
return
m_hadler.handle_recv(ptr, cb);
64
}
65
t_type
m_hadler;
66
};
67
68
69
class
protocol_switcher
70
{
71
public
:
72
typedef
protocl_switcher_config
config_type
;
73
74
protocol_switcher
(
net_utils::i_service_endpoint
* psnd_hndlr,
config_type
&
config
,
const
net_utils::connection_context_base
& conn_context);
75
virtual
~protocol_switcher
(){}
76
77
virtual
bool
handle_recv
(
const
void
* ptr,
size_t
cb);
78
79
bool
after_init_connection
(){
return
true
;}
80
private
:
81
t_protocol_handler<http::http_custom_handler>
m_http_handler;
82
t_protocol_handler<levin::protocol_handler>
m_levin_handler;
83
i_protocol_handler
* pcurrent_handler;
84
85
std::string m_cached_buff;
86
};
87
88
protocol_switcher::protocol_switcher
(
net_utils::i_service_endpoint
* psnd_hndlr,
config_type
&
config
,
const
net_utils::connection_context_base
& conn_context):m_http_handler(psnd_hndlr,
config
.m_http_config, conn_context), m_levin_handler(psnd_hndlr,
config
.m_levin_config, conn_context), pcurrent_handler(NULL)
89
{}
90
91
bool
protocol_switcher::handle_recv
(
const
void
* ptr,
size_t
cb)
92
{
93
if
(pcurrent_handler)
94
return
pcurrent_handler->handle_recv(ptr, cb);
95
else
96
{
97
m_cached_buff.append((
const
char
*)ptr, cb);
98
if
(m_cached_buff.size() <
sizeof
(
uint64_t
))
99
return
true
;
100
101
if
(*((
uint64_t
*)&m_cached_buff[0]) ==
LEVIN_SIGNATURE
)
102
{
103
pcurrent_handler = &m_levin_handler;
104
return
pcurrent_handler->handle_recv(m_cached_buff.data(), m_cached_buff.size());
105
}
106
if
(m_cached_buff.substr(0, 4) ==
"GET "
|| m_cached_buff.substr(0, 4) ==
"POST"
)
107
{
108
pcurrent_handler = &m_http_handler;
109
return
pcurrent_handler->handle_recv(m_cached_buff.data(), m_cached_buff.size());
110
}
else
111
{
112
LOG_ERROR
(
"Wrong protocol accepted on port..."
);
113
return
false
;
114
}
115
}
116
117
return
true
;
118
}
119
}
120
}
121
#endif
//_PROTOCOL_SWITCHER_H_
epee::levin::protocol_handler::config_type
protocl_handler_config< t_connection_context > config_type
Definition
levin_protocol_handler.h:56
epee::net_utils::http::http_custom_handler::config_type
custum_handler_config< t_connection_context > config_type
Definition
http_protocol_handler.h:176
epee::net_utils::protocol_switcher::config_type
protocl_switcher_config config_type
Definition
protocol_switcher.h:72
epee::net_utils::protocol_switcher::protocol_switcher
protocol_switcher(net_utils::i_service_endpoint *psnd_hndlr, config_type &config, const net_utils::connection_context_base &conn_context)
Definition
protocol_switcher.h:88
epee::net_utils::protocol_switcher::handle_recv
virtual bool handle_recv(const void *ptr, size_t cb)
Definition
protocol_switcher.h:91
epee::net_utils::protocol_switcher::after_init_connection
bool after_init_connection()
Definition
protocol_switcher.h:79
epee::net_utils::protocol_switcher::~protocol_switcher
virtual ~protocol_switcher()
Definition
protocol_switcher.h:75
epee::net_utils::t_protocol_handler
Definition
protocol_switcher.h:55
epee::net_utils::t_protocol_handler::t_type
t t_type
Definition
protocol_switcher.h:57
epee::net_utils::t_protocol_handler::t_protocol_handler
t_protocol_handler(i_service_endpoint *psnd_hndlr, typename t_type::config_type &config, const connection_context &conn_context)
Definition
protocol_switcher.h:58
levin_base.h
LEVIN_SIGNATURE
#define LEVIN_SIGNATURE
Definition
levin_base.h:34
levin_protocol_handler.h
LOG_ERROR
#define LOG_ERROR(x)
Definition
misc_log_ex.h:98
config
Definition
cryptonote_config.h:194
epee::net_utils
Definition
gzip_encoding.h:40
epee
Definition
ado_db_helper.h:67
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:136
epee::net_utils::connection_context_base
Definition
net_utils_base.h:248
epee::net_utils::i_protocol_handler
Definition
protocol_switcher.h:49
epee::net_utils::i_protocol_handler::handle_recv
virtual bool handle_recv(const void *ptr, size_t cb)=0
epee::net_utils::i_service_endpoint
Definition
net_utils_base.h:323
epee::net_utils::protocl_switcher_config
Definition
protocol_switcher.h:42
epee::net_utils::protocl_switcher_config::m_http_config
http::http_custom_handler::config_type m_http_config
Definition
protocol_switcher.h:43
epee::net_utils::protocl_switcher_config::m_levin_config
levin::protocol_handler::config_type m_levin_config
Definition
protocol_switcher.h:44
contrib
epee
include
net
protocol_switcher.h
Generated on
for Electroneum by
1.17.0