1 // 2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See accompanying 5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // Official repository: https://github.com/vinniefalco/CppCon2018 8 // 9 10 #ifndef BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_HTTP_SESSION_HPP 11 #define BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_HTTP_SESSION_HPP 12 13 #include "net.hpp" 14 #include "beast.hpp" 15 #include "shared_state.hpp" 16 #include <boost/optional.hpp> 17 #include <boost/smart_ptr.hpp> 18 #include <cstdlib> 19 #include <memory> 20 21 /** Represents an established HTTP connection 22 */ 23 class http_session : public boost::enable_shared_from_this<http_session> 24 { 25 beast::tcp_stream stream_; 26 beast::flat_buffer buffer_; 27 boost::shared_ptr<shared_state> state_; 28 29 // The parser is stored in an optional container so we can 30 // construct it from scratch it at the beginning of each new message. 31 boost::optional<http::request_parser<http::string_body>> parser_; 32 33 struct send_lambda; 34 35 void fail(beast::error_code ec, char const* what); 36 void do_read(); 37 void on_read(beast::error_code ec, std::size_t); 38 void on_write(beast::error_code ec, std::size_t, bool close); 39 40 public: 41 http_session( 42 tcp::socket&& socket, 43 boost::shared_ptr<shared_state> const& state); 44 45 void run(); 46 }; 47 48 #endif 49