make command not block.
This commit is contained in:
parent
83b0ff4369
commit
e833d990db
@ -349,7 +349,13 @@ void Application::requetExit() {
|
|||||||
nng_log_set_logger(nng_stderr_logger);
|
nng_log_set_logger(nng_stderr_logger);
|
||||||
LOG(info) << "send exit request to program.";
|
LOG(info) << "send exit request to program.";
|
||||||
Nng::Socket request(Nng::Request);
|
Nng::Socket request(Nng::Request);
|
||||||
request.dial(IpcUrl);
|
request.setOption(Nng::RecvTimeout, std::chrono::milliseconds(2000));
|
||||||
|
std::error_code error;
|
||||||
|
request.dial(IpcUrl, error);
|
||||||
|
if (error) {
|
||||||
|
LOG(error) << error.message();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
boost::json::object object;
|
boost::json::object object;
|
||||||
object["command"] = "exit";
|
object["command"] = "exit";
|
||||||
|
160
Server/main.cpp
160
Server/main.cpp
@ -32,87 +32,89 @@ int main(int argc, char const *argv[]) {
|
|||||||
try {
|
try {
|
||||||
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), values);
|
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), values);
|
||||||
boost::program_options::notify(values);
|
boost::program_options::notify(values);
|
||||||
|
|
||||||
|
if (values.count("help")) {
|
||||||
|
std::cout << description << std::endl;
|
||||||
|
std::exit(0);
|
||||||
|
} else if (values.count("exit")) {
|
||||||
|
Application::requetExit();
|
||||||
|
std::exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::error_code error;
|
||||||
|
auto prefix = std::filesystem::current_path(error);
|
||||||
|
if (error) {
|
||||||
|
LOG(fatal) << "cannot get current path,reason: " << error.message();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (values.count("prefix")) {
|
||||||
|
prefix = values["prefix"].as<std::string>();
|
||||||
|
if (prefix.empty() || !std::filesystem::exists(prefix)) {
|
||||||
|
LOG(fatal) << "working directory: " << prefix << " is not exists.";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
std::filesystem::current_path(prefix, error);
|
||||||
|
LOG_IF(fatal, error) << "cannot set current path,reason: " << error.message();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto application = Singleton<Application>::instance<Construct>("settings.ini");
|
||||||
|
|
||||||
|
auto database = Singleton<Database>::instance<Construct>();
|
||||||
|
database->open(application->getSqlte3Path());
|
||||||
|
|
||||||
|
if (!std::filesystem::exists(application->getDocumentRoot())) {
|
||||||
|
LOG(fatal) << "document root: " << application->getDocumentRoot() << " is not exists...";
|
||||||
|
std::exit(102);
|
||||||
|
}
|
||||||
|
BOOST_ASSERT_MSG(!application->getServer().empty(), "server.empty() == true");
|
||||||
|
|
||||||
|
auto address = boost::asio::ip::make_address(application->getServer());
|
||||||
|
auto listener = std::make_shared<Listener>(application->ioContext(),
|
||||||
|
boost::asio::ip::tcp::endpoint{address, application->getPort()});
|
||||||
|
listener->startAccept();
|
||||||
|
|
||||||
|
auto wechatContext = Singleton<WeChatContext>::instance<Construct>(application->ioContext());
|
||||||
|
auto corpContext = Singleton<CorporationContext>::instance<Construct>(application->ioContext());
|
||||||
|
corpContext->start();
|
||||||
|
auto live2d = std::make_shared<Live2dBackend>();
|
||||||
|
|
||||||
|
LOG(info) << "hardware_concurrency: " << std::thread::hardware_concurrency()
|
||||||
|
<< ",threads: " << application->getThreads();
|
||||||
|
LOG(info) << "working directory: " << prefix.generic_string();
|
||||||
|
LOG(info) << "server: " << application->getServer() << ",port: " << application->getPort();
|
||||||
|
LOG(info) << "document root: " << application->getDocumentRoot();
|
||||||
|
|
||||||
|
// Capture SIGINT and SIGTERM to perform a clean shutdown
|
||||||
|
#ifndef WIN32
|
||||||
|
boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM, SIGHUP);
|
||||||
|
#else
|
||||||
|
boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM);
|
||||||
|
#endif
|
||||||
|
signals.add(SIGUSR1);
|
||||||
|
signals.async_wait([&application](boost::system::error_code const &, int signal) {
|
||||||
|
// Stop the io_context. This will cause run()
|
||||||
|
// to return immediately, eventually destroying the
|
||||||
|
// io_context and any remaining handlers in it.
|
||||||
|
LOG(info) << "capture " << (signal == SIGINT ? "SIGINT" : "SIGTERM") << ",stop!";
|
||||||
|
application->ioContext().stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
auto udpServer = std::make_shared<UdpServer>(application->ioContext());
|
||||||
|
auto mediaServer = std::make_shared<MediaServer>(554, false);
|
||||||
|
auto webApp = std::make_unique<WebApplication>();
|
||||||
|
|
||||||
|
using namespace boost::asio::ip;
|
||||||
|
auto proxyAddress = make_address(application->getServer());
|
||||||
|
uint16_t proxyPort = 41091;
|
||||||
|
auto proxy = std::make_shared<ProxyListener>(application->ioContext(), tcp::endpoint{proxyAddress, proxyPort});
|
||||||
|
boost::system::error_code perror;
|
||||||
|
proxy->run(perror);
|
||||||
|
return application->exec();
|
||||||
} catch (const boost::program_options::invalid_command_line_syntax &e) {
|
} catch (const boost::program_options::invalid_command_line_syntax &e) {
|
||||||
LOG(fatal) << e.what();
|
LOG(fatal) << e.what();
|
||||||
std::exit(-1);
|
std::exit(-1);
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
LOG(error) << e.what();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (values.count("help")) {
|
|
||||||
std::cout << description << std::endl;
|
|
||||||
std::exit(0);
|
|
||||||
} else if (values.count("exit")) {
|
|
||||||
Application::requetExit();
|
|
||||||
std::exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::error_code error;
|
|
||||||
auto prefix = std::filesystem::current_path(error);
|
|
||||||
if (error) {
|
|
||||||
LOG(fatal) << "cannot get current path,reason: " << error.message();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (values.count("prefix")) {
|
|
||||||
prefix = values["prefix"].as<std::string>();
|
|
||||||
if (prefix.empty() || !std::filesystem::exists(prefix)) {
|
|
||||||
LOG(fatal) << "working directory: " << prefix << " is not exists.";
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
std::filesystem::current_path(prefix, error);
|
|
||||||
LOG_IF(fatal, error) << "cannot set current path,reason: " << error.message();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto application = Singleton<Application>::instance<Construct>("settings.ini");
|
|
||||||
|
|
||||||
auto database = Singleton<Database>::instance<Construct>();
|
|
||||||
database->open(application->getSqlte3Path());
|
|
||||||
|
|
||||||
if (!std::filesystem::exists(application->getDocumentRoot())) {
|
|
||||||
LOG(fatal) << "document root: " << application->getDocumentRoot() << " is not exists...";
|
|
||||||
std::exit(102);
|
|
||||||
}
|
|
||||||
BOOST_ASSERT_MSG(!application->getServer().empty(), "server.empty() == true");
|
|
||||||
|
|
||||||
auto address = boost::asio::ip::make_address(application->getServer());
|
|
||||||
auto listener = std::make_shared<Listener>(application->ioContext(),
|
|
||||||
boost::asio::ip::tcp::endpoint{address, application->getPort()});
|
|
||||||
listener->startAccept();
|
|
||||||
|
|
||||||
auto wechatContext = Singleton<WeChatContext>::instance<Construct>(application->ioContext());
|
|
||||||
auto corpContext = Singleton<CorporationContext>::instance<Construct>(application->ioContext());
|
|
||||||
corpContext->start();
|
|
||||||
auto live2d = std::make_shared<Live2dBackend>();
|
|
||||||
|
|
||||||
LOG(info) << "hardware_concurrency: " << std::thread::hardware_concurrency()
|
|
||||||
<< ",threads: " << application->getThreads();
|
|
||||||
LOG(info) << "working directory: " << prefix.generic_string();
|
|
||||||
LOG(info) << "server: " << application->getServer() << ",port: " << application->getPort();
|
|
||||||
LOG(info) << "document root: " << application->getDocumentRoot();
|
|
||||||
|
|
||||||
// Capture SIGINT and SIGTERM to perform a clean shutdown
|
|
||||||
#ifndef WIN32
|
|
||||||
boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM, SIGHUP);
|
|
||||||
#else
|
|
||||||
boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM);
|
|
||||||
#endif
|
|
||||||
signals.add(SIGUSR1);
|
|
||||||
signals.async_wait([&application](boost::system::error_code const &, int signal) {
|
|
||||||
// Stop the io_context. This will cause run()
|
|
||||||
// to return immediately, eventually destroying the
|
|
||||||
// io_context and any remaining handlers in it.
|
|
||||||
LOG(info) << "capture " << (signal == SIGINT ? "SIGINT" : "SIGTERM") << ",stop!";
|
|
||||||
application->ioContext().stop();
|
|
||||||
});
|
|
||||||
|
|
||||||
auto udpServer = std::make_shared<UdpServer>(application->ioContext());
|
|
||||||
auto mediaServer = std::make_shared<MediaServer>(554, false);
|
|
||||||
auto webApp = std::make_unique<WebApplication>();
|
|
||||||
|
|
||||||
using namespace boost::asio::ip;
|
|
||||||
auto proxyAddress = make_address(application->getServer());
|
|
||||||
uint16_t proxyPort = 41091;
|
|
||||||
auto proxy = std::make_shared<ProxyListener>(application->ioContext(), tcp::endpoint{proxyAddress, proxyPort});
|
|
||||||
boost::system::error_code perror;
|
|
||||||
proxy->run(perror);
|
|
||||||
return application->exec();
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user