//// Copyright (c) 2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)//// Distributed under the Boost Software License, Version 1.0. (See accompanying// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)//#include <boost/asio/io_context.hpp>#include <boost/asio/signal_set.hpp>#include <cstdlib>#include <iostream>#include <string>#include "error.hpp"#include "listener.hpp"#include "services/redis_client.hpp"#include "shared_state.hpp"using namespace chat;int main(int argc, char* argv[]){// Check command line arguments.if (argc != 4){std::cerr << "Usage: " << argv[0] << " <address> <port> <doc_root>\n"<< "Example:\n"<< " " << argv[0] << " 0.0.0.0 8080 .\n";return EXIT_FAILURE;}// Application configconst char* doc_root = argv[3]; // Path to static filesconst char* ip = argv[1]; // IP where the server will listenauto port = static_cast<unsigned short>(std::atoi(argv[2])); // Port// An event loop, where the application will run. The server is single-// threaded, so we set the concurrency hint to 1boost::asio::io_context ioc{1};// Singleton objects shared by all connectionsauto st = std::make_shared<shared_state>(doc_root, ioc.get_executor());// The physical endpoint where our server will listenboost::asio::ip::tcp::endpoint listening_endpoint{boost::asio::ip::make_address(ip), port};// A signal_set allows us to intercept SIGINT and SIGTERM and// exit gracefullyboost::asio::signal_set signals{ioc.get_executor(), SIGINT, SIGTERM};// Launch the Redis connectionst->redis().start_run();// Start listening for HTTP connections. This will run until the context is stoppedauto ec = launch_http_listener(ioc.get_executor(), listening_endpoint, st);if (ec){log_error(ec, "Error launching the HTTP listener");exit(EXIT_FAILURE);}// Capture SIGINT and SIGTERM to perform a clean shutdownsignals.async_wait([st, &ioc](error_code, int) {// Stop the Redis reconnection loopst->redis().cancel();// Stop the io_context. This will cause run() to returnioc.stop();});// Run the io_context. This will block until the context is stopped by// a signal and all outstanding async tasks are finished.ioc.run();// (If we get here, it means we got a SIGINT or SIGTERM)return EXIT_SUCCESS;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。