Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 20 | // |
| 21 | // connection.hpp |
| 22 | // ~~~~~~~~~~~~~~ |
| 23 | // |
| 24 | // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
| 25 | // |
| 26 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 27 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 28 | // |
| 29 | |
| 30 | #ifndef HTTP_CONNECTION_HPP |
| 31 | #define HTTP_CONNECTION_HPP |
| 32 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 33 | #include "reply.hpp" |
| 34 | #include "request.hpp" |
| 35 | #include "request_handler.hpp" |
| 36 | #include "request_parser.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 37 | #include <boost/array.hpp> |
| 38 | #include <boost/asio.hpp> |
| 39 | #include <boost/enable_shared_from_this.hpp> |
| 40 | #include <boost/noncopyable.hpp> |
| 41 | #include <boost/shared_ptr.hpp> |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 42 | |
| 43 | namespace http { |
| 44 | namespace server { |
| 45 | |
| 46 | class connection_manager; |
| 47 | |
| 48 | /// Represents a single connection from a client. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 49 | class connection : public boost::enable_shared_from_this<connection>, private boost::noncopyable |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 50 | { |
| 51 | public: |
| 52 | /// Construct a connection with the given io_service. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 53 | explicit connection(boost::asio::io_service& io_service, connection_manager& manager, |
| 54 | request_handler& handler); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 55 | |
| 56 | /// Get the socket associated with the connection. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | boost::asio::ip::tcp::socket& |
| 58 | socket(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 59 | |
| 60 | /// Start the first asynchronous operation for the connection. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 61 | void |
| 62 | start(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 63 | |
| 64 | /// Stop all asynchronous operations associated with the connection. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 65 | void |
| 66 | stop(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | /// Handle completion of a read operation. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 70 | void |
| 71 | handle_read(const boost::system::error_code& e, std::size_t bytes_transferred); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 72 | |
| 73 | /// Handle completion of a write operation. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 74 | void |
| 75 | handle_write(const boost::system::error_code& e); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 76 | |
| 77 | /// Socket for the connection. |
| 78 | boost::asio::ip::tcp::socket socket_; |
| 79 | |
| 80 | /// The manager for this connection. |
| 81 | connection_manager& connection_manager_; |
| 82 | |
| 83 | /// The handler used to process the incoming request. |
| 84 | request_handler& request_handler_; |
| 85 | |
| 86 | /// Buffer for incoming data. |
| 87 | boost::array<char, 8192> buffer_; |
| 88 | |
| 89 | /// The incoming request. |
| 90 | request request_; |
| 91 | |
| 92 | /// The parser for the incoming request. |
| 93 | request_parser request_parser_; |
| 94 | |
| 95 | /// The reply to be sent back to the client. |
| 96 | reply reply_; |
| 97 | }; |
| 98 | |
| 99 | typedef boost::shared_ptr<connection> connection_ptr; |
| 100 | |
| 101 | } // namespace server |
| 102 | } // namespace http |
| 103 | |
| 104 | #endif // HTTP_CONNECTION_HPP |