]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/server/chat-multi/main.cpp
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // Official repository: https://github.com/vinniefalco/CppCon2018
10 //------------------------------------------------------------------------------
12 WebSocket chat server, multi-threaded
14 This implements a multi-user chat room using WebSocket. The
15 `io_context` runs on any number of threads, specified at
19 //------------------------------------------------------------------------------
21 #include "listener.hpp"
22 #include "shared_state.hpp"
24 #include <boost/asio/signal_set.hpp>
25 #include <boost/smart_ptr.hpp>
30 main(int argc
, char* argv
[])
32 // Check command line arguments.
36 "Usage: websocket-chat-multi <address> <port> <doc_root> <threads>\n" <<
38 " websocket-chat-server 0.0.0.0 8080 . 5\n";
41 auto address
= net::ip::make_address(argv
[1]);
42 auto port
= static_cast<unsigned short>(std::atoi(argv
[2]));
43 auto doc_root
= argv
[3];
44 auto const threads
= std::max
<int>(1, std::atoi(argv
[4]));
46 // The io_context is required for all I/O
49 // Create and launch a listening port
50 boost::make_shared
<listener
>(
52 tcp::endpoint
{address
, port
},
53 boost::make_shared
<shared_state
>(doc_root
))->run();
55 // Capture SIGINT and SIGTERM to perform a clean shutdown
56 net::signal_set
signals(ioc
, SIGINT
, SIGTERM
);
58 [&ioc
](boost::system::error_code
const&, int)
60 // Stop the io_context. This will cause run()
61 // to return immediately, eventually destroying the
62 // io_context and any remaining handlers in it.
66 // Run the I/O service on the requested number of threads
67 std::vector
<std::thread
> v
;
68 v
.reserve(threads
- 1);
69 for(auto i
= threads
- 1; i
> 0; --i
)
77 // (If we get here, it means we got a SIGINT or SIGTERM)
79 // Block until all the threads exit