connection.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // connection.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #ifndef HTTP_CONNECTION_HPP
  11. #define HTTP_CONNECTION_HPP
  12. #include <array>
  13. #include <memory>
  14. #include <boost/asio.hpp>
  15. #include "reply.hpp"
  16. #include "request.hpp"
  17. #include "request_handler.hpp"
  18. #include "request_parser.hpp"
  19. namespace http {
  20. namespace server {
  21. class connection_manager;
  22. /// Represents a single connection from a client.
  23. class connection
  24. : public std::enable_shared_from_this<connection>
  25. {
  26. public:
  27. connection(const connection&) = delete;
  28. connection& operator=(const connection&) = delete;
  29. /// Construct a connection with the given socket.
  30. explicit connection(boost::asio::ip::tcp::socket socket,
  31. connection_manager& manager, request_handler& handler);
  32. /// Start the first asynchronous operation for the connection.
  33. void start();
  34. /// Stop all asynchronous operations associated with the connection.
  35. void stop();
  36. private:
  37. /// Perform an asynchronous read operation.
  38. void do_read();
  39. /// Perform an asynchronous write operation.
  40. void do_write();
  41. /// Socket for the connection.
  42. boost::asio::ip::tcp::socket socket_;
  43. /// The manager for this connection.
  44. connection_manager& connection_manager_;
  45. /// The handler used to process the incoming request.
  46. request_handler& request_handler_;
  47. /// Buffer for incoming data.
  48. std::array<char, 8192> buffer_;
  49. /// The incoming request.
  50. request request_;
  51. /// The parser for the incoming request.
  52. request_parser request_parser_;
  53. /// The reply to be sent back to the client.
  54. reply reply_;
  55. };
  56. typedef std::shared_ptr<connection> connection_ptr;
  57. } // namespace server
  58. } // namespace http
  59. #endif // HTTP_CONNECTION_HPP