]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/beast/example/websocket/server/stackless-ssl/websocket_server_stackless_ssl.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / beast / example / websocket / server / stackless-ssl / websocket_server_stackless_ssl.cpp
CommitLineData
b32b8144
FG
1//
2// Copyright (c) 2016-2017 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/boostorg/beast
8//
9
10//------------------------------------------------------------------------------
11//
12// Example: WebSocket SSL server, stackless coroutine
13//
14//------------------------------------------------------------------------------
15
16#include "example/common/server_certificate.hpp"
17
18#include <boost/beast/core.hpp>
19#include <boost/beast/websocket.hpp>
20#include <boost/beast/websocket/ssl.hpp>
21#include <boost/asio/bind_executor.hpp>
22#include <boost/asio/coroutine.hpp>
23#include <boost/asio/ip/tcp.hpp>
24#include <boost/asio/ssl/stream.hpp>
25#include <boost/asio/strand.hpp>
26#include <algorithm>
27#include <cstdlib>
28#include <functional>
29#include <iostream>
30#include <memory>
31#include <string>
32#include <thread>
33#include <vector>
34
35using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
36namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
37namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
38
39//------------------------------------------------------------------------------
40
41// Report a failure
42void
43fail(boost::system::error_code ec, char const* what)
44{
45 std::cerr << what << ": " << ec.message() << "\n";
46}
47
48// Echoes back all received WebSocket messages
49class session
50 : public boost::asio::coroutine
51 , public std::enable_shared_from_this<session>
52{
53 tcp::socket socket_;
54 websocket::stream<ssl::stream<tcp::socket&>> ws_;
55 boost::asio::strand<
56 boost::asio::io_context::executor_type> strand_;
57 boost::beast::multi_buffer buffer_;
58
59public:
60 // Take ownership of the socket
61 session(tcp::socket socket, ssl::context& ctx)
62 : socket_(std::move(socket))
63 , ws_(socket_, ctx)
64 , strand_(ws_.get_executor())
65 {
66 }
67
68 // Start the asynchronous operation
69 void
70 run()
71 {
72 loop({}, 0);
73 }
74
75#include <boost/asio/yield.hpp>
76 void
77 loop(
78 boost::system::error_code ec,
79 std::size_t bytes_transferred)
80 {
81 boost::ignore_unused(bytes_transferred);
82
83 reenter(*this)
84 {
85 // Perform the SSL handshake
86 yield ws_.next_layer().async_handshake(
87 ssl::stream_base::server,
88 boost::asio::bind_executor(
89 strand_,
90 std::bind(
91 &session::loop,
92 shared_from_this(),
93 std::placeholders::_1,
94 0)));
95 if(ec)
96 return fail(ec, "handshake");
97
98 // Accept the websocket handshake
99 yield ws_.async_accept(
100 boost::asio::bind_executor(
101 strand_,
102 std::bind(
103 &session::loop,
104 shared_from_this(),
105 std::placeholders::_1,
106 0)));
107 if(ec)
108 return fail(ec, "accept");
109
110 for(;;)
111 {
112 // Read a message into our buffer
113 yield ws_.async_read(
114 buffer_,
115 boost::asio::bind_executor(
116 strand_,
117 std::bind(
118 &session::loop,
119 shared_from_this(),
120 std::placeholders::_1,
121 std::placeholders::_2)));
122 if(ec == websocket::error::closed)
123 {
124 // This indicates that the session was closed
125 return;
126 }
127 if(ec)
128 fail(ec, "read");
129
130 // Echo the message
131 ws_.text(ws_.got_text());
132 yield ws_.async_write(
133 buffer_.data(),
134 boost::asio::bind_executor(
135 strand_,
136 std::bind(
137 &session::loop,
138 shared_from_this(),
139 std::placeholders::_1,
140 std::placeholders::_2)));
141 if(ec)
142 return fail(ec, "write");
143
144 // Clear the buffer
145 buffer_.consume(buffer_.size());
146 }
147 }
148 }
149#include <boost/asio/unyield.hpp>
150};
151
152//------------------------------------------------------------------------------
153
154// Accepts incoming connections and launches the sessions
155class listener
156 : public boost::asio::coroutine
157 , public std::enable_shared_from_this<listener>
158{
159 ssl::context& ctx_;
160 tcp::acceptor acceptor_;
161 tcp::socket socket_;
162
163public:
164 listener(
165 boost::asio::io_context& ioc,
166 ssl::context& ctx,
167 tcp::endpoint endpoint)
168 : ctx_(ctx)
169 , acceptor_(ioc)
170 , socket_(ioc)
171 {
172 boost::system::error_code ec;
173
174 // Open the acceptor
175 acceptor_.open(endpoint.protocol(), ec);
176 if(ec)
177 {
178 fail(ec, "open");
179 return;
180 }
181
11fdf7f2
TL
182 // Allow address reuse
183 acceptor_.set_option(boost::asio::socket_base::reuse_address(true));
184 if(ec)
185 {
186 fail(ec, "set_option");
187 return;
188 }
189
b32b8144
FG
190 // Bind to the server address
191 acceptor_.bind(endpoint, ec);
192 if(ec)
193 {
194 fail(ec, "bind");
195 return;
196 }
197
198 // Start listening for connections
199 acceptor_.listen(
200 boost::asio::socket_base::max_listen_connections, ec);
201 if(ec)
202 {
203 fail(ec, "listen");
204 return;
205 }
206 }
207
208 // Start accepting incoming connections
209 void
210 run()
211 {
212 if(! acceptor_.is_open())
213 return;
214 loop();
215 }
216
217#include <boost/asio/yield.hpp>
218 void
219 loop(boost::system::error_code ec = {})
220 {
221 reenter(*this)
222 {
223 for(;;)
224 {
225 yield acceptor_.async_accept(
226 socket_,
227 std::bind(
228 &listener::loop,
229 shared_from_this(),
230 std::placeholders::_1));
231 if(ec)
232 {
233 fail(ec, "accept");
234 }
235 else
236 {
237 // Create the session and run it
238 std::make_shared<session>(std::move(socket_), ctx_)->run();
239 }
240 }
241 }
242 }
243#include <boost/asio/unyield.hpp>
244};
245
246//------------------------------------------------------------------------------
247
248int main(int argc, char* argv[])
249{
250 // Check command line arguments.
251 if (argc != 4)
252 {
253 std::cerr <<
254 "Usage: websocket-server-async-ssl <address> <port> <threads>\n" <<
255 "Example:\n" <<
256 " websocket-server-async-ssl 0.0.0.0 8080 1\n";
257 return EXIT_FAILURE;
258 }
259 auto const address = boost::asio::ip::make_address(argv[1]);
260 auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
261 auto const threads = std::max<int>(1, std::atoi(argv[3]));
262
263 // The io_context is required for all I/O
264 boost::asio::io_context ioc{threads};
265
266 // The SSL context is required, and holds certificates
267 ssl::context ctx{ssl::context::sslv23};
268
269 // This holds the self-signed certificate used by the server
270 load_server_certificate(ctx);
271
272 // Create and launch a listening port
273 std::make_shared<listener>(ioc, ctx, tcp::endpoint{address, port})->run();
274
275 // Run the I/O service on the requested number of threads
276 std::vector<std::thread> v;
277 v.reserve(threads - 1);
278 for(auto i = threads - 1; i > 0; --i)
279 v.emplace_back(
280 [&ioc]
281 {
282 ioc.run();
283 });
284 ioc.run();
285
286 return EXIT_SUCCESS;
287}