]>
Commit | Line | Data |
---|---|---|
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 | ||
35 | using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> | |
36 | namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp> | |
37 | namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp> | |
38 | ||
39 | //------------------------------------------------------------------------------ | |
40 | ||
41 | // Report a failure | |
42 | void | |
43 | fail(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 | |
49 | class 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 | ||
59 | public: | |
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 | |
155 | class 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 | ||
163 | public: | |
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 | ||
182 | // Bind to the server address | |
183 | acceptor_.bind(endpoint, ec); | |
184 | if(ec) | |
185 | { | |
186 | fail(ec, "bind"); | |
187 | return; | |
188 | } | |
189 | ||
190 | // Start listening for connections | |
191 | acceptor_.listen( | |
192 | boost::asio::socket_base::max_listen_connections, ec); | |
193 | if(ec) | |
194 | { | |
195 | fail(ec, "listen"); | |
196 | return; | |
197 | } | |
198 | } | |
199 | ||
200 | // Start accepting incoming connections | |
201 | void | |
202 | run() | |
203 | { | |
204 | if(! acceptor_.is_open()) | |
205 | return; | |
206 | loop(); | |
207 | } | |
208 | ||
209 | #include <boost/asio/yield.hpp> | |
210 | void | |
211 | loop(boost::system::error_code ec = {}) | |
212 | { | |
213 | reenter(*this) | |
214 | { | |
215 | for(;;) | |
216 | { | |
217 | yield acceptor_.async_accept( | |
218 | socket_, | |
219 | std::bind( | |
220 | &listener::loop, | |
221 | shared_from_this(), | |
222 | std::placeholders::_1)); | |
223 | if(ec) | |
224 | { | |
225 | fail(ec, "accept"); | |
226 | } | |
227 | else | |
228 | { | |
229 | // Create the session and run it | |
230 | std::make_shared<session>(std::move(socket_), ctx_)->run(); | |
231 | } | |
232 | } | |
233 | } | |
234 | } | |
235 | #include <boost/asio/unyield.hpp> | |
236 | }; | |
237 | ||
238 | //------------------------------------------------------------------------------ | |
239 | ||
240 | int main(int argc, char* argv[]) | |
241 | { | |
242 | // Check command line arguments. | |
243 | if (argc != 4) | |
244 | { | |
245 | std::cerr << | |
246 | "Usage: websocket-server-async-ssl <address> <port> <threads>\n" << | |
247 | "Example:\n" << | |
248 | " websocket-server-async-ssl 0.0.0.0 8080 1\n"; | |
249 | return EXIT_FAILURE; | |
250 | } | |
251 | auto const address = boost::asio::ip::make_address(argv[1]); | |
252 | auto const port = static_cast<unsigned short>(std::atoi(argv[2])); | |
253 | auto const threads = std::max<int>(1, std::atoi(argv[3])); | |
254 | ||
255 | // The io_context is required for all I/O | |
256 | boost::asio::io_context ioc{threads}; | |
257 | ||
258 | // The SSL context is required, and holds certificates | |
259 | ssl::context ctx{ssl::context::sslv23}; | |
260 | ||
261 | // This holds the self-signed certificate used by the server | |
262 | load_server_certificate(ctx); | |
263 | ||
264 | // Create and launch a listening port | |
265 | std::make_shared<listener>(ioc, ctx, tcp::endpoint{address, port})->run(); | |
266 | ||
267 | // Run the I/O service on the requested number of threads | |
268 | std::vector<std::thread> v; | |
269 | v.reserve(threads - 1); | |
270 | for(auto i = threads - 1; i > 0; --i) | |
271 | v.emplace_back( | |
272 | [&ioc] | |
273 | { | |
274 | ioc.run(); | |
275 | }); | |
276 | ioc.run(); | |
277 | ||
278 | return EXIT_SUCCESS; | |
279 | } |