From 836c8e0c44c4737d250d1661d637500022731649 Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Sun, 11 May 2025 15:17:48 +0800 Subject: [PATCH] add nng code. --- .gitignore | 1 + .vscode/c_cpp_properties.json | 2 +- Application.cpp | 36 +++++++++++++++++++++++++++++++++++ Application.h | 3 +++ CMakeLists.txt | 4 ++++ main.cpp | 2 +- resources/build.sh | 4 +++- 7 files changed, 49 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 23d9f9e..4877ef3 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ target_wrapper.* # QtCreator CMake CMakeLists.txt.user* build +logs .DS_Store diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index acfb9b0..d7b1fc6 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -5,7 +5,7 @@ "includePath": [ "${workspaceFolder}/**", "${workspaceFolder}/3rdparty/sqlite-amalgamation-3490100", - "/opt/Libraries/boost_1_87_0/include" + "/opt/Libraries/boost_1_88_0/include" ], "defines": [], "compilerPath": "/usr/bin/gcc", diff --git a/Application.cpp b/Application.cpp index 12b1ac1..19f5f40 100644 --- a/Application.cpp +++ b/Application.cpp @@ -5,6 +5,9 @@ #include "Core/Singleton.h" #include "Database.h" #include "HttpSession.h" +#include "Nng/Asio.h" +#include "Nng/Message.h" +#include "Nng/Socket.h" #include "Router/router.hpp" #include "ServiceLogic.h" #include "SessionStore.h" @@ -17,8 +20,21 @@ namespace Older { class ApplicationPrivate { public: + void asyncReply() { + reply->asyncReceive([this](const std::error_code &error, const Nng::Message &message) { + if (error) { + LOG(error) << error.message(); + return; + } + LOG(info) << "request: " << message.data(); + std::string_view data("i am older server."); + reply->send(data.data(), data.size()); + asyncReply(); + }); + } std::shared_ptr> router; std::shared_ptr acceptor; + std::unique_ptr reply; }; Application::Application() : m_d{new ApplicationPrivate()} { @@ -47,6 +63,16 @@ Application::Application() : m_d{new ApplicationPrivate()} { } session.reply(ServiceLogic::make_200(request, "notify successed.\n", "text/html")); }); + + m_d->reply = std::make_unique(*m_ioContext->ioContext(), Nng::Reply); + m_d->reply->listen("tcp://127.0.0.1:8985"); + m_d->asyncReply(); +} + +Application::~Application() { + if (m_d != nullptr) { + delete m_d; + } } boost::asio::io_context &Application::ioContext() { @@ -93,9 +119,19 @@ int Application::exec() { ServiceLogic::staticFilesDeploy(); startAcceptHttpConnections(settings->server(), settings->port()); m_ioContext->run(); + LOG(info) << "app exit, code: " << m_returnCode; return 0; } +void Application::exit(int returnCode) { + if (m_returnCode != returnCode) { + m_returnCode = returnCode; + } + if (m_ioContext) { + m_ioContext->stop(); + } +} + void Application::asyncAcceptHttpConnections() { auto socket = std::make_shared(boost::asio::make_strand(*m_ioContext->ioContext())); m_d->acceptor->async_accept(*socket, [self{shared_from_this()}, socket](const boost::system::error_code &error) { diff --git a/Application.h b/Application.h index 771d756..8cc0dce 100644 --- a/Application.h +++ b/Application.h @@ -32,16 +32,19 @@ public: using Request = boost::beast::http::request; using RequestHandler = std::function; Application(); + ~Application(); boost::asio::io_context &ioContext(); void startAcceptHttpConnections(const std::string &address, uint16_t port); void insertUrl(std::string_view url, RequestHandler &&handler); int exec(); + void exit(int returnCode = 0); protected: void asyncAcceptHttpConnections(); private: ApplicationPrivate *m_d = nullptr; + int m_returnCode = 0; std::shared_ptr m_settings; std::shared_ptr m_ioContext; std::shared_ptr m_messageManager; diff --git a/CMakeLists.txt b/CMakeLists.txt index ddde7df..8e189f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ target_link_libraries(Older PRIVATE Kylin::Core PRIVATE Kylin::Http PRIVATE Kylin::Router + PRIVATE Kylin::Nng PRIVATE Boost::json PRIVATE sqlite3 ) @@ -44,5 +45,8 @@ include(FetchContent) FetchContent_Declare(Kylin GIT_REPOSITORY ssh://git@gitea.amass.fun:2022/amass/Kylin.git ) +set(ENABLE_HTTP ON) +set(ENABLE_ROUTER ON) +set(ENABLE_NNG ON) # add_subdirectory(/mnt/e/Projects/Kylin Kylin) FetchContent_MakeAvailable(Kylin) \ No newline at end of file diff --git a/main.cpp b/main.cpp index 0cad51f..6d9fa47 100644 --- a/main.cpp +++ b/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char const *argv[]) { boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM, SIGHUP); signals.async_wait([&application](boost::system::error_code const &, int signal) { LOG(info) << "capture " << (signal == SIGINT ? "SIGINT" : "SIGTERM") << ",stop!"; - application->ioContext().stop(); + application->exit(5); }); return application->exec(); diff --git a/resources/build.sh b/resources/build.sh index b1e5e0f..e0a9125 100755 --- a/resources/build.sh +++ b/resources/build.sh @@ -18,7 +18,9 @@ function cmake_scan() { cd ${build_path} ${cmake_exe} -G Ninja -S ${base_path} -B ${build_path} \ -DCMAKE_BUILD_TYPE=Debug \ - -DBOOST_ROOT=${libraries_root}/boost_1_87_0 + -DMbedTLS_DIR=${libraries_root}/mbedtls-3.6.3/lib/cmake/MbedTLS \ + -Dnng_DIR=${libraries_root}/nng-1.10.1/lib/cmake/nng \ + -DBOOST_ROOT=${libraries_root}/boost_1_88_0 } function build() {