-
Notifications
You must be signed in to change notification settings - Fork 13
/
connection.hpp
102 lines (78 loc) · 2.73 KB
/
connection.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// connection.hpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef HTTP_SERVER3_CONNECTION_HPP
#define HTTP_SERVER3_CONNECTION_HPP
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <vector>
#include <boost/enable_shared_from_this.hpp>
#include "reply.hpp"
#include "request.hpp"
#include "request_handler.hpp"
#include "request_parser.hpp"
#include <iostream>
namespace http
{
namespace server
{
/// Represents a single connection from a client.
class connection: public boost::enable_shared_from_this<connection>, private boost::noncopyable
{
public:
/// Construct a connection with the given io_service.
explicit
connection(boost::asio::io_service& io_service, request_handler& handler);
/// Get the socket associated with the connection.
boost::asio::ip::tcp::socket&
socket();
/// Start the first asynchronous operation for the connection.
void
start();
/// Handle completion of a read operation.
void
handle_read(const boost::system::error_code& e, std::size_t bytes_transferred);
/// Handle completion of a write operation.
void
handle_write(const boost::system::error_code& e);
void
async_write(const std::vector<boost::asio::const_buffer>& buffers)
{
boost::asio::async_write(
socket_, buffers,
strand_.wrap(boost::bind(&connection::handle_write, shared_from_this(), boost::asio::placeholders::error)));
}
template<typename HandleWrite>
void
async_write(const std::vector<boost::asio::const_buffer>& buffers, const HandleWrite& handler)
{
boost::asio::async_write(socket_, buffers, strand_.wrap(handler));
}
/// No callbacks will be called concurrently for this connection.
boost::asio::strand strand_;
/// Socket for the connection.
boost::asio::ip::tcp::socket socket_;
/// The handler used to process the incoming request.
request_handler& request_handler_;
/// Buffer for incoming data.
boost::array<char, 8192> buffer_;
/// The incoming request.
request request_;
/// The parser for the incoming request.
request_parser request_parser_;
/// The reply to be sent back to the client.
reply reply_;
};
typedef boost::shared_ptr<connection> connection_ptr;
} // namespace server3
} // namespace http
#endif // HTTP_SERVER3_CONNECTION_HPP