]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/beast/example/websocket/server/sync/websocket_server_sync.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / beast / example / websocket / server / sync / websocket_server_sync.cpp
CommitLineData
b32b8144 1//
92f5a8d4 2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
b32b8144
FG
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 server, synchronous
13//
14//------------------------------------------------------------------------------
15
16#include <boost/beast/core.hpp>
17#include <boost/beast/websocket.hpp>
18#include <boost/asio/ip/tcp.hpp>
19#include <cstdlib>
20#include <functional>
21#include <iostream>
22#include <string>
23#include <thread>
24
92f5a8d4
TL
25namespace beast = boost::beast; // from <boost/beast.hpp>
26namespace http = beast::http; // from <boost/beast/http.hpp>
27namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
28namespace net = boost::asio; // from <boost/asio.hpp>
29using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
b32b8144
FG
30
31//------------------------------------------------------------------------------
32
33// Echoes back all received WebSocket messages
34void
20effc67 35do_session(tcp::socket socket)
b32b8144
FG
36{
37 try
38 {
39 // Construct the stream by moving in the socket
40 websocket::stream<tcp::socket> ws{std::move(socket)};
41
92f5a8d4
TL
42 // Set a decorator to change the Server of the handshake
43 ws.set_option(websocket::stream_base::decorator(
44 [](websocket::response_type& res)
45 {
46 res.set(http::field::server,
47 std::string(BOOST_BEAST_VERSION_STRING) +
48 " websocket-server-sync");
49 }));
50
b32b8144
FG
51 // Accept the websocket handshake
52 ws.accept();
53
54 for(;;)
55 {
56 // This buffer will hold the incoming message
92f5a8d4 57 beast::flat_buffer buffer;
b32b8144
FG
58
59 // Read a message
60 ws.read(buffer);
61
62 // Echo the message back
63 ws.text(ws.got_text());
64 ws.write(buffer.data());
65 }
66 }
92f5a8d4 67 catch(beast::system_error const& se)
b32b8144
FG
68 {
69 // This indicates that the session was closed
70 if(se.code() != websocket::error::closed)
71 std::cerr << "Error: " << se.code().message() << std::endl;
72 }
73 catch(std::exception const& e)
74 {
75 std::cerr << "Error: " << e.what() << std::endl;
76 }
77}
78
79//------------------------------------------------------------------------------
80
81int main(int argc, char* argv[])
82{
83 try
84 {
85 // Check command line arguments.
86 if (argc != 3)
87 {
88 std::cerr <<
89 "Usage: websocket-server-sync <address> <port>\n" <<
90 "Example:\n" <<
91 " websocket-server-sync 0.0.0.0 8080\n";
92 return EXIT_FAILURE;
93 }
92f5a8d4 94 auto const address = net::ip::make_address(argv[1]);
b32b8144
FG
95 auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
96
97 // The io_context is required for all I/O
92f5a8d4 98 net::io_context ioc{1};
b32b8144
FG
99
100 // The acceptor receives incoming connections
101 tcp::acceptor acceptor{ioc, {address, port}};
102 for(;;)
103 {
104 // This will receive the new connection
105 tcp::socket socket{ioc};
106
107 // Block until we get a connection
108 acceptor.accept(socket);
109
110 // Launch the session, transferring ownership of the socket
20effc67 111 std::thread(
b32b8144 112 &do_session,
20effc67 113 std::move(socket)).detach();
b32b8144
FG
114 }
115 }
116 catch (const std::exception& e)
117 {
118 std::cerr << "Error: " << e.what() << std::endl;
119 return EXIT_FAILURE;
120 }
121}