@@ -23,29 +23,20 @@ namespace boost { namespace network { namespace http {
2323typedef typename boost::network::http::response_header<Tag>::type response_header;
2424typedef async_connection<Tag,Handler> connection;
2525typedef 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 (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>())
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 {
5748void stop () {
5849// stop accepting new requests and let all the existing
5950// 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+ }
6960 }
7061
7162void listen () {
72- scoped_mutex_locklistening_lock (listening_mutex_);
63+ scoped_mutex_locklistening_lock (listening_mutex_);
7364BOOST_NETWORK_MESSAGE (" Listening on" << address_ <<' :' << port_);
7465if (!listening)start_listening ();// we only initialize our acceptor/sockets if we arent already listening
7566if (!listening) {
@@ -81,53 +72,44 @@ namespace boost { namespace network { namespace http {
8172private:
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;
8677bool stopping;
8778 connection_ptr new_connection;
8879 boost::mutex listening_mutex_;
89- boost::mutex stopping_mutex_;
80+ boost::mutex stopping_mutex_;
9081bool 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+ }
9687
9788void 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+ }
10293if (!ec) {
10394socket_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 {
120105BOOST_NETWORK_MESSAGE (" Error accepting connection, reason:" << ec);
121106 }
122107 }
123108
124109void start_listening () {
125110using 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::resolverresolver (service_);
132114 tcp::resolver::queryquery (address_, port_);
133115 tcp::resolver::iterator endpoint_iterator = resolver.resolve (query, error);
@@ -152,15 +134,15 @@ namespace boost { namespace network { namespace http {
152134BOOST_NETWORK_MESSAGE (" Error listening on socket: '" << error <<" ' on" << address_ <<" :" << port_);
153135return ;
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 (),
157139boost::bind (
158140 &async_server_base<Tag,Handler>::handle_accept
159141 ,this
160142 , boost::asio::placeholders::error));
161143 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
164146BOOST_NETWORK_MESSAGE (" Now listening on socket: '" << address_ <<" :" << port_ <<" '" );
165147 }
166148 };