Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b1870e

Browse files
committed
Using Boost.Parameter in the HTTP Server constructor interface.
1 parentf829f7d commit2b1870e

File tree

8 files changed

+171
-43
lines changed

8 files changed

+171
-43
lines changed

‎boost/network/protocol/http/client/parameters.hpp‎

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

9-
#include<boost/parameter.hpp>
9+
#include<boost/network/protocol/http/parameters.hpp>
1010

1111
namespaceboost {namespacenetwork {namespacehttp {
1212

1313
BOOST_PARAMETER_NAME(follow_redirects)
14-
BOOST_PARAMETER_NAME(io_service)
1514
BOOST_PARAMETER_NAME(cache_resolved)
1615

1716
}/* http*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_PARAMETERS_HPP_20101210
2+
#defineBOOST_NETWORK_PROTOCOL_HTTP_PARAMETERS_HPP_20101210
3+
4+
// Copyright 2010 Dean Michael Berris.
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
9+
#if !defined(BOOST_PARAMETER_MAX_ARITY)
10+
#defineBOOST_PARAMETER_MAX_ARITY6
11+
#endif
12+
#include<boost/parameter.hpp>
13+
14+
namespaceboost {namespacenetwork {namespacehttp {
15+
16+
BOOST_PARAMETER_NAME(io_service)
17+
18+
}/* http*/
19+
20+
}/* network*/
21+
22+
}/* boost*/
23+
24+
#endif/* BOOST_NETWORK_PROTOCOL_HTTP_PARAMETERS_HPP_20101210*/

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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>
1617

1718
namespaceboost {namespacenetwork {namespacehttp {
1819

@@ -38,11 +39,16 @@ namespace boost { namespace network { namespace http {
3839
typedeftypename server_base<tags::http_server, Handler>::type
3940
server_base;
4041

41-
server(typename server_base::string_typeconst & address,
42-
typename server_base::string_typeconst & port,
43-
Handler & handler)
44-
: server_base(address, port, handler) {}
45-
42+
BOOST_PARAMETER_CONSTRUCTOR(
43+
server, (server_base), tag,
44+
(required
45+
(address, (typename server_base::string_typeconst &))
46+
(port, (typename server_base::string_typeconst &))
47+
(in_out(handler), (Handler &)))
48+
(optional
49+
(in_out(io_service), (boost::asio::io_service &))
50+
(socket_options, *))
51+
)
4652
};
4753

4854
template<classHandler>
@@ -51,11 +57,17 @@ namespace boost { namespace network { namespace http {
5157
typedeftypename server_base<tags::http_async_server, Handler>::type
5258
server_base;
5359

54-
async_server(typename server_base::string_typeconst & address,
55-
typename server_base::string_typeconst & port,
56-
Handler & handler,
57-
utils::thread_pool & thread_pool)
58-
: server_base(address, port, handler, thread_pool) {}
60+
BOOST_PARAMETER_CONSTRUCTOR(
61+
async_server, (server_base), tag,
62+
(required
63+
(address, (typename server_base::string_typeconst &))
64+
(port, (typename server_base::string_typeconst &))
65+
(in_out(handler), (Handler&))
66+
(in_out(thread_pool), (utils::thread_pool&)))
67+
(optional
68+
(in_out(io_service), (boost::asio::io_service&))
69+
(socket_options, *))
70+
)
5971
};
6072

6173
}// namespace http

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,45 @@
77
// http://www.boost.org/LICENSE_1_0.txt)
88

99
#include<boost/network/protocol/http/server/async_connection.hpp>
10+
#include<boost/thread/mutex.hpp>
11+
#include<boost/network/protocol/http/server/storage_base.hpp>
1012
#include<boost/network/utils/thread_pool.hpp>
1113

1214
namespaceboost {namespacenetwork {namespacehttp {
1315

1416
template<classTag,classHandler>
15-
structasync_server_base {
17+
structasync_server_base: server_storage_base{
1618
typedef basic_request<Tag> request;
1719
typedef basic_response<Tag> response;
1820
typedeftypename string<Tag>::type string_type;
1921
typedeftypename boost::network::http::response_header<Tag>::type response_header;
2022
typedef async_connection<Tag,Handler> connection;
2123
typedef shared_ptr<connection> connection_ptr;
2224

23-
async_server_base(string_typeconst & address
24-
, string_typeconst & port
25-
, Handler & handler
26-
, utils::thread_pool & thread_pool)
27-
: handler(handler)
28-
, address_(address)
29-
, port_(port)
30-
, thread_pool(thread_pool)
31-
, self_service(new asio::io_service())
32-
, io_service()
33-
, acceptor(io_service)
25+
template<classArgPack>
26+
async_server_base(ArgPackconst & args)
27+
: server_storage_base(args,
28+
typename mpl::if_<
29+
is_same<
30+
typename parameter::value_type<ArgPack, tag::io_service,void>::type,
31+
void
32+
>,
33+
server_storage_base::no_io_service,
34+
server_storage_base::has_io_service
35+
>::type())
36+
, handler(args[_handler])
37+
, address_(args[_address])
38+
, port_(args[_port])
39+
, thread_pool(args[_thread_pool])
40+
, acceptor(server_storage_base::service_)
3441
, stopping(false)
3542
, new_connection()
3643
, listening_mutex_()
3744
, listening(false)
3845
{}
3946

4047
voidrun() {
41-
boost::unique_lock<boost::mutex>listening_lock(listening_mutex_);
42-
if (!listening)start_listening();
43-
listening_lock.unlock();
48+
listen();
4449
io_service.run();
4550
};
4651

@@ -51,6 +56,12 @@ namespace boost { namespace network { namespace http {
5156
acceptor.cancel();
5257
}
5358

59+
voidlisten() {
60+
boost::unique_lock<boost::mutex>listening_lock(listening_mutex_);
61+
if (!listening)start_listening();
62+
listening_lock.unlock();
63+
}
64+
5465
private:
5566
Handler & handler;
5667
string_type address_, port_;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_PARAMETERS_HPP_20101210
2+
#defineBOOST_NETWORK_PROTOCOL_HTTP_SERVER_PARAMETERS_HPP_20101210
3+
4+
// Copyright 2010 Dean Michael Berris.
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
9+
#include<boost/network/protocol/http/parameters.hpp>
10+
11+
namespaceboost {namespacenetwork {namespacehttp {
12+
13+
BOOST_PARAMETER_NAME(address)
14+
BOOST_PARAMETER_NAME(port)
15+
BOOST_PARAMETER_NAME(handler)
16+
BOOST_PARAMETER_NAME(thread_pool)
17+
BOOST_PARAMETER_NAME(socket_options)
18+
19+
}/* http*/
20+
21+
}/* network*/
22+
23+
}/* boost*/
24+
25+
#endif/* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_PARAMETERS_HPP_20101210*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_STORAGE_BASE_HPP_20101210
2+
#defineBOOST_NETWORK_PROTOCOL_HTTP_SERVER_STORAGE_BASE_HPP_20101210
3+
4+
// Copyright 2010 Dean Michael Berris.
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
9+
#include<boost/network/protocol/http/server/parameters.hpp>
10+
11+
namespaceboost {namespacenetwork {namespacehttp {
12+
13+
structserver_storage_base {
14+
structno_io_service {};
15+
structhas_io_service {};
16+
protected:
17+
template<classArgPack>
18+
server_storage_base(ArgPackconst & args, no_io_service)
19+
: self_service_(new boost::asio::io_service())
20+
, service_(*self_service_)
21+
{}
22+
23+
template<classArgPack>
24+
server_storage_base(ArgPackconst & args, has_io_service)
25+
: self_service_()
26+
, service_(args[_io_service])
27+
{}
28+
29+
std::auto_ptr<asio::io_service> self_service_;
30+
asio::io_service & service_;
31+
};
32+
33+
34+
}/* http*/
35+
36+
}/* network*/
37+
38+
}/* boost*/
39+
40+
#endif/* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_STORAGE_BASE_HPP_20101210*/

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,42 @@
1414
#include<boost/network/protocol/http/response.hpp>
1515
#include<boost/network/protocol/http/request.hpp>
1616
#include<boost/network/protocol/http/server/sync_connection.hpp>
17+
#include<boost/network/protocol/http/server/parameters.hpp>
18+
#include<boost/network/protocol/http/server/storage_base.hpp>
1719
#include<boost/network/traits/string.hpp>
1820
#include<boost/thread/mutex.hpp>
1921

2022
namespaceboost {namespacenetwork {namespacehttp {
21-
23+
2224
template<classTag,classHandler>
23-
structsync_server_base {
25+
structsync_server_base: server_storage_base{
2426
typedeftypename string<Tag>::type string_type;
2527
typedef basic_request<Tag> request;
2628
typedef basic_response<Tag> response;
2729
typedeftypename boost::network::http::response_header<Tag>::type response_header;
2830

29-
sync_server_base(string_typeconst & address,
30-
string_typeconst & port,
31-
Handler & handler)
32-
: handler_(handler)
33-
, address_(address)
34-
, port_(port)
35-
, self_service_(new boost::asio::io_service())
36-
, service_(*self_service_)
37-
, acceptor_(service_)
31+
template<classArgPack>
32+
sync_server_base(ArgPackconst & args)
33+
: server_storage_base(args,
34+
typename mpl::if_<
35+
is_same<
36+
typename parameter::value_type<ArgPack, tag::io_service,void>::type,
37+
void
38+
>,
39+
server_storage_base::no_io_service,
40+
server_storage_base::has_io_service
41+
>::type())
42+
, handler_(args[_handler])
43+
, address_(args[_address])
44+
, port_(args[_port])
45+
, acceptor_(server_storage_base::service_)
3846
, new_connection()
3947
, listening_mutex_()
4048
, listening_(false)
4149
{}
4250

4351
voidrun() {
44-
boost::unique_lock<boost::mutex>listening_lock(listening_mutex_);
45-
if (!listening_)start_listening();
46-
listening_lock.unlock();
52+
listen();
4753
service_.run();
4854
}
4955

@@ -52,12 +58,16 @@ namespace boost { namespace network { namespace http {
5258
acceptor_.cancel();
5359
}
5460

61+
voidlisten() {
62+
boost::unique_lock<boost::mutex>listening_lock(listening_mutex_);
63+
if (!listening_)start_listening();
64+
listening_lock.unlock();
65+
}
66+
5567
private:
5668

5769
Handler & handler_;
5870
string_type address_, port_;
59-
std::auto_ptr<boost::asio::io_service> self_service_;
60-
boost::asio::io_service & service_;
6171
boost::asio::ip::tcp::acceptor acceptor_;
6272
boost::shared_ptr<sync_connection<Tag,Handler> > new_connection;
6373
boost::mutex listening_mutex_;

‎libs/network/test/http/server_constructor_test.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ BOOST_AUTO_TEST_CASE(minimal_constructor) {
4444
}
4545

4646
BOOST_AUTO_TEST_CASE(with_io_service_parameter) {
47+
dummy_sync_handler sync_handler;
48+
dummy_async_handler async_handler;
49+
util::thread_pool pool;
50+
boost::asio::io_service io_service;
51+
52+
BOOST_CHECK_NO_THROW(sync_serversync_instance("127.0.0.1","80", sync_handler, io_service));
53+
BOOST_CHECK_NO_THROW(async_serverasync_instance("127.0.0.1","80", async_handler, pool, io_service));
4754
}
4855

4956
BOOST_AUTO_TEST_CASE(with_socket_options_parameter) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp