Skip to content

Commit d3b68c9

Browse files
committed
Remove dependency on Boost.Parameter from server.
1 parent 6da32c4 commit d3b68c9

File tree

10 files changed

+99
-258
lines changed

10 files changed

+99
-258
lines changed

boost/network/protocol/http/server.hpp

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <boost/network/protocol/http/request.hpp>
1414
#include <boost/network/protocol/http/server/sync_server.hpp>
1515
#include <boost/network/protocol/http/server/async_server.hpp>
16-
#include <boost/network/protocol/http/server/parameters.hpp>
1716

1817
namespace boost { namespace network { namespace http {
1918

@@ -40,52 +39,21 @@ namespace boost { namespace network { namespace http {
4039
struct server : server_base<tags::http_server, Handler>::type {
4140
typedef typename server_base<tags::http_server, Handler>::type
4241
server_base;
42+
typedef server_options<tags::http_server, Handler> options;
4343

44-
BOOST_PARAMETER_CONSTRUCTOR(
45-
server, (server_base), tag,
46-
(required
47-
(address, (typename server_base::string_type const &))
48-
(port, (typename server_base::string_type const &))
49-
(in_out(handler), (Handler &)))
50-
(optional
51-
(in_out(io_service), (boost::asio::io_service &))
52-
(reuse_address, (bool))
53-
(report_aborted, (bool))
54-
(receive_buffer_size, (int))
55-
(send_buffer_size, (int))
56-
(receive_low_watermark, (int))
57-
(send_low_watermark, (int))
58-
(non_blocking_io, (int))
59-
(linger, (bool))
60-
(linger_timeout, (int)))
61-
)
44+
explicit server(options const &options)
45+
: server_base(options) {}
6246
};
6347

6448
template <class Handler>
6549
struct async_server : server_base<tags::http_async_server, Handler>::type
6650
{
6751
typedef typename server_base<tags::http_async_server, Handler>::type
6852
server_base;
53+
typedef server_options<tags::http_async_server, Handler> options;
6954

70-
BOOST_PARAMETER_CONSTRUCTOR(
71-
async_server, (server_base), tag,
72-
(required
73-
(address, (typename server_base::string_type const &))
74-
(port, (typename server_base::string_type const &))
75-
(in_out(handler), (Handler&))
76-
(in_out(thread_pool), (utils::thread_pool&)))
77-
(optional
78-
(in_out(io_service), (boost::asio::io_service&))
79-
(reuse_address, (bool))
80-
(report_aborted, (bool))
81-
(receive_buffer_size, (int))
82-
(send_buffer_size, (int))
83-
(receive_low_watermark, (int))
84-
(send_low_watermark, (int))
85-
(non_blocking_io, (bool))
86-
(linger, (bool))
87-
(linger_timeout, (int)))
88-
)
55+
explicit async_server(options const &options)
56+
: server_base(options) {}
8957
};
9058

9159
} // namespace http

boost/network/protocol/http/server/async_server.hpp

Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,20 @@ namespace boost { namespace network { namespace http {
2323
typedef typename boost::network::http::response_header<Tag>::type response_header;
2424
typedef async_connection<Tag,Handler> connection;
2525
typedef shared_ptr<connection> connection_ptr;
26-
typedef boost::unique_lock<boost::mutex> scoped_mutex_lock;
26+
typedef boost::unique_lock<boost::mutex> scoped_mutex_lock;
2727

28-
template <class ArgPack>
29-
async_server_base(ArgPack const & args)
30-
: server_storage_base(args,
31-
typename mpl::if_<
32-
is_same<
33-
typename parameter::value_type<ArgPack, tag::io_service, void>::type,
34-
void
35-
>,
36-
server_storage_base::no_io_service,
37-
server_storage_base::has_io_service
38-
>::type())
39-
, socket_options_base(args)
40-
, handler(args[_handler])
41-
, address_(args[_address])
42-
, port_(args[_port])
43-
, thread_pool(args[_thread_pool])
28+
explicit async_server_base(server_options<Tag, Handler> const &options)
29+
: server_storage_base(options)
30+
, socket_options_base(options)
31+
, handler(options.handler())
32+
, address_(options.address())
33+
, port_(options.port())
34+
, thread_pool(options.thread_pool() ? options.thread_pool() : boost::make_shared<utils::thread_pool>())
4435
, acceptor(server_storage_base::service_)
4536
, stopping(false)
4637
, new_connection()
4738
, listening_mutex_()
48-
, stopping_mutex_()
39+
, stopping_mutex_()
4940
, listening(false)
5041
{}
5142

@@ -57,19 +48,19 @@ namespace boost { namespace network { namespace http {
5748
void stop() {
5849
// stop accepting new requests and let all the existing
5950
// handlers finish.
60-
scoped_mutex_lock listening_lock(listening_mutex_);
61-
if (listening) { // we dont bother stopping if we arent currently listening
62-
scoped_mutex_lock stopping_lock(stopping_mutex_);
63-
stopping = true;
64-
system::error_code ignored;
65-
acceptor.close(ignored);
66-
listening = false;
67-
service_.post(boost::bind(&async_server_base::handle_stop, this));
68-
}
51+
scoped_mutex_lock listening_lock(listening_mutex_);
52+
if (listening) { // we dont bother stopping if we arent currently listening
53+
scoped_mutex_lock stopping_lock(stopping_mutex_);
54+
stopping = true;
55+
system::error_code ignored;
56+
acceptor.close(ignored);
57+
listening = false;
58+
service_.post(boost::bind(&async_server_base::handle_stop, this));
59+
}
6960
}
7061

7162
void listen() {
72-
scoped_mutex_lock listening_lock(listening_mutex_);
63+
scoped_mutex_lock listening_lock(listening_mutex_);
7364
BOOST_NETWORK_MESSAGE("Listening on " << address_ << ':' << port_);
7465
if (!listening) start_listening(); // we only initialize our acceptor/sockets if we arent already listening
7566
if (!listening) {
@@ -81,53 +72,44 @@ namespace boost { namespace network { namespace http {
8172
private:
8273
Handler & handler;
8374
string_type address_, port_;
84-
utils::thread_pool & thread_pool;
75+
boost::shared_ptr<utils::thread_pool> thread_pool;
8576
asio::ip::tcp::acceptor acceptor;
8677
bool stopping;
8778
connection_ptr new_connection;
8879
boost::mutex listening_mutex_;
89-
boost::mutex stopping_mutex_;
80+
boost::mutex stopping_mutex_;
9081
bool listening;
91-
92-
void handle_stop() {
93-
scoped_mutex_lock stopping_lock(stopping_mutex_);
94-
if (stopping) service_.stop(); // a user may have started listening again before the stop command is reached
95-
}
82+
83+
void handle_stop() {
84+
scoped_mutex_lock stopping_lock(stopping_mutex_);
85+
if (stopping) service_.stop(); // a user may have started listening again before the stop command is reached
86+
}
9687

9788
void handle_accept(boost::system::error_code const & ec) {
98-
{
99-
scoped_mutex_lock stopping_lock(stopping_mutex_);
100-
if (stopping) return; // we dont want to add another handler instance, and we dont want to know about errors for a socket we dont need anymore
101-
}
89+
{
90+
scoped_mutex_lock stopping_lock(stopping_mutex_);
91+
if (stopping) return; // we dont want to add another handler instance, and we dont want to know about errors for a socket we dont need anymore
92+
}
10293
if (!ec) {
10394
socket_options_base::socket_options(new_connection->socket());
10495
new_connection->start();
10596
new_connection.reset(
106-
new connection(
107-
service_
108-
, handler
109-
, thread_pool
110-
)
111-
);
112-
acceptor.async_accept(new_connection->socket(),
113-
boost::bind(
114-
&async_server_base<Tag,Handler>::handle_accept
115-
, this
116-
, boost::asio::placeholders::error
117-
)
118-
);
97+
new connection(service_, handler, *thread_pool));
98+
acceptor.async_accept(
99+
new_connection->socket(),
100+
boost::bind(
101+
&async_server_base<Tag,Handler>::handle_accept,
102+
this,
103+
boost::asio::placeholders::error));
119104
} else {
120105
BOOST_NETWORK_MESSAGE("Error accepting connection, reason: " << ec);
121106
}
122107
}
123108

124109
void start_listening() {
125110
using boost::asio::ip::tcp;
126-
127111
system::error_code error;
128-
129-
service_.reset(); // this allows repeated cycles of run -> stop -> run
130-
112+
service_.reset(); // this allows repeated cycles of run -> stop -> run
131113
tcp::resolver resolver(service_);
132114
tcp::resolver::query query(address_, port_);
133115
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
@@ -152,15 +134,15 @@ namespace boost { namespace network { namespace http {
152134
BOOST_NETWORK_MESSAGE("Error listening on socket: '" << error << "' on " << address_ << ":" << port_);
153135
return;
154136
}
155-
new_connection.reset(new connection(service_, handler, thread_pool));
137+
new_connection.reset(new connection(service_, handler, *thread_pool));
156138
acceptor.async_accept(new_connection->socket(),
157139
boost::bind(
158140
&async_server_base<Tag,Handler>::handle_accept
159141
, this
160142
, boost::asio::placeholders::error));
161143
listening = true;
162-
scoped_mutex_lock stopping_lock(stopping_mutex_);
163-
stopping = false; // if we were in the process of stopping, we revoke that command and continue listening
144+
scoped_mutex_lock stopping_lock(stopping_mutex_);
145+
stopping = false; // if we were in the process of stopping, we revoke that command and continue listening
164146
BOOST_NETWORK_MESSAGE("Now listening on socket: '" << address_ << ":" << port_ << "'");
165147
}
166148
};

boost/network/protocol/http/server/parameters.hpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

boost/network/protocol/http/server/socket_options_base.hpp

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// (See accompanying file LICENSE_1_0.txt or copy at
77
// http://www.boost.org/LICENSE_1_0.txt)
88

9-
#include <boost/network/protocol/http/server/parameters.hpp>
10-
#include <boost/optional.hpp>
119
#include <boost/utility/in_place_factory.hpp>
1210

1311
namespace boost { namespace network { namespace http {
@@ -23,18 +21,17 @@ namespace boost { namespace network { namespace http {
2321
asio::socket_base::non_blocking_io non_blocking_io;
2422
asio::socket_base::linger linger;
2523

26-
template <class ArgPack>
27-
socket_options_base(ArgPack const & args)
28-
: acceptor_reuse_address(args[_reuse_address|false])
29-
, acceptor_report_aborted(args[_report_aborted|false])
30-
, non_blocking_io(args[_non_blocking_io|true])
31-
, linger(args[_linger|true], args[_linger_timeout|0])
32-
{
33-
set_optional(receive_buffer_size, args, _receive_buffer_size);
34-
set_optional(send_buffer_size, args, _send_buffer_size);
35-
set_optional(receive_low_watermark, args, _receive_low_watermark);
36-
set_optional(send_low_watermark, args, _send_low_watermark);
37-
}
24+
template <class Tag, class Handler>
25+
explicit socket_options_base(server_options<Tag, Handler> const &options)
26+
: acceptor_reuse_address(options.reuse_address())
27+
, acceptor_report_aborted(options.report_aborted())
28+
, receive_buffer_size(options.receive_buffer_size())
29+
, send_buffer_size(options.send_buffer_size())
30+
, receive_low_watermark(options.receive_low_watermark())
31+
, send_low_watermark(options.send_low_watermark())
32+
, non_blocking_io(options.non_blocking_io())
33+
, linger(options.linger(), options.linger_timeout())
34+
{}
3835

3936
void acceptor_options(boost::asio::ip::tcp::acceptor & acceptor) {
4037
acceptor.set_option(acceptor_reuse_address);
@@ -50,35 +47,6 @@ namespace boost { namespace network { namespace http {
5047
if (send_buffer_size) socket.set_option(*send_buffer_size, ignored);
5148
if (send_low_watermark) socket.set_option(*send_low_watermark, ignored);
5249
}
53-
54-
private:
55-
56-
template <class Optional, class Args, class Keyword>
57-
typename boost::enable_if<
58-
mpl::not_<
59-
boost::is_same<
60-
typename boost::parameter::value_type<Args, Keyword, void>::type
61-
, void
62-
>
63-
>
64-
, void
65-
>::type
66-
set_optional(Optional & option, Args const & args, Keyword const & keyword) {
67-
option = in_place<typename Optional::value_type>(args[keyword]);
68-
}
69-
70-
template <class Optional, class Args, class Keyword>
71-
typename boost::enable_if<
72-
boost::is_same<
73-
typename boost::parameter::value_type<Args, Keyword, void>::type
74-
, void
75-
>
76-
, void
77-
>::type
78-
set_optional(Optional &, Args const &, Keyword const &) {
79-
// do nothing
80-
}
81-
8250
};
8351

8452
} /* http */

boost/network/protocol/http/server/storage_base.hpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,23 @@
66
// (See accompanying file LICENSE_1_0.txt or copy at
77
// http://www.boost.org/LICENSE_1_0.txt)
88

9-
#include <boost/network/protocol/http/server/parameters.hpp>
9+
#include <boost/shared_ptr.hpp>
10+
#include <boost/make_shared.hpp>
11+
#include <boost/network/protocol/http/server/options.hpp>
1012

1113
namespace boost { namespace network { namespace http {
1214

1315
struct server_storage_base {
1416
struct no_io_service {};
1517
struct has_io_service {};
1618
protected:
17-
template <class ArgPack>
18-
server_storage_base(ArgPack const & /* args */, no_io_service)
19-
: self_service_(new boost::asio::io_service())
19+
template <class Tag, class Handler>
20+
explicit server_storage_base(server_options<Tag, Handler> const & options)
21+
: self_service_(options.io_service()? options.io_service() : boost::make_shared<boost::asio::io_service>())
2022
, service_(*self_service_)
2123
{}
2224

23-
template <class ArgPack>
24-
server_storage_base(ArgPack const & args, has_io_service)
25-
: self_service_(0)
26-
, service_(args[_io_service])
27-
{}
28-
29-
~server_storage_base() {
30-
delete self_service_;
31-
self_service_ = 0;
32-
}
33-
34-
asio::io_service * self_service_;
25+
boost::shared_ptr<asio::io_service> self_service_;
3526
asio::io_service & service_;
3627
};
3728

0 commit comments

Comments
 (0)