| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- // AsioUDPNetDLL.cpp : 定义 DLL 应用程序的导出函数。
- #ifdef _WIN32
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #else
- #include <dlfcn.h>
- #endif
- #include "AsioUDPNetDLL.h"
- #include "UDPServer.h"
- #include "CustomLog.h"
- #include "ConfigReader.h"
- #include "asio.hpp"
- #include <thread>
- #include <string>
- using asio::ip::udp;
- asio::io_context io_context;
- UDPServer* g_pUDPServer = nullptr;
- std::thread g_Thread;
- ConfigReader g_Config;
- std::string getExecutablePath() {
- #ifdef _WIN32
- char path[MAX_PATH];
- GetModuleFileNameA(NULL, path, MAX_PATH);
- std::string fullPath(path);
- return fullPath.substr(0, fullPath.find_last_of("\\/"));
- #else
- char path[PATH_MAX];
- ssize_t count = readlink("/proc/self/exe", path, PATH_MAX);
- if (count <= 0) return "";
- std::string fullPath(path, count);
- return fullPath.substr(0, fullPath.find_last_of("/"));
- #endif
- }
- bool CreateFilePath(const std::string& logDirName) {
- std::string workPath = getExecutablePath();
- if (workPath.empty()) {
- std::cerr << "Failed to get executable path" << std::endl;
- return false;
- }
- std::string logDir = workPath + "/" + logDirName;
- return CustomLogger::getInstance().initialize(logDir);
- }
- void InitParam() {
- std::string configPath = getExecutablePath() + "/config";
- if (!g_Config.load(configPath)) {
- LOG_WARNING("Failed to load config file: " + configPath);
- }
- }
- extern "C" bool NetDll_InitUDPNet() {
- try {
- if (g_pUDPServer) {
- LOG_WARNING("已初始化过网络模块跳过初始化。");
- return false;
- }
- std::string configPath = getExecutablePath() + "/config";
- if (!g_Config.load(configPath)) {
- LOG_WARNING("Failed to load config file: " + configPath);
- }
- int nLocalPort = g_Config.getInt("NetData.LocalPort", 12000);
- int nRemoteSize = g_Config.getInt("NetData.RemoteCount", 0);
- if (nRemoteSize==0)
- {
- return false;
- }
- g_pUDPServer = new UDPServer(io_context, nLocalPort);
- if (nRemoteSize <= 1)
- {
- std::string remoteIP = g_Config.getString("NetData.RemoteIP", "127.0.0.1");
- int nRemotePort = g_Config.getInt("NetData.RemotePort", 12001);
- LOG_INFO("读取配置:本地localPort:" + std::to_string(nLocalPort) +
- " 远端RemoteIP:" + remoteIP +
- " 远端RemotePort" + std::to_string(nRemotePort));
- g_pUDPServer->addSendTarget(remoteIP.c_str(), nRemotePort);
- }
- else
- {
- for (int nIndex=0;nIndex<nRemoteSize;nIndex++)
- {
- std::string strNumber = std::to_string(nIndex+1);
- std::string remoteIP = g_Config.getString("NetData.RemoteIP"+ strNumber, "127.0.0.1");
- int nRemotePort = g_Config.getInt("NetData.RemotePort"+ strNumber, 12001);
- g_pUDPServer->addSendTarget(remoteIP.c_str(), nRemotePort);
- }
- }
- LOG_INFO("始化过网络模块");
- return true;
- }
- catch (std::exception& e) {
- LOG_ERROR("异常: " + std::string(e.what()));
- return false;
- }
- return false;
- }
- extern "C" bool NetDll_DestroyUDPNet() {
- if (g_pUDPServer) {
- NetDll_StopThread();
- delete g_pUDPServer;
- g_pUDPServer = nullptr;
- LOG_INFO("网络模块销毁成功。");
- return true;
- }
- LOG_WARNING("网络模块已销毁跳过销毁。");
- return false;
- }
- extern "C" void NetDll_StartThread() {
- g_Thread = std::thread([]() {
- io_context.run();
- });
- return;
- }
- extern "C" void NetDll_StopThread() {
- io_context.stop();
- if (g_Thread.joinable()) {
- g_Thread.join();
- }
- return;
- }
- extern "C" bool NetDll_SendUDPData(HDDSType nType, void* pData, long nLen) {
- if (!g_pUDPServer) {
- LOG_WARNING("未初始化网络");
- return false;
- }
- HDDSCMD pCmd;
- pCmd.flags = 0;
- if (pCmd.SetcData(nType, pData, nLen)) {
- if (g_pUDPServer) {
- g_pUDPServer->blockingSendData(&pCmd, pCmd.GetBufLen());
- return true;
- }
- LOG_WARNING("未初始化网络");
- return true;
- }
- else {
- LOG_WARNING("数据包长度超出网络包大小");
- return false;
- }
- }
- extern "C" bool NetDll_SetNetQueue(HDDSCMDQueue * pCMDQueue) {
- if (!g_pUDPServer) {
- LOG_WARNING("未初始化网络");
- return false;
- }
- g_pUDPServer->setCMDQueue(pCMDQueue);
- return true;
- }
- extern "C" bool NetDll_LibraryInit()
- {
- CreateFilePath("logs");
- InitParam();
- return true;
- }
- extern "C" void NetDll_LibraryCleanup()
- {
- NetDll_DestroyUDPNet();
- CustomLogger::getInstance().shutdown();
- }
- // 跨平台的库初始化和清理
- #ifdef _WIN32
- BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
- switch (ul_reason_for_call) {
- case DLL_PROCESS_ATTACH:
- CreateFilePath("logs");
- InitParam();
- NetDll_InitUDPNet();
- break;
- case DLL_PROCESS_DETACH:
- NetDll_DestroyUDPNet();
- CustomLogger::getInstance().shutdown();
- break;
- }
- return TRUE;
- }
- #else
- // Linux 共享库构造函数和析构函数
- /*
- __attribute__((constructor))
- static void library_init() {
- CreateFilePath("logs");
- InitParam();
- NetDll_InitUDPNet();
- }
- __attribute__((destructor))
- static void library_cleanup() {
- NetDll_DestroyUDPNet();
- CustomLogger::getInstance().shutdown();
- }*/
- #endif
- extern "C" bool NetDll_SetMultiSendParams(UDPTargetConfig * targets, int count) {
- if (!g_pUDPServer) {
- LOG_WARNING("网络未初始化");
- return false;
- }
- if (!targets || count <= 0) {
- LOG_WARNING("无效的目标参数");
- return false;
- }
- std::vector<UDPTarget> udp_targets;
- for (int i = 0; i < count; i++) {
- UDPTarget target;
- target.ip = targets[i].ip;
- target.port = targets[i].port;
- target.resolved = false;
- udp_targets.push_back(target);
- }
- g_pUDPServer->setMultiSendParams(udp_targets);
- return true;
- }
- extern "C" bool NetDll_AddSendTarget(const char* hostIP, int nPort) {
- if (!g_pUDPServer) {
- LOG_WARNING("网络未初始化");
- return false;
- }
- if (!hostIP || nPort <= 0) {
- LOG_WARNING("无效的目标参数");
- return false;
- }
- g_pUDPServer->addSendTarget(hostIP, nPort);
- return true;
- }
- extern "C" bool NetDll_RemoveSendTarget(const char* hostIP, int nPort) {
- if (!g_pUDPServer) {
- LOG_WARNING("网络未初始化");
- return false;
- }
- if (!hostIP || nPort <= 0) {
- LOG_WARNING("无效的目标参数");
- return false;
- }
- g_pUDPServer->removeSendTarget(hostIP, nPort);
- return true;
- }
- extern "C" void NetDll_ClearSendTargets() {
- if (g_pUDPServer) {
- g_pUDPServer->clearSendTargets();
- }
- }
- extern "C" bool NetDll_SendUDPDataToMulti(HDDSType nType, void* pData, long nLen) {
- if (!g_pUDPServer) {
- LOG_WARNING("网络未初始化");
- return false;
- }
- HDDSCMD pCmd;
- pCmd.flags = 0;
- if (pCmd.SetcData(nType, pData, nLen)) {
- int result = g_pUDPServer->blockingSendDataToMulti(&pCmd, pCmd.GetBufLen());
- return result > 0;
- }
- else {
- LOG_WARNING("数据格式错误无法发送网络包");
- return false;
- }
- }
|