muti thread

This commit is contained in:
Simplxs 2024-05-07 18:14:31 +08:00
parent d4a43b89b3
commit ea42c03794
No known key found for this signature in database
GPG Key ID: E23537FF14DD6507
8 changed files with 9834 additions and 20604 deletions

View File

@ -21,7 +21,7 @@ set(CMAKE_CXX_STANDARD 17)
file(GLOB SOURCE_FILES "./src/*.cpp" "./src/*.asm")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} "./include/mongoose/mongoose.c")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
if(MSVC)
target_link_options(SignerServer PRIVATE

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,3 @@
#include "server.h"
Server server(8080);
Server server(1145);

View File

@ -7,74 +7,41 @@
Sign sign;
// HTTP server event handler function
void ev_handler(struct mg_connection *c, int ev, void *ev_data)
{
if (ev == MG_EV_HTTP_MSG)
{
struct mg_http_message *hm = reinterpret_cast<mg_http_message *>(ev_data);
if (mg_match(hm->uri, mg_str("/sign"), NULL))
{
try
{
rapidjson::Document doc;
doc.Parse(hm->body.buf, hm->body.len);
std::string_view cmd = doc["cmd"].GetString();
std::string_view src = doc["src"].GetString();
int seq = doc["seq"].GetInt64();
auto [signDataHex, extraDataHex, tokenDataHex] = sign.Call(cmd, src, seq);
// Construct response
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("sign");
writer.String(signDataHex.c_str());
writer.Key("extra");
writer.String(extraDataHex.c_str());
writer.Key("token");
writer.String(tokenDataHex.c_str());
writer.EndObject();
mg_http_reply(c, 200, NULL, buffer.GetString());
}
catch (...)
{
mg_http_reply(c, 400, NULL, "400 Bad Request");
return;
}
}
else
{
mg_http_reply(c, 404, NULL, "404 Not Found");
}
// Log request
// MG_INFO(("%.*s %.*s %lu -> %.*s %lu", hm->method.len, hm->method.buf,
// hm->uri.len, hm->uri.buf, hm->body.len, 3, c->send.buf + 9,
// c->send.len));
}
}
Server::Server(int port)
{
char url[32];
snprintf(url, sizeof(url), "http://0.0.0.0:%d", port);
svr.Post("/sign", [](const httplib::Request &req, httplib::Response &res)
{
try
{
rapidjson::Document doc;
doc.Parse(req.body.c_str(), req.body.size());
mg_mgr_init(&mgr); // Initialise event manager
mg_http_listen(&mgr, url, ev_handler, NULL); // Setup listener
std::string_view cmd = doc["cmd"].GetString();
std::string_view src = doc["src"].GetString();
int seq = doc["seq"].GetInt64();
// new thread to loop
std::thread t(&Server::Loop, this);
t.detach();
}
auto [signDataHex, extraDataHex, tokenDataHex] = sign.Call(cmd, src, seq);
void Server::Loop()
{
for (;;)
{ // Run an infinite event loop
mg_mgr_poll(&mgr, 1000);
}
}
// Construct response
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("sign");
writer.String(signDataHex.c_str());
writer.Key("extra");
writer.String(extraDataHex.c_str());
writer.Key("token");
writer.String(tokenDataHex.c_str());
writer.EndObject();
res.set_content(buffer.GetString(), "application/json");
}
catch (...)
{
res.set_content("Bad Request", "text/plain");
res.status = httplib::StatusCode::BadRequest_400;
} });
std::thread([this, port]
{ svr.listen("0.0.0.0", port); }).detach();
}

View File

@ -1,4 +1,4 @@
#include "../include/mongoose/mongoose.h"
#include "../include/cpp-httplib/httplib.h"
class Server
{
@ -6,8 +6,5 @@ public:
Server(int port);
private:
struct mg_mgr mgr; // Declare event manager
private:
void Loop();
httplib::Server svr;
};

View File

@ -45,7 +45,7 @@ std::map<std::string, uint64_t> addrMap = {
{"6.9.20-17153", 0x1c73dd0}};
#endif
#elif defined(_LINUX_PLATFORM_)
#define CURRENT_VERSION "3.2.7-23361"
#define CURRENT_VERSION "3.1.2-13107"
#if defined(_X64_ARCH_)
std::map<std::string, uint64_t> addrMap = {
{"3.1.2-12912", 0x33C38E0},
@ -53,7 +53,7 @@ std::map<std::string, uint64_t> addrMap = {
{"3.2.7-23361", 0x4C93C57}};
#endif
#endif
int SignOffsets = 767; // 562 in 3.1.2-12912
int SignOffsets = 562; // 562 in 3.1.2-12912
int ExtraOffsets = 511;
int TokenOffsets = 255;
@ -150,9 +150,9 @@ std::tuple<std::string, std::string, std::string> Sign::Call(const std::string_v
if (SignFunction == nullptr)
throw std::runtime_error("Sign function not initialized");
printf("cmd: %s\n", cmd.data());
printf("src: %s\n", src.data());
printf("seq: %d\n", seq);
// printf("cmd: %s\n", cmd.data());
// printf("src: %s\n", src.data());
// printf("seq: %d\n", seq);
const std::vector<uint8_t> signArgSrc = Hex2Bin(src);
@ -161,15 +161,11 @@ std::tuple<std::string, std::string, std::string> Sign::Call(const std::string_v
SignFunction(cmd.data(), signArgSrc.data(), signArgSrc.size(), seq, signResult);
printf("signResult: %s\n", Bin2Hex(signResult, resultSize).c_str());
// printf("signResult: %s\n", Bin2Hex(signResult, resultSize).c_str());
// 获取大小
uint32_t signSizeU32 = *(signResult + SignOffsets);
uint32_t extraSizeU32 = *(signResult + ExtraOffsets);
uint32_t tokenSizeU32 = *(signResult + TokenOffsets);
std::string signDataHex = Bin2Hex(signResult + 512, signSizeU32);
std::string extraDataHex = Bin2Hex(signResult + 256, extraSizeU32);
std::string tokenDataHex = Bin2Hex(signResult, tokenSizeU32);
std::string signDataHex = Bin2Hex(signResult + 512, *(signResult + SignOffsets));
std::string extraDataHex = Bin2Hex(signResult + 256, *(signResult + ExtraOffsets));
std::string tokenDataHex = Bin2Hex(signResult, *(signResult + TokenOffsets));
return std::make_tuple(signDataHex, extraDataHex, tokenDataHex);
}