add nng code.
Some checks failed
Deploy / Build (push) Failing after 21s

This commit is contained in:
amass 2025-05-11 15:17:48 +08:00
parent 334edbddfd
commit 836c8e0c44
7 changed files with 49 additions and 3 deletions

1
.gitignore vendored
View File

@ -41,4 +41,5 @@ target_wrapper.*
# QtCreator CMake
CMakeLists.txt.user*
build
logs
.DS_Store

View File

@ -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",

View File

@ -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<char>();
std::string_view data("i am older server.");
reply->send(data.data(), data.size());
asyncReply();
});
}
std::shared_ptr<boost::urls::router<Application::RequestHandler>> router;
std::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor;
std::unique_ptr<Nng::Socket> reply;
};
Application::Application() : m_d{new ApplicationPrivate()} {
@ -47,6 +63,16 @@ Application::Application() : m_d{new ApplicationPrivate()} {
}
session.reply(ServiceLogic::make_200<boost::beast::http::string_body>(request, "notify successed.\n", "text/html"));
});
m_d->reply = std::make_unique<Nng::Socket>(*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::ip::tcp::socket>(boost::asio::make_strand(*m_ioContext->ioContext()));
m_d->acceptor->async_accept(*socket, [self{shared_from_this()}, socket](const boost::system::error_code &error) {

View File

@ -32,16 +32,19 @@ public:
using Request = boost::beast::http::request<boost::beast::http::string_body>;
using RequestHandler = std::function<void(HttpSession &, const Request &, const boost::urls::matches &)>;
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<Settings> m_settings;
std::shared_ptr<Core::IoContext> m_ioContext;
std::shared_ptr<Core::MessageManager> m_messageManager;

View File

@ -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)

View File

@ -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();

View File

@ -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() {