2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 // We wrote this code based on the original code which has the
31 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
33 // Distributed under the Boost Software License, Version 1.0. (See accompanying
34 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
37 #ifndef ASIO_SERVER_CONNECTION_H
38 #define ASIO_SERVER_CONNECTION_H
40 #include "nghttp2_config.h"
44 #include <boost/noncopyable.hpp>
45 #include <boost/array.hpp>
47 #include <nghttp2/asio_http2_server.h>
49 #include "asio_server_http2_handler.h"
50 #include "asio_server_serve_mux.h"
55 namespace asio_http2 {
59 /// Represents a single connection from a client.
60 template <typename socket_type>
61 class connection : public std::enable_shared_from_this<connection<socket_type>>,
62 private boost::noncopyable {
64 /// Construct a connection with the given io_service.
65 template <typename... SocketArgs>
66 explicit connection(serve_mux &mux, SocketArgs &&... args)
67 : socket_(std::forward<SocketArgs>(args)...), mux_(mux), writing_(false) {
70 /// Start the first asynchronous operation for the connection.
72 handler_ = std::make_shared<http2_handler>(socket_.get_io_service(),
73 [this]() { do_write(); }, mux_);
74 if (handler_->start() != 0) {
80 socket_type &socket() { return socket_; }
83 auto self = this->shared_from_this();
85 socket_.async_read_some(boost::asio::buffer(buffer_),
86 [this, self](const boost::system::error_code &e,
87 std::size_t bytes_transferred) {
89 if (handler_->on_read(buffer_, bytes_transferred) != 0) {
95 if (!writing_ && handler_->should_stop()) {
102 // If an error occurs then no new asynchronous operations are
103 // started. This means that all shared_ptr references to the
104 // connection object will disappear and the object will be
105 // destroyed automatically after this handler returns. The
106 // connection class's destructor closes the socket.
111 auto self = this->shared_from_this();
120 rv = handler_->on_write(outbuf_, nwrite);
132 boost::asio::async_write(
133 socket_, boost::asio::buffer(outbuf_, nwrite),
134 [this, self](const boost::system::error_code &e, std::size_t) {
142 // No new asynchronous operations are started. This means that all
143 // shared_ptr references to the connection object will disappear and
144 // the object will be destroyed automatically after this handler
145 // returns. The connection class's destructor closes the socket.
153 std::shared_ptr<http2_handler> handler_;
155 /// Buffer for incoming data.
156 boost::array<uint8_t, 8192> buffer_;
158 boost::array<uint8_t, 65536> outbuf_;
163 } // namespace server
165 } // namespace asio_http2
167 } // namespace nghttp2
169 #endif // ASIO_SERVER_CONNECTION_H