mirror of
https://github.com/ttttupup/wxhelper.git
synced 2024-11-05 18:09:24 +08:00
重构
This commit is contained in:
parent
61c9e6f8c1
commit
384aade1a8
@ -14,7 +14,7 @@ file(GLOB CPP_FILES ${PROJECT_SOURCE_DIR}/src/*.cc ${PROJECT_SOURCE_DIR}/src/*
|
||||
include_directories(${VCPKG_INSTALLED_DIR}/x86-windows/include)
|
||||
|
||||
# add_subdirectory(3rd)
|
||||
add_subdirectory(source)
|
||||
# add_subdirectory(source)
|
||||
|
||||
find_package(nlohmann_json CONFIG REQUIRED)
|
||||
find_package(unofficial-mongoose CONFIG REQUIRED)
|
||||
|
@ -31,7 +31,7 @@
|
||||
using namespace std;
|
||||
using namespace nlohmann;
|
||||
|
||||
#define STR2INT(str) (is_digit(str) ? stoi(str) : 0)
|
||||
// #define STR2INT(str) (is_digit(str) ? stoi(str) : 0)
|
||||
#define WS2LW(wstr) (LPWSTR) wstr.c_str()
|
||||
|
||||
static bool kHttpRuning = false;
|
||||
|
@ -188,3 +188,16 @@ void Hex2Bytes(const std::string &hex, BYTE *bytes) {
|
||||
bytes[i] = n;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool IsDigit(string str) {
|
||||
if (str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
for (auto it : str) {
|
||||
if (it < '0' || it > '9') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
#define COMMON_H_
|
||||
#include <string>
|
||||
#define READ_WSTRING(addr, offset) ((*(DWORD *)(addr + offset + 0x4) == 0) ? std::wstring(L"") : std::wstring((wchar_t *)(*(DWORD *)(addr + offset)), *(DWORD *)(addr + offset + 0x4)))
|
||||
|
||||
#define STR2INT(str) (IsDigit(str) ? stoi(str) : 0)
|
||||
/// @brief utf8 转换成unicode
|
||||
/// @param buffer utf8
|
||||
/// @return unicode
|
||||
@ -63,6 +63,8 @@ std::string Bytes2Hex(const BYTE *bytes, const int length);
|
||||
|
||||
void Hex2Bytes(const std::string &hex, BYTE *bytes);
|
||||
|
||||
bool IsDigit(std::string str);
|
||||
|
||||
template <typename T1, typename T2>
|
||||
std::vector<T1> split(T1 str, T2 letter) {
|
||||
vector<T1> arr;
|
||||
|
@ -1,12 +1,14 @@
|
||||
#include "pch.h"
|
||||
#include "api.h"
|
||||
#include "common.h"
|
||||
#include "wxhelper.h"
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
|
||||
LPVOID lpReserved) {
|
||||
switch (ul_reason_for_call) {
|
||||
case DLL_PROCESS_ATTACH: {
|
||||
http_start(19088);
|
||||
// http_start(19088);
|
||||
wxhelper::WXHelper::GetInstance().http_start(19088);
|
||||
break;
|
||||
}
|
||||
case DLL_THREAD_ATTACH: {
|
||||
@ -17,7 +19,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
|
||||
}
|
||||
case DLL_PROCESS_DETACH: {
|
||||
CloseConsole();
|
||||
http_close();
|
||||
// http_close();
|
||||
wxhelper::WXHelper::GetInstance().http_close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
86
src/http_server.cc
Normal file
86
src/http_server.cc
Normal file
@ -0,0 +1,86 @@
|
||||
#include "http_server.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "api_route.h"
|
||||
#include "common.h"
|
||||
#include "pch.h"
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
using namespace std;
|
||||
using namespace nlohmann;
|
||||
|
||||
namespace wxhelper {
|
||||
HttpServer& HttpServer::GetInstance() {
|
||||
static HttpServer p;
|
||||
return p;
|
||||
}
|
||||
|
||||
void HttpServer::Init(int port) {
|
||||
port_ = port;
|
||||
running_ = false;
|
||||
http_handler_ = new HttpHandler();
|
||||
mg_mgr_init(&mgr_);
|
||||
|
||||
}
|
||||
|
||||
HttpServer::~HttpServer() {
|
||||
mg_mgr_free(&mgr_);
|
||||
delete http_handler_;
|
||||
}
|
||||
|
||||
bool HttpServer::HttpStart() {
|
||||
if (running_) {
|
||||
return true;
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
CreateConsole();
|
||||
#endif
|
||||
running_ = true;
|
||||
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StartHttpServer, NULL,
|
||||
NULL, 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
void HttpServer::StartHttpServer() {
|
||||
string lsten_addr = "http://0.0.0.0:" + to_string(GetInstance().port_);
|
||||
mg_http_listen(&GetInstance().mgr_, lsten_addr.c_str(), EventHandler,
|
||||
&GetInstance().mgr_);
|
||||
for (;;) mg_mgr_poll(&GetInstance().mgr_, 1000);
|
||||
}
|
||||
|
||||
void HttpServer::EventHandler(struct mg_connection *c,
|
||||
int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
|
||||
if (mg_http_match_uri(hm, "/websocket")) {
|
||||
mg_ws_upgrade(c, hm, NULL);
|
||||
} else if (mg_http_match_uri(hm, "/api/")) {
|
||||
GetInstance().HandleHttpRequest(c,hm);
|
||||
} else {
|
||||
mg_http_reply(c, 500, NULL, "%s", "Invalid URI");
|
||||
}
|
||||
} else if (ev == MG_EV_WS_MSG) {
|
||||
|
||||
GetInstance().HandleWebsocketRequest(c,ev_data);
|
||||
}
|
||||
(void)fn_data;
|
||||
}
|
||||
|
||||
void HttpServer::HandleHttpRequest(struct mg_connection *c,
|
||||
void *ev_data) {
|
||||
|
||||
http_handler_->HandlerRequest(c,ev_data);
|
||||
}
|
||||
|
||||
void HttpServer::HandleWebsocketRequest(struct mg_connection *c,
|
||||
void *ev_data) {
|
||||
// Got websocket frame. Received data is wm->data. Echo it back!
|
||||
struct mg_ws_message *wm = (struct mg_ws_message *)ev_data;
|
||||
mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
|
||||
}
|
||||
|
||||
bool HttpServer::HttpClose() { return false; }
|
||||
|
||||
} // namespace wxhelper
|
36
src/http_server.h
Normal file
36
src/http_server.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef WXHELPER_HTTP_SERVER_H_
|
||||
#define WXHELPER_HTTP_SERVER_H_
|
||||
|
||||
#include <mongoose.h>
|
||||
#include "http_handler.h"
|
||||
namespace wxhelper {
|
||||
class HttpServer {
|
||||
|
||||
|
||||
public:
|
||||
|
||||
static HttpServer &GetInstance();
|
||||
bool HttpStart();
|
||||
bool HttpClose();
|
||||
void Init(int port);
|
||||
|
||||
private:
|
||||
HttpServer(){}
|
||||
HttpServer(const HttpServer &) = delete;
|
||||
HttpServer &operator=(const HttpServer &) = delete;
|
||||
~HttpServer();
|
||||
static void StartHttpServer();
|
||||
static void EventHandler(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
|
||||
void HandleHttpRequest(struct mg_connection *c, void *ev_data);
|
||||
void HandleWebsocketRequest(struct mg_connection *c, void *ev_data);
|
||||
|
||||
|
||||
private:
|
||||
int port_;
|
||||
bool running_;
|
||||
struct mg_mgr mgr_;
|
||||
HttpHandler* http_handler_;
|
||||
};
|
||||
} // namespace wxhelper
|
||||
|
||||
#endif
|
24
src/wxhelper.cc
Normal file
24
src/wxhelper.cc
Normal file
@ -0,0 +1,24 @@
|
||||
#include "wxhelper.h"
|
||||
|
||||
#include <mongoose.h>
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
namespace wxhelper {
|
||||
WXHelper& WXHelper::GetInstance() {
|
||||
static WXHelper p;
|
||||
return p;
|
||||
}
|
||||
|
||||
int WXHelper::http_start(int port) {
|
||||
HttpServer::GetInstance().Init(port);
|
||||
bool ret = HttpServer::GetInstance().HttpStart();
|
||||
return ret == true ? 1 : 0;
|
||||
}
|
||||
|
||||
int WXHelper::http_close() {
|
||||
bool ret = HttpServer::GetInstance().HttpClose();
|
||||
return ret == true ? 1 : 0;
|
||||
}
|
||||
|
||||
} // namespace wxhelper
|
21
src/wxhelper.h
Normal file
21
src/wxhelper.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef WXHELPER_WXHELPER_H_
|
||||
#define WXHELPER_WXHELPER_H_
|
||||
#include "http_server.h"
|
||||
namespace wxhelper {
|
||||
|
||||
class WXHelper {
|
||||
public:
|
||||
static WXHelper &GetInstance();
|
||||
|
||||
int http_start(int port);
|
||||
int http_close();
|
||||
|
||||
private:
|
||||
WXHelper(){};
|
||||
WXHelper(const WXHelper &) = delete;
|
||||
WXHelper &operator=(const WXHelper &) = delete;
|
||||
~WXHelper(){};
|
||||
};
|
||||
} // namespace wxhelper
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user