connection.h 2.2 KB

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