diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-04 19:24:16 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-04 19:24:16 +0200 |
commit | 1fcaed7a34cce8e55bb071d503bb583f715e7d37 (patch) | |
tree | 9c6bcaa267a66b902f308ee253a79da874780e55 /server.cpp | |
parent | 938fbe7a2f2f10a3abb530a9463e57fc20f40038 (diff) |
Serve configured sockets
Diffstat (limited to 'server.cpp')
-rw-r--r-- | server.cpp | 56 |
1 files changed, 54 insertions, 2 deletions
@@ -1,10 +1,62 @@ +#include <boost/beast/core.hpp> +#include <boost/beast/http.hpp> +#include <boost/beast/version.hpp> +#include <boost/beast/ssl.hpp> +#include <boost/asio/dispatch.hpp> +#include <boost/asio/strand.hpp> +#include <boost/config.hpp> + +#include <thread> +#include <vector> + #include "server.h" #include "http.h" #include "https.h" +namespace beast = boost::beast; // from <boost/beast.hpp> +namespace http = beast::http; // from <boost/beast/http.hpp> +namespace net = boost::asio; // from <boost/asio.hpp> +namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp> +using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> + +Server::Server(Config& config, boost::asio::io_context& ioc): m_config(config), m_ioc(ioc) +{ +} + +Server::~Server() +{ +} + int server(Config& config) { - //return HTTP::server(config); - return HTTPS::server(config); + auto const threads = std::max<int>(1, config.Threads()); + + boost::asio::io_context ioc{threads}; + + std::vector<std::shared_ptr<Server>> servers; + + const auto& sockets {config.Sockets()}; + for (const auto& socket: sockets) { + if (socket.protocol == SocketProtocol::HTTP) { + servers.push_back(std::make_shared<HTTP::Server>(config, ioc, socket)); + } else { + servers.push_back(std::make_shared<HTTPS::Server>(config, ioc, socket)); + } + servers.back()->start(); + } + + // Run the I/O service on the requested number of threads + 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(); + + return EXIT_SUCCESS; } + |