Skip to content

Commit 961f68d

Browse files
committed
Stubbed out initial look of the Asynchonous Server implementation.
1 parent f347a5f commit 961f68d

File tree

11 files changed

+331
-64
lines changed

11 files changed

+331
-64
lines changed

boost/network/protocol/http/impl/request.hpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ namespace boost { namespace network { namespace http {
115115
* primarily and be solely a POD for performance
116116
* reasons.
117117
*/
118-
template <>
119-
struct basic_request<tags::http_server> {
120-
typedef tags::http_server tag;
121-
typedef string<tags::http_server>::type string_type;
122-
typedef vector<tags::http_server>::apply<request_header>::type vector_type;
118+
template <class Tag>
119+
struct pod_request_base {
120+
typedef Tag tag;
121+
typedef typename string<Tag>::type string_type;
122+
typedef typename vector<tags::http_server>::
123+
template apply<request_header>::type
124+
vector_type;
123125
typedef vector_type headers_container_type;
124126
typedef boost::uint16_t port_type;
125127
mutable string_type source;
@@ -130,7 +132,7 @@ namespace boost { namespace network { namespace http {
130132
mutable vector_type headers;
131133
mutable string_type body;
132134

133-
void swap(basic_request & r) const {
135+
void swap(pod_request_base & r) const {
134136
using std::swap;
135137
swap(method, r.method);
136138
swap(source, r.source);
@@ -142,6 +144,12 @@ namespace boost { namespace network { namespace http {
142144
}
143145
};
144146

147+
template <>
148+
struct basic_request<tags::http_async_server> : pod_request_base<tags::http_async_server> {};
149+
150+
template <>
151+
struct basic_request<tags::http_server> : pod_request_base<tags::http_server> {};
152+
145153
template <class Tag>
146154
inline void swap(basic_request<Tag> & lhs, basic_request<Tag> & rhs) {
147155
lhs.swap(rhs);
@@ -155,6 +163,11 @@ namespace boost { namespace network { namespace http {
155163
vector<http::tags::http_server>::apply<http::request_header>
156164
{};
157165

166+
template <>
167+
struct headers_container<http::tags::http_async_server> :
168+
vector<http::tags::http_async_server>::apply<http::request_header>
169+
{};
170+
158171
namespace http { namespace impl {
159172

160173
template <>
@@ -179,6 +192,28 @@ namespace boost { namespace network { namespace http {
179192
}
180193
};
181194

195+
template <>
196+
struct request_headers_wrapper<tags::http_async_server> {
197+
basic_request<tags::http_async_server> const & request_;
198+
request_headers_wrapper(basic_request<tags::http_async_server> const & request_)
199+
: request_(request_) {}
200+
typedef headers_container<tags::http_async_server>::type headers_container_type;
201+
operator headers_container_type () {
202+
return request_.headers;
203+
}
204+
};
205+
206+
template <>
207+
struct body_wrapper<basic_request<tags::http_async_server> > {
208+
typedef string<tags::http_async_server>::type string_type;
209+
basic_request<tags::http_async_server> const & request_;
210+
body_wrapper(basic_request<tags::http_async_server> const & request_)
211+
: request_(request_) {}
212+
operator string_type () {
213+
return request_.body;
214+
}
215+
};
216+
182217
} // namespace impl
183218

184219
} // namespace http

boost/network/protocol/http/server.hpp

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Distributed under the Boost Software License, Version 1.0.
66
// (See accompanying file LICENSE_1_0.txt or copy at
77
// http://www.boost.org/LICENSE_1_0.txt)
8-
//
98

109
#ifndef BOOST_NETWORK_HTTP_SERVER_HPP_
1110
#define BOOST_NETWORK_HTTP_SERVER_HPP_
@@ -17,65 +16,32 @@
1716
#include <boost/network/protocol/http/request.hpp>
1817
#include <boost/network/protocol/http/connection.hpp>
1918
#include <boost/network/traits/string.hpp>
19+
#include <boost/network/protocol/http/server/sync_server.hpp>
20+
#include <boost/network/protocol/http/server/async_server.hpp>
2021

2122
namespace boost { namespace network { namespace http {
2223

2324
template <class Tag, class Handler>
24-
struct basic_server {
25-
typedef typename string<Tag>::type string_type;
26-
typedef basic_request<Tag> request;
27-
typedef basic_response<Tag> response;
25+
struct server_base :
26+
mpl::if_<
27+
is_async<Tag>
28+
, async_server_base<Tag, Handler>
29+
, typename mpl::if_<
30+
is_sync<Tag>
31+
, sync_server_base<Tag, Handler>
32+
, unsupported_tag<Tag>
33+
>::type
34+
>
35+
{};
2836

29-
basic_server(string_type const & address,
30-
string_type const & port,
31-
Handler & handler)
32-
: handler_(handler)
33-
, service_()
34-
, acceptor_(service_)
35-
, new_connection(new connection<Tag,Handler>(service_, handler))
36-
{
37-
using boost::asio::ip::tcp;
38-
tcp::resolver resolver(service_);
39-
tcp::resolver::query query(address, port);
40-
tcp::endpoint endpoint = *resolver.resolve(query);
41-
acceptor_.open(endpoint.protocol());
42-
acceptor_.bind(endpoint);
43-
acceptor_.listen();
44-
acceptor_.async_accept(new_connection->socket(),
45-
boost::bind(&basic_server<Tag,Handler>::handle_accept,
46-
this, boost::asio::placeholders::error));
47-
}
48-
49-
void run() {
50-
service_.run();
51-
}
52-
53-
void stop() {
54-
// TODO Graceful stop here?
55-
service_.stop();
56-
}
57-
58-
private:
59-
60-
Handler & handler_;
61-
boost::asio::io_service service_;
62-
boost::asio::ip::tcp::acceptor acceptor_;
63-
boost::shared_ptr<connection<Tag,Handler> > new_connection;
64-
65-
void handle_accept(boost::system::error_code const & ec) {
66-
if (!ec) {
67-
new_connection->start();
68-
new_connection.reset(new connection<Tag,Handler>(service_, handler_));
69-
acceptor_.async_accept(new_connection->socket(),
70-
boost::bind(&basic_server<Tag,Handler>::handle_accept,
71-
this, boost::asio::placeholders::error));
72-
}
73-
}
74-
};
37+
template <class Tag, class Handler>
38+
struct basic_server : server_base<Tag, Handler>::type
39+
{};
7540

7641
template <class Handler>
77-
struct server : basic_server<tags::http_server, Handler> {
78-
typedef basic_server<tags::http_server, Handler> server_base;
42+
struct server : server_base<tags::http_server, Handler>::type {
43+
typedef typename server_base<tags::http_server, Handler>::type
44+
server_base;
7945

8046
server(typename server_base::string_type const & address,
8147
typename server_base::string_type const & port,
@@ -84,6 +50,19 @@ namespace boost { namespace network { namespace http {
8450

8551
};
8652

53+
template <class Handler>
54+
struct async_server : server_base<tags::http_async_server, Handler>::type
55+
{
56+
typedef typename server_base<tags::http_async_server, Handler>::type
57+
server_base;
58+
59+
async_server(typename server_base::string_type const & address,
60+
typename server_base::string_type const & port,
61+
Handler & handler,
62+
utils::thread_pool & thread_pool)
63+
: server_base(address, port, handler, thread_pool) {}
64+
};
65+
8766
} // namespace http
8867

8968
} // namespace network
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_CONNECTION_HPP_20101027
2+
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_CONNECTION_HPP_20101027
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+
namespace boost { namespace network { namespace http {
10+
11+
template <class Tag, class Handler>
12+
struct async_connection {
13+
enum status_t {
14+
ok = 200
15+
, method_not_supported = 306
16+
};
17+
template <class Range>
18+
void set_headers(Range);
19+
void set_status(status_t);
20+
template <class Range>
21+
void write(Range);
22+
void flush();
23+
void close();
24+
};
25+
26+
} /* http */
27+
28+
} /* network */
29+
30+
} /* boost */
31+
32+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_CONNECTION_HPP_20101027 */
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_ASYNC_SERVER_HPP_20101025
2+
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_ASYNC_SERVER_HPP_20101025
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/async_connection.hpp>
10+
#include <boost/network/protocol/http/server/header.hpp>
11+
#include <boost/network/utils/thread_pool.hpp>
12+
13+
namespace boost { namespace network { namespace http {
14+
15+
template <class Tag, class Handler>
16+
struct async_server_base {
17+
typedef basic_request<Tag> request;
18+
typedef basic_response<Tag> response;
19+
typedef async_connection<Tag, Handler> connection;
20+
typedef response_header<Tag> response_header;
21+
typedef typename string<Tag>::type string_type;
22+
23+
async_server_base(string_type const & address
24+
, string_type const & port
25+
, Handler & handler
26+
, utils::thread_pool & thread_pool)
27+
{}
28+
29+
void run() {};
30+
31+
};
32+
33+
} /* http */
34+
35+
} /* network */
36+
37+
} /* boost */
38+
39+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_ASYNC_SERVER_HPP_20101025 */
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_HEADER_HPP_20101027
2+
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_HEADER_HPP_20101027
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/traits/string.hpp>
10+
#include <algorithm>
11+
12+
namespace boost { namespace network { namespace http {
13+
14+
template <class Tag>
15+
struct response_header {
16+
typedef typename string<Tag>::type string_type;
17+
string_type name, value;
18+
};
19+
20+
21+
template <class Tag>
22+
void swap(response_header<Tag> & l, response_header<Tag> & r) {
23+
std::swap(l.name, r.name);
24+
std::swap(l.value, r.value);
25+
}
26+
27+
} /* http */
28+
29+
} /* network */
30+
31+
} /* boost */
32+
33+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_HEADER_HPP_20101027 */
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
// Copyright 2010 Dean Michael Berris.
3+
// Copyright 2010 Glyn Matthews.
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE_1_0.txt or copy at
6+
// http://www.boost.org/LICENSE_1_0.txt)
7+
8+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_SYNC_SERVER_HPP_20101025
9+
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_SYNC_SERVER_HPP_20101025
10+
11+
#include <boost/shared_ptr.hpp>
12+
#include <boost/bind.hpp>
13+
#include <boost/asio/ip/tcp.hpp>
14+
#include <boost/network/protocol/http/response.hpp>
15+
#include <boost/network/protocol/http/request.hpp>
16+
#include <boost/network/protocol/http/connection.hpp>
17+
#include <boost/network/traits/string.hpp>
18+
19+
namespace boost { namespace network { namespace http {
20+
21+
template <class Tag, class Handler>
22+
struct sync_server_base {
23+
typedef typename string<Tag>::type string_type;
24+
typedef basic_request<Tag> request;
25+
typedef basic_response<Tag> response;
26+
27+
sync_server_base(string_type const & address,
28+
string_type const & port,
29+
Handler & handler)
30+
: handler_(handler)
31+
, service_()
32+
, acceptor_(service_)
33+
, new_connection(new connection<Tag,Handler>(service_, handler))
34+
{
35+
using boost::asio::ip::tcp;
36+
tcp::resolver resolver(service_);
37+
tcp::resolver::query query(address, port);
38+
tcp::endpoint endpoint = *resolver.resolve(query);
39+
acceptor_.open(endpoint.protocol());
40+
acceptor_.bind(endpoint);
41+
acceptor_.listen();
42+
acceptor_.async_accept(new_connection->socket(),
43+
boost::bind(&sync_server_base<Tag,Handler>::handle_accept,
44+
this, boost::asio::placeholders::error));
45+
}
46+
47+
void run() {
48+
service_.run();
49+
}
50+
51+
void stop() {
52+
// TODO Graceful stop here?
53+
service_.stop();
54+
}
55+
56+
private:
57+
58+
Handler & handler_;
59+
boost::asio::io_service service_;
60+
boost::asio::ip::tcp::acceptor acceptor_;
61+
boost::shared_ptr<connection<Tag,Handler> > new_connection;
62+
63+
void handle_accept(boost::system::error_code const & ec) {
64+
if (!ec) {
65+
new_connection->start();
66+
new_connection.reset(new connection<Tag,Handler>(service_, handler_));
67+
acceptor_.async_accept(new_connection->socket(),
68+
boost::bind(&sync_server_base<Tag,Handler>::handle_accept,
69+
this, boost::asio::placeholders::error));
70+
}
71+
}
72+
};
73+
74+
} /* http */
75+
76+
} /* network */
77+
78+
} /* boost */
79+
80+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_SERVER_SYNC_SERVER_HPP_20101025 */

boost/network/protocol/http/traits/vector.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ namespace boost { namespace network {
2121

2222
};
2323

24+
template <>
25+
struct vector<http::tags::http_async_server> {
26+
27+
template <class Type>
28+
struct apply {
29+
typedef std::vector<Type> type;
30+
};
31+
32+
};
33+
2434
} /* network */
2535

2636
} /* boost */

0 commit comments

Comments
 (0)