Skip to content

Commit 20dba83

Browse files
committed
Incremental Improvement on Async Server
These changes moves the synchronous connection headers into the correct header location in boost/network/protocol/http/server. This also paves the way for a more functional asynchronous connection handler/handle for the HTTP server.
1 parent bb76f2c commit 20dba83

File tree

4 files changed

+67
-46
lines changed

4 files changed

+67
-46
lines changed

boost/network/protocol/http/server.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99
#ifndef BOOST_NETWORK_HTTP_SERVER_HPP_
1010
#define BOOST_NETWORK_HTTP_SERVER_HPP_
1111

12-
#include <boost/shared_ptr.hpp>
13-
#include <boost/bind.hpp>
14-
#include <boost/asio/ip/tcp.hpp>
1512
#include <boost/network/protocol/http/response.hpp>
1613
#include <boost/network/protocol/http/request.hpp>
17-
#include <boost/network/protocol/http/connection.hpp>
18-
#include <boost/network/traits/string.hpp>
1914
#include <boost/network/protocol/http/server/sync_server.hpp>
2015
#include <boost/network/protocol/http/server/async_server.hpp>
2116

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <boost/range/algorithm/transform.hpp>
1414
#include <boost/asio/ip/tcp.hpp>
1515
#include <boost/asio/streambuf.hpp>
16+
#include <boost/asio/strand.hpp>
17+
#include <boost/asio/buffer.hpp>
1618
#include <iterator>
1719

1820
namespace boost { namespace network { namespace http {
@@ -46,6 +48,7 @@ namespace boost { namespace network { namespace http {
4648
, utils::thread_pool & thread_pool
4749
)
4850
: socket_(io_service)
51+
, strand(io_service)
4952
, handler(handler)
5053
, thread_pool_(thread_pool)
5154
, headers_already_set(false)
@@ -67,11 +70,8 @@ namespace boost { namespace network { namespace http {
6770
boost::throw_exception(std::logic_error("Headers have already been set."));
6871

6972
bool commit = false;
70-
BOOST_SCOPE_EXIT_TPL((&commit)(&headers_already_set))
71-
{
72-
if (!commit) {
73-
headers_already_set = false;
74-
}
73+
BOOST_SCOPE_EXIT_TPL((&commit)(&headers_already_set)) {
74+
if (!commit) headers_already_set = false;
7575
} BOOST_SCOPE_EXIT_END
7676

7777
typedef constants<Tag> consts;
@@ -112,21 +112,45 @@ namespace boost { namespace network { namespace http {
112112
socket_.close();
113113
}
114114

115-
asio::ip::tcp::socket & socket() { return socket_; }
115+
asio::ip::tcp::socket & socket() { return socket_; }
116116
utils::thread_pool & thread_pool() { return thread_pool_; }
117117

118118
private:
119119
asio::ip::tcp::socket socket_;
120+
asio::io_service::strand strand;
120121
Handler & handler;
121122
utils::thread_pool & thread_pool_;
122123
bool headers_already_set;
123124
asio::streambuf headers_buffer;
125+
126+
typedef boost::array<char, BOOST_NETWORK_HTTP_SERVER_CONNECTION_BUFFER_SIZE>
127+
buffer_type;
128+
buffer_type read_buffer_;
124129
boost::uint16_t status;
125130

126131
template <class, class> friend struct async_server_base;
127132

133+
enum state_t {
134+
method, uri, version, headers
135+
};
136+
128137
void start() {
129-
// FIXME do something!
138+
socket_.async_read_some(
139+
asio::buffer(read_buffer_)
140+
, strand.wrap(
141+
boost::bind(
142+
&async_connection<Tag,Handler>::handle_read_data,
143+
async_connection<Tag,Handler>::shared_from_this(),
144+
method,
145+
boost::asio::placeholders::error,
146+
boost::asio::placeholders::bytes_transferred
147+
)
148+
)
149+
);
150+
}
151+
152+
void handle_read_data(state_t, boost::system::error_code const & ec, std::size_t bytes_transferred) {
153+
// FIXME -- damn all that work got wiped out because Jeni tripped on the power. :(
130154
}
131155

132156
};

boost/network/protocol/http/connection.hpp renamed to boost/network/protocol/http/server/sync_connection.hpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

8-
#ifndef BOOST_NETWORK_HTTP_CONNECTION_HPP_
9-
#define BOOST_NETWORK_HTTP_CONNECTION_HPP_
8+
#ifndef BOOST_NETWORK_HTTP_SERVER_SYNC_CONNECTION_HPP_
9+
#define BOOST_NETWORK_HTTP_SERVER_SYNC_CONNECTION_HPP_
1010

11-
#ifndef BOOST_HTTP_SERVER_BUFFER_SIZE
12-
#define BOOST_HTTP_SERVER_BUFFER_SIZE 1024
11+
#ifndef BOOST_NETWORK_HTTP_SERVER_CONNECTION_BUFFER_SIZE
12+
#define BOOST_NETWORK_HTTP_SERVER_CONNECTION_BUFFER_SIZE 1024
1313
#endif
1414

1515
#include <boost/enable_shared_from_this.hpp>
@@ -31,9 +31,9 @@
3131
namespace boost { namespace network { namespace http {
3232

3333
template <class Tag, class Handler>
34-
struct connection : boost::enable_shared_from_this<connection<Tag,Handler> > {
34+
struct sync_connection : boost::enable_shared_from_this<sync_connection<Tag,Handler> > {
3535

36-
connection(boost::asio::io_service & service, Handler & handler)
36+
sync_connection(boost::asio::io_service & service, Handler & handler)
3737
: service_(service)
3838
, handler_(handler)
3939
, socket_(service_)
@@ -59,8 +59,8 @@ namespace boost { namespace network { namespace http {
5959
boost::asio::buffer(buffer_),
6060
wrapper_.wrap(
6161
boost::bind(
62-
&connection<Tag,Handler>::handle_read_headers,
63-
connection<Tag,Handler>::shared_from_this(),
62+
&sync_connection<Tag,Handler>::handle_read_headers,
63+
sync_connection<Tag,Handler>::shared_from_this(),
6464
boost::asio::placeholders::error,
6565
boost::asio::placeholders::bytes_transferred
6666
)
@@ -81,7 +81,7 @@ namespace boost { namespace network { namespace http {
8181
if (!ec) {
8282
request_.source = socket_.remote_endpoint().address().to_string();
8383
boost::tribool done;
84-
boost::array<char, BOOST_HTTP_SERVER_BUFFER_SIZE>::iterator new_start;
84+
buffer_type::iterator new_start;
8585
tie(done,new_start) = parser_.parse_headers(request_, buffer_.data(), buffer_.data() + bytes_transferred);
8686
if (done) {
8787
if (request_.method[0] == 'P') {
@@ -99,8 +99,8 @@ namespace boost { namespace network { namespace http {
9999
response_.to_buffers(),
100100
wrapper_.wrap(
101101
boost::bind(
102-
&connection<Tag,Handler>::handle_write,
103-
connection<Tag,Handler>::shared_from_this(),
102+
&sync_connection<Tag,Handler>::handle_write,
103+
sync_connection<Tag,Handler>::shared_from_this(),
104104
boost::asio::placeholders::error
105105
)
106106
)
@@ -119,8 +119,8 @@ namespace boost { namespace network { namespace http {
119119
response_.to_buffers(),
120120
wrapper_.wrap(
121121
boost::bind(
122-
&connection<Tag,Handler>::handle_write,
123-
connection<Tag,Handler>::shared_from_this(),
122+
&sync_connection<Tag,Handler>::handle_write,
123+
sync_connection<Tag,Handler>::shared_from_this(),
124124
boost::asio::placeholders::error
125125
)
126126
)
@@ -138,8 +138,8 @@ namespace boost { namespace network { namespace http {
138138
boost::asio::buffer(buffer_),
139139
wrapper_.wrap(
140140
boost::bind(
141-
&connection<Tag,Handler>::handle_read_body_contents,
142-
connection<Tag,Handler>::shared_from_this(),
141+
&sync_connection<Tag,Handler>::handle_read_body_contents,
142+
sync_connection<Tag,Handler>::shared_from_this(),
143143
boost::asio::placeholders::error,
144144
content_length,
145145
boost::asio::placeholders::bytes_transferred
@@ -156,8 +156,8 @@ namespace boost { namespace network { namespace http {
156156
response_.to_buffers(),
157157
wrapper_.wrap(
158158
boost::bind(
159-
&connection<Tag,Handler>::handle_write,
160-
connection<Tag,Handler>::shared_from_this(),
159+
&sync_connection<Tag,Handler>::handle_write,
160+
sync_connection<Tag,Handler>::shared_from_this(),
161161
boost::asio::placeholders::error
162162
)
163163
)
@@ -169,8 +169,8 @@ namespace boost { namespace network { namespace http {
169169
response_.to_buffers(),
170170
wrapper_.wrap(
171171
boost::bind(
172-
&connection<Tag,Handler>::handle_write,
173-
connection<Tag,Handler>::shared_from_this(),
172+
&sync_connection<Tag,Handler>::handle_write,
173+
sync_connection<Tag,Handler>::shared_from_this(),
174174
boost::asio::placeholders::error
175175
)
176176
)
@@ -183,8 +183,8 @@ namespace boost { namespace network { namespace http {
183183
response_.to_buffers(),
184184
wrapper_.wrap(
185185
boost::bind(
186-
&connection<Tag,Handler>::handle_write,
187-
connection<Tag,Handler>::shared_from_this(),
186+
&sync_connection<Tag,Handler>::handle_write,
187+
sync_connection<Tag,Handler>::shared_from_this(),
188188
boost::asio::placeholders::error
189189
)
190190
)
@@ -194,8 +194,8 @@ namespace boost { namespace network { namespace http {
194194
boost::asio::buffer(buffer_),
195195
wrapper_.wrap(
196196
boost::bind(
197-
&connection<Tag,Handler>::handle_read_headers,
198-
connection<Tag,Handler>::shared_from_this(),
197+
&sync_connection<Tag,Handler>::handle_read_headers,
198+
sync_connection<Tag,Handler>::shared_from_this(),
199199
boost::asio::placeholders::error,
200200
boost::asio::placeholders::bytes_transferred
201201
)
@@ -209,7 +209,7 @@ namespace boost { namespace network { namespace http {
209209
void handle_read_body_contents(boost::system::error_code const & ec, size_t bytes_to_read, size_t bytes_transferred) {
210210
if (!ec) {
211211
size_t difference = bytes_to_read - bytes_transferred;
212-
boost::array<char,BOOST_HTTP_SERVER_BUFFER_SIZE>::iterator start = buffer_.begin(),
212+
buffer_type::iterator start = buffer_.begin(),
213213
past_end = start;
214214
std::advance(past_end, (std::min)(bytes_to_read,bytes_transferred));
215215
request_.body.append(buffer_.begin(), past_end);
@@ -220,8 +220,8 @@ namespace boost { namespace network { namespace http {
220220
response_.to_buffers(),
221221
wrapper_.wrap(
222222
boost::bind(
223-
&connection<Tag,Handler>::handle_write,
224-
connection<Tag,Handler>::shared_from_this(),
223+
&sync_connection<Tag,Handler>::handle_write,
224+
sync_connection<Tag,Handler>::shared_from_this(),
225225
boost::asio::placeholders::error
226226
)
227227
)
@@ -231,8 +231,8 @@ namespace boost { namespace network { namespace http {
231231
boost::asio::buffer(buffer_),
232232
wrapper_.wrap(
233233
boost::bind(
234-
&connection<Tag,Handler>::handle_read_body_contents,
235-
connection<Tag,Handler>::shared_from_this(),
234+
&sync_connection<Tag,Handler>::handle_read_body_contents,
235+
sync_connection<Tag,Handler>::shared_from_this(),
236236
boost::asio::placeholders::error,
237237
difference,
238238
boost::asio::placeholders::bytes_transferred
@@ -257,7 +257,9 @@ namespace boost { namespace network { namespace http {
257257
Handler & handler_;
258258
boost::asio::ip::tcp::socket socket_;
259259
boost::asio::io_service::strand wrapper_;
260-
boost::array<char,BOOST_HTTP_SERVER_BUFFER_SIZE> buffer_;
260+
261+
typedef boost::array<char,BOOST_NETWORK_HTTP_SERVER_CONNECTION_BUFFER_SIZE> buffer_type;
262+
buffer_type buffer_;
261263
request_parser parser_;
262264
basic_request<Tag> request_;
263265
basic_response<Tag> response_;
@@ -270,5 +272,5 @@ namespace boost { namespace network { namespace http {
270272

271273
} // namespace boost
272274

273-
#endif // BOOST_NETWORK_HTTP_CONNECTION_HPP_
275+
#endif // BOOST_NETWORK_HTTP_SERVER_SYNC_CONNECTION_HPP_
274276

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <boost/asio/ip/tcp.hpp>
1414
#include <boost/network/protocol/http/response.hpp>
1515
#include <boost/network/protocol/http/request.hpp>
16-
#include <boost/network/protocol/http/connection.hpp>
16+
#include <boost/network/protocol/http/server/sync_connection.hpp>
1717
#include <boost/network/traits/string.hpp>
1818

1919
namespace boost { namespace network { namespace http {
@@ -30,7 +30,7 @@ namespace boost { namespace network { namespace http {
3030
: handler_(handler)
3131
, service_()
3232
, acceptor_(service_)
33-
, new_connection(new connection<Tag,Handler>(service_, handler))
33+
, new_connection(new sync_connection<Tag,Handler>(service_, handler))
3434
{
3535
using boost::asio::ip::tcp;
3636
tcp::resolver resolver(service_);
@@ -58,12 +58,12 @@ namespace boost { namespace network { namespace http {
5858
Handler & handler_;
5959
boost::asio::io_service service_;
6060
boost::asio::ip::tcp::acceptor acceptor_;
61-
boost::shared_ptr<connection<Tag,Handler> > new_connection;
61+
boost::shared_ptr<sync_connection<Tag,Handler> > new_connection;
6262

6363
void handle_accept(boost::system::error_code const & ec) {
6464
if (!ec) {
6565
new_connection->start();
66-
new_connection.reset(new connection<Tag,Handler>(service_, handler_));
66+
new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));
6767
acceptor_.async_accept(new_connection->socket(),
6868
boost::bind(&sync_server_base<Tag,Handler>::handle_accept,
6969
this, boost::asio::placeholders::error));

0 commit comments

Comments
 (0)