]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/server/sync-ssl/websocket_server_sync_ssl.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / beast / example / websocket / server / sync-ssl / websocket_server_sync_ssl.cpp
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, synchronous
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/ip/tcp.hpp>
22 #include <boost/asio/ssl/stream.hpp>
23 #include <cstdlib>
24 #include <functional>
25 #include <iostream>
26 #include <string>
27 #include <thread>
28
29 using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
30 namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
31 namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
32
33 //------------------------------------------------------------------------------
34
35 // Echoes back all received WebSocket messages
36 void
37 do_session(tcp::socket& socket, ssl::context& ctx)
38 {
39 try
40 {
41 // Construct the websocket stream around the socket
42 websocket::stream<ssl::stream<tcp::socket&>> ws{socket, ctx};
43
44 // Perform the SSL handshake
45 ws.next_layer().handshake(ssl::stream_base::server);
46
47 // Accept the websocket handshake
48 ws.accept();
49
50 for(;;)
51 {
52 // This buffer will hold the incoming message
53 boost::beast::multi_buffer buffer;
54
55 // Read a message
56 ws.read(buffer);
57
58 // Echo the message back
59 ws.text(ws.got_text());
60 ws.write(buffer.data());
61 }
62 }
63 catch(boost::system::system_error const& se)
64 {
65 // This indicates that the session was closed
66 if(se.code() != websocket::error::closed)
67 std::cerr << "Error: " << se.code().message() << std::endl;
68 }
69 catch(std::exception const& e)
70 {
71 std::cerr << "Error: " << e.what() << std::endl;
72 }
73 }
74
75 //------------------------------------------------------------------------------
76
77 int main(int argc, char* argv[])
78 {
79 try
80 {
81 // Check command line arguments.
82 if (argc != 3)
83 {
84 std::cerr <<
85 "Usage: websocket-server-sync-ssl <address> <port>\n" <<
86 "Example:\n" <<
87 " websocket-server-sync-ssl 0.0.0.0 8080\n";
88 return EXIT_FAILURE;
89 }
90 auto const address = boost::asio::ip::make_address(argv[1]);
91 auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
92
93 // The io_context is required for all I/O
94 boost::asio::io_context ioc{1};
95
96 // The SSL context is required, and holds certificates
97 ssl::context ctx{ssl::context::sslv23};
98
99 // This holds the self-signed certificate used by the server
100 load_server_certificate(ctx);
101
102 // The acceptor receives incoming connections
103 tcp::acceptor acceptor{ioc, {address, port}};
104 for(;;)
105 {
106 // This will receive the new connection
107 tcp::socket socket{ioc};
108
109 // Block until we get a connection
110 acceptor.accept(socket);
111
112 // Launch the session, transferring ownership of the socket
113 std::thread{std::bind(
114 &do_session,
115 std::move(socket),
116 std::ref(ctx))}.detach();
117 }
118 }
119 catch (const std::exception& e)
120 {
121 std::cerr << "Error: " << e.what() << std::endl;
122 return EXIT_FAILURE;
123 }
124 }