1 //
2 // connection.hpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef HTTP_SERVER3_CONNECTION_HPP
12 #define HTTP_SERVER3_CONNECTION_HPP
13 
14 #include <boost/asio.hpp>
15 #include <boost/array.hpp>
16 #include <boost/noncopyable.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/enable_shared_from_this.hpp>
19 #include "reply.hpp"
20 #include "request.hpp"
21 #include "request_handler.hpp"
22 #include "request_parser.hpp"
23 
24 namespace http {
25 namespace server3 {
26 
27 /// Represents a single connection from a client.
28 class connection
29   : public boost::enable_shared_from_this<connection>,
30     private boost::noncopyable
31 {
32 public:
33   /// Construct a connection with the given io_service.
34   explicit connection(boost::asio::io_service& io_service,
35       request_handler& handler);
36 
37   /// Get the socket associated with the connection.
38   boost::asio::ip::tcp::socket& socket();
39 
40   /// Start the first asynchronous operation for the connection.
41   void start();
42 
43 private:
44   /// Handle completion of a read operation.
45   void handle_read(const boost::system::error_code& e,
46       std::size_t bytes_transferred);
47 
48   /// Handle completion of a write operation.
49   void handle_write(const boost::system::error_code& e);
50 
51   /// Strand to ensure the connection's handlers are not called concurrently.
52   boost::asio::io_service::strand strand_;
53 
54   /// Socket for the connection.
55   boost::asio::ip::tcp::socket socket_;
56 
57   /// The handler used to process the incoming request.
58   request_handler& request_handler_;
59 
60   /// Buffer for incoming data.
61   boost::array<char, 8192> buffer_;
62 
63   /// The incoming request.
64   request request_;
65 
66   /// The parser for the incoming request.
67   request_parser request_parser_;
68 
69   /// The reply to be sent back to the client.
70   reply reply_;
71 };
72 
73 typedef boost::shared_ptr<connection> connection_ptr;
74 
75 } // namespace server3
76 } // namespace http
77 
78 #endif // HTTP_SERVER3_CONNECTION_HPP
79