]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/server/chat-multi/websocket_session.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / beast / example / websocket / server / chat-multi / websocket_session.hpp
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_WEBSOCKET_SESSION_HPP
11 #define BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_WEBSOCKET_SESSION_HPP
12
13 #include "net.hpp"
14 #include "beast.hpp"
15 #include "shared_state.hpp"
16
17 #include <cstdlib>
18 #include <memory>
19 #include <string>
20 #include <vector>
21
22 // Forward declaration
23 class shared_state;
24
25 /** Represents an active WebSocket connection to the server
26 */
27 class websocket_session : public boost::enable_shared_from_this<websocket_session>
28 {
29 beast::flat_buffer buffer_;
30 websocket::stream<beast::tcp_stream> ws_;
31 boost::shared_ptr<shared_state> state_;
32 std::vector<boost::shared_ptr<std::string const>> queue_;
33
34 void fail(beast::error_code ec, char const* what);
35 void on_accept(beast::error_code ec);
36 void on_read(beast::error_code ec, std::size_t bytes_transferred);
37 void on_write(beast::error_code ec, std::size_t bytes_transferred);
38
39 public:
40 websocket_session(
41 tcp::socket&& socket,
42 boost::shared_ptr<shared_state> const& state);
43
44 ~websocket_session();
45
46 template<class Body, class Allocator>
47 void
48 run(http::request<Body, http::basic_fields<Allocator>> req);
49
50 // Send a message
51 void
52 send(boost::shared_ptr<std::string const> const& ss);
53
54 private:
55 void
56 on_send(boost::shared_ptr<std::string const> const& ss);
57 };
58
59 template<class Body, class Allocator>
60 void
61 websocket_session::
62 run(http::request<Body, http::basic_fields<Allocator>> req)
63 {
64 // Set suggested timeout settings for the websocket
65 ws_.set_option(
66 websocket::stream_base::timeout::suggested(
67 beast::role_type::server));
68
69 // Set a decorator to change the Server of the handshake
70 ws_.set_option(websocket::stream_base::decorator(
71 [](websocket::response_type& res)
72 {
73 res.set(http::field::server,
74 std::string(BOOST_BEAST_VERSION_STRING) +
75 " websocket-chat-multi");
76 }));
77
78 // Accept the websocket handshake
79 ws_.async_accept(
80 req,
81 beast::bind_front_handler(
82 &websocket_session::on_accept,
83 shared_from_this()));
84 }
85
86 #endif