Skip to content

Commit abe9b59

Browse files
committed
Adding support for socket options to be passed in the constructor via Boost.Parameter enabled constructor.
Needs documentation!;
1 parent 2b1870e commit abe9b59

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

boost/network/protocol/http/parameters.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// http://www.boost.org/LICENSE_1_0.txt)
88

99
#if !defined(BOOST_PARAMETER_MAX_ARITY)
10-
#define BOOST_PARAMETER_MAX_ARITY 6
10+
#define BOOST_PARAMETER_MAX_ARITY 16
1111
#endif
1212
#include <boost/parameter.hpp>
1313

boost/network/protocol/http/server.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ namespace boost { namespace network { namespace http {
4747
(in_out(handler), (Handler &)))
4848
(optional
4949
(in_out(io_service), (boost::asio::io_service &))
50-
(socket_options, *))
50+
(reuse_address, (bool))
51+
(report_aborted, (bool))
52+
(receive_buffer_size, (int))
53+
(send_buffer_size, (int))
54+
(receive_low_watermark, (int))
55+
(send_low_watermark, (int))
56+
(non_blocking_io, (int))
57+
(linger, (bool))
58+
(linger_timeout, (int)))
5159
)
5260
};
5361

@@ -66,7 +74,15 @@ namespace boost { namespace network { namespace http {
6674
(in_out(thread_pool), (utils::thread_pool&)))
6775
(optional
6876
(in_out(io_service), (boost::asio::io_service&))
69-
(socket_options, *))
77+
(reuse_address, (bool))
78+
(report_aborted, (bool))
79+
(receive_buffer_size, (int))
80+
(send_buffer_size, (int))
81+
(receive_low_watermark, (int))
82+
(send_low_watermark, (int))
83+
(non_blocking_io, (int))
84+
(linger, (bool))
85+
(linger_timeout, (int)))
7086
)
7187
};
7288

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ namespace boost { namespace network { namespace http {
1414
BOOST_PARAMETER_NAME(port)
1515
BOOST_PARAMETER_NAME(handler)
1616
BOOST_PARAMETER_NAME(thread_pool)
17-
BOOST_PARAMETER_NAME(socket_options)
17+
18+
BOOST_PARAMETER_NAME(reuse_address)
19+
BOOST_PARAMETER_NAME(report_aborted)
20+
BOOST_PARAMETER_NAME(receive_buffer_size)
21+
BOOST_PARAMETER_NAME(send_buffer_size)
22+
BOOST_PARAMETER_NAME(receive_low_watermark)
23+
BOOST_PARAMETER_NAME(send_low_watermark)
24+
BOOST_PARAMETER_NAME(non_blocking_io)
25+
BOOST_PARAMETER_NAME(linger)
26+
BOOST_PARAMETER_NAME(linger_timeout)
1827

1928
} /* http */
2029

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,46 @@
2121

2222
namespace boost { namespace network { namespace http {
2323

24+
struct socket_options_base {
25+
protected:
26+
asio::socket_base::reuse_address acceptor_reuse_address;
27+
asio::socket_base::enable_connection_aborted acceptor_report_aborted;
28+
asio::socket_base::receive_buffer_size receive_buffer_size;
29+
asio::socket_base::send_buffer_size send_buffer_size;
30+
asio::socket_base::receive_low_watermark receive_low_watermark;
31+
asio::socket_base::send_low_watermark send_low_watermark;
32+
asio::socket_base::non_blocking_io non_blocking_io;
33+
asio::socket_base::linger linger;
34+
35+
template <class ArgPack>
36+
socket_options_base(ArgPack const & args)
37+
: acceptor_reuse_address(args[_reuse_address|false])
38+
, acceptor_report_aborted(args[_report_aborted|false])
39+
, receive_buffer_size(args[_receive_buffer_size|1024])
40+
, send_buffer_size(args[_send_buffer_size|1024])
41+
, receive_low_watermark(args[_receive_low_watermark|512])
42+
, send_low_watermark(args[_send_low_watermark|512])
43+
, non_blocking_io(args[_non_blocking_io|true])
44+
, linger(args[_linger|true], args[_linger_timeout|0])
45+
{}
46+
47+
void acceptor_options(boost::asio::ip::tcp::acceptor & acceptor) {
48+
acceptor.set_option(acceptor_reuse_address);
49+
acceptor.set_option(acceptor_report_aborted);
50+
}
51+
52+
void socket_options(boost::asio::ip::tcp::socket & socket) {
53+
socket.set_option(receive_buffer_size);
54+
socket.set_option(receive_low_watermark);
55+
socket.set_option(send_buffer_size);
56+
socket.set_option(send_low_watermark);
57+
socket.io_control(non_blocking_io);
58+
socket.set_option(linger);
59+
}
60+
};
61+
2462
template <class Tag, class Handler>
25-
struct sync_server_base : server_storage_base {
63+
struct sync_server_base : server_storage_base, socket_options_base {
2664
typedef typename string<Tag>::type string_type;
2765
typedef basic_request<Tag> request;
2866
typedef basic_response<Tag> response;
@@ -39,6 +77,7 @@ namespace boost { namespace network { namespace http {
3977
server_storage_base::no_io_service,
4078
server_storage_base::has_io_service
4179
>::type())
80+
, socket_options_base(args)
4281
, handler_(args[_handler])
4382
, address_(args[_address])
4483
, port_(args[_port])
@@ -75,6 +114,7 @@ namespace boost { namespace network { namespace http {
75114

76115
void handle_accept(boost::system::error_code const & ec) {
77116
if (!ec) {
117+
socket_options_base::socket_options(new_connection->socket());
78118
new_connection->start();
79119
new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));
80120
acceptor_.async_accept(new_connection->socket(),
@@ -90,6 +130,7 @@ namespace boost { namespace network { namespace http {
90130
tcp::endpoint endpoint = *resolver.resolve(query);
91131
acceptor_.open(endpoint.protocol());
92132
acceptor_.bind(endpoint);
133+
socket_options_base::acceptor_options(acceptor_);
93134
acceptor_.listen();
94135
listening_ = true;
95136
new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));

libs/network/test/http/server_constructor_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,31 @@ BOOST_AUTO_TEST_CASE(with_io_service_parameter) {
5454
}
5555

5656
BOOST_AUTO_TEST_CASE(with_socket_options_parameter) {
57+
dummy_sync_handler sync_handler;
58+
dummy_async_handler async_handler;
59+
util::thread_pool pool;
60+
61+
BOOST_CHECK_NO_THROW(sync_server sync_instance("127.0.0.1", "80", sync_handler,
62+
http::_reuse_address=true,
63+
http::_report_aborted=true,
64+
http::_receive_buffer_size=4096,
65+
http::_send_buffer_size=4096,
66+
http::_receive_low_watermark=1024,
67+
http::_send_low_watermark=1024,
68+
http::_non_blocking_io=true,
69+
http::_linger=true,
70+
http::_linger_timeout=0
71+
));
72+
BOOST_CHECK_NO_THROW(async_server async_instance("127.0.0.1", "80", async_handler, pool,
73+
http::_reuse_address=true,
74+
http::_report_aborted=true,
75+
http::_receive_buffer_size=4096,
76+
http::_send_buffer_size=4096,
77+
http::_receive_low_watermark=1024,
78+
http::_send_low_watermark=1024,
79+
http::_non_blocking_io=true,
80+
http::_linger=true,
81+
http::_linger_timeout=0
82+
));
83+
5784
}

0 commit comments

Comments
 (0)