generated from Sithas/conan_template
60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
#ifdef WIN32
|
|
#include <sdkddkver.h>
|
|
#endif
|
|
|
|
#include <algorithm>
|
|
#include <boost/asio/signal_set.hpp>
|
|
#include <boost/beast/core.hpp>
|
|
#include <boost/beast/http.hpp>
|
|
#include <boost/beast/websocket.hpp>
|
|
#include <mysqlx/xdevapi.h>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "./session/WebsocketSession.h"
|
|
#include "./listener/Listener.h"
|
|
|
|
namespace beast = boost::beast;
|
|
namespace http = beast::http;
|
|
namespace websocket = beast::websocket;
|
|
namespace net = boost::asio;
|
|
using tcp = boost::asio::ip::tcp;
|
|
using namespace uad;
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
if (argc != 5)
|
|
{
|
|
std::cerr << "Usage: advanced-server <address> <port> <doc_root> <threads>\n"
|
|
<< "Example:\n"
|
|
<< " advanced-server 0.0.0.0 8080 . 1\n";
|
|
return EXIT_FAILURE;
|
|
}
|
|
auto const address = net::ip::make_address(argv[1]);
|
|
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
|
auto const doc_root = std::make_shared<std::string>(argv[3]);
|
|
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
|
|
|
net::io_context ioc{threads};
|
|
|
|
std::make_shared<Listener>(ioc, tcp::endpoint{address, port}, doc_root)->Run();
|
|
|
|
net::signal_set signals(ioc, SIGINT, SIGTERM);
|
|
signals.async_wait([&](beast::error_code const&, int) { ioc.stop(); });
|
|
|
|
std::vector<std::thread> v;
|
|
v.reserve(threads - 1);
|
|
for (auto i = threads - 1; i > 0; --i)
|
|
v.emplace_back([&ioc] { ioc.run(); });
|
|
ioc.run();
|
|
|
|
for (auto& t : v)
|
|
t.join();
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|