@@ -23,29 +23,20 @@ namespace boost { namespace network { namespace http {
23
23
typedef typename boost::network::http::response_header<Tag>::type response_header;
24
24
typedef async_connection<Tag,Handler> connection;
25
25
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;
27
27
28
- template <class ArgPack >
29
- async_server_base (ArgPackconst & 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>())
44
35
, acceptor(server_storage_base::service_)
45
36
, stopping(false )
46
37
, new_connection()
47
38
, listening_mutex_()
48
- , stopping_mutex_()
39
+ , stopping_mutex_()
49
40
, listening(false )
50
41
{}
51
42
@@ -57,19 +48,19 @@ namespace boost { namespace network { namespace http {
57
48
void stop () {
58
49
// stop accepting new requests and let all the existing
59
50
// handlers finish.
60
- scoped_mutex_locklistening_lock (listening_mutex_);
61
- if (listening) {// we dont bother stopping if we arent currently listening
62
- scoped_mutex_lockstopping_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_locklistening_lock (listening_mutex_);
52
+ if (listening) {// we dont bother stopping if we arent currently listening
53
+ scoped_mutex_lockstopping_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
+ }
69
60
}
70
61
71
62
void listen () {
72
- scoped_mutex_locklistening_lock (listening_mutex_);
63
+ scoped_mutex_locklistening_lock (listening_mutex_);
73
64
BOOST_NETWORK_MESSAGE (" Listening on" << address_ <<' :' << port_);
74
65
if (!listening)start_listening ();// we only initialize our acceptor/sockets if we arent already listening
75
66
if (!listening) {
@@ -81,53 +72,44 @@ namespace boost { namespace network { namespace http {
81
72
private:
82
73
Handler & handler;
83
74
string_type address_, port_;
84
- utils::thread_pool & thread_pool;
75
+ boost::shared_ptr< utils::thread_pool> thread_pool;
85
76
asio::ip::tcp::acceptor acceptor;
86
77
bool stopping;
87
78
connection_ptr new_connection;
88
79
boost::mutex listening_mutex_;
89
- boost::mutex stopping_mutex_;
80
+ boost::mutex stopping_mutex_;
90
81
bool listening;
91
-
92
- void handle_stop () {
93
- scoped_mutex_lockstopping_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_lockstopping_lock (stopping_mutex_);
85
+ if (stopping) service_.stop ();// a user may have started listening again before the stop command is reached
86
+ }
96
87
97
88
void handle_accept (boost::system::error_codeconst & ec) {
98
- {
99
- scoped_mutex_lockstopping_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_lockstopping_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
+ }
102
93
if (!ec) {
103
94
socket_options_base::socket_options (new_connection->socket ());
104
95
new_connection->start ();
105
96
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));
119
104
}else {
120
105
BOOST_NETWORK_MESSAGE (" Error accepting connection, reason:" << ec);
121
106
}
122
107
}
123
108
124
109
void start_listening () {
125
110
using boost::asio::ip::tcp;
126
-
127
111
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
131
113
tcp::resolverresolver (service_);
132
114
tcp::resolver::queryquery (address_, port_);
133
115
tcp::resolver::iterator endpoint_iterator = resolver.resolve (query, error);
@@ -152,15 +134,15 @@ namespace boost { namespace network { namespace http {
152
134
BOOST_NETWORK_MESSAGE (" Error listening on socket: '" << error <<" ' on" << address_ <<" :" << port_);
153
135
return ;
154
136
}
155
- new_connection.reset (new connection (service_, handler, thread_pool));
137
+ new_connection.reset (new connection (service_, handler,* thread_pool));
156
138
acceptor.async_accept (new_connection->socket (),
157
139
boost::bind (
158
140
&async_server_base<Tag,Handler>::handle_accept
159
141
,this
160
142
, boost::asio::placeholders::error));
161
143
listening =true ;
162
- scoped_mutex_lockstopping_lock (stopping_mutex_);
163
- stopping =false ;// if we were in the process of stopping, we revoke that command and continue listening
144
+ scoped_mutex_lockstopping_lock (stopping_mutex_);
145
+ stopping =false ;// if we were in the process of stopping, we revoke that command and continue listening
164
146
BOOST_NETWORK_MESSAGE (" Now listening on socket: '" << address_ <<" :" << port_ <<" '" );
165
147
}
166
148
};