Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
http_server_impl_base.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
30
#pragma once
31
32
33
#include <boost/thread.hpp>
34
#include <boost/bind/bind.hpp>
35
36
#include "
net/abstract_tcp_server2.h
"
37
#include "
http_protocol_handler.h
"
38
#include "
net/http_server_handlers_map2.h
"
39
40
#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
41
#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net.http"
42
43
namespace
epee
44
{
45
46
template
<
class
t_child_
class
,
class
t_connection_context = epee::net_utils::connection_context_base>
47
class
http_server_impl_base
:
public
net_utils::http::i_http_server_handler
<t_connection_context>
48
{
49
50
public
:
51
http_server_impl_base
()
52
:
m_net_server
(
epee
::
net_utils
::e_connection_type_RPC)
53
{}
54
55
explicit
http_server_impl_base
(boost::asio::io_service& external_io_service)
56
:
m_net_server
(external_io_service)
57
{}
58
59
bool
init
(std::function<
void
(
size_t
,
uint8_t
*)> rng,
const
std::string& bind_port =
"0"
,
const
std::string& bind_ip =
"0.0.0.0"
,
60
std::vector<std::string> access_control_origins = std::vector<std::string>(),
61
boost::optional<net_utils::http::login> user = boost::none,
62
net_utils::ssl_options_t
ssl_options =
net_utils::ssl_support_t::e_ssl_support_autodetect
)
63
{
64
65
//set self as callback handler
66
m_net_server
.get_config_object().m_phandler =
static_cast<
t_child_class*
>
(
this
);
67
m_net_server
.get_config_object().rng = std::move(rng);
68
69
//here set folder for hosting reqests
70
m_net_server
.get_config_object().m_folder =
""
;
71
72
//set access control allow origins if configured
73
std::sort(access_control_origins.begin(), access_control_origins.end());
74
m_net_server
.get_config_object().m_access_control_origins = std::move(access_control_origins);
75
76
m_net_server
.get_config_object().m_user = std::move(user);
77
78
MGINFO
(
"Binding on "
<< bind_ip <<
":"
<< bind_port);
79
bool
res
=
m_net_server
.init_server(bind_port, bind_ip, std::move(ssl_options));
80
if
(!
res
)
81
{
82
LOG_ERROR
(
"Failed to bind server"
);
83
return
false
;
84
}
85
return
true
;
86
}
87
88
bool
run
(
size_t
threads_count,
bool
wait =
true
)
89
{
90
//go to loop
91
MINFO
(
"Run net_service loop( "
<< threads_count <<
" threads)..."
);
92
// cn_slow_hash requires ~2MB stack; default thread stack (512KB on macOS)
93
// is insufficient and causes a stack overflow (SIGBUS/SIGSEGV) on arm64.
94
boost::thread::attributes attrs;
95
attrs.set_stack_size(8 * 1024 * 1024);
// 8 MB
96
if
(!
m_net_server
.run_server(threads_count, wait, attrs))
97
{
98
LOG_ERROR
(
"Failed to run net tcp server!"
);
99
}
100
101
if
(wait)
102
MINFO
(
"net_service loop stopped."
);
103
return
true
;
104
}
105
106
bool
deinit
()
107
{
108
return
m_net_server
.deinit_server();
109
}
110
111
bool
timed_wait_server_stop
(
uint64_t
ms)
112
{
113
return
m_net_server
.timed_wait_server_stop(ms);
114
}
115
116
bool
send_stop_signal
()
117
{
118
m_net_server
.send_stop_signal();
119
return
true
;
120
}
121
122
int
get_binded_port
()
123
{
124
return
m_net_server
.get_binded_port();
125
}
126
127
long
get_connections_count
()
const
128
{
129
return
m_net_server
.get_connections_count();
130
}
131
132
protected
:
133
net_utils::boosted_tcp_server<net_utils::http::http_custom_handler<t_connection_context>
>
m_net_server
;
134
};
135
}
abstract_tcp_server2.h
the connection templated-class for one peer connection
epee::http_server_impl_base::http_server_impl_base
http_server_impl_base(boost::asio::io_service &external_io_service)
Definition
http_server_impl_base.h:55
epee::http_server_impl_base::send_stop_signal
bool send_stop_signal()
Definition
http_server_impl_base.h:116
epee::http_server_impl_base::get_connections_count
long get_connections_count() const
Definition
http_server_impl_base.h:127
epee::http_server_impl_base::run
bool run(size_t threads_count, bool wait=true)
Definition
http_server_impl_base.h:88
epee::http_server_impl_base::get_binded_port
int get_binded_port()
Definition
http_server_impl_base.h:122
epee::http_server_impl_base::init
bool init(std::function< void(size_t, uint8_t *)> rng, const std::string &bind_port="0", const std::string &bind_ip="0.0.0.0", std::vector< std::string > access_control_origins=std::vector< std::string >(), boost::optional< net_utils::http::login > user=boost::none, net_utils::ssl_options_t ssl_options=net_utils::ssl_support_t::e_ssl_support_autodetect)
Definition
http_server_impl_base.h:59
epee::http_server_impl_base::timed_wait_server_stop
bool timed_wait_server_stop(uint64_t ms)
Definition
http_server_impl_base.h:111
epee::http_server_impl_base::http_server_impl_base
http_server_impl_base()
Definition
http_server_impl_base.h:51
epee::http_server_impl_base::m_net_server
net_utils::boosted_tcp_server< net_utils::http::http_custom_handler< t_connection_context > > m_net_server
Definition
http_server_impl_base.h:133
epee::http_server_impl_base::deinit
bool deinit()
Definition
http_server_impl_base.h:106
epee::net_utils::boosted_tcp_server
Definition
abstract_tcp_server2.h:209
epee::net_utils::ssl_options_t
Definition
net_ssl.h:74
res
const char * res
Definition
hmac_keccak.cpp:41
http_protocol_handler.h
http_server_handlers_map2.h
LOG_ERROR
#define LOG_ERROR(x)
Definition
misc_log_ex.h:98
MGINFO
#define MGINFO(x)
Definition
misc_log_ex.h:80
MINFO
#define MINFO(x)
Definition
misc_log_ex.h:75
epee::net_utils
Definition
gzip_encoding.h:40
epee::net_utils::ssl_support_t::e_ssl_support_autodetect
@ e_ssl_support_autodetect
Definition
net_ssl.h:49
epee
Definition
ado_db_helper.h:67
uint8_t
unsigned char uint8_t
Definition
stdint.h:124
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:136
epee::net_utils::http::i_http_server_handler
Definition
http_protocol_handler.h:152
contrib
epee
include
net
http_server_impl_base.h
Generated on
for Electroneum by
1.17.0