AsioUDPNetDLL.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // AsioUDPNetDLL.cpp : 定义 DLL 应用程序的导出函数。
  2. #ifdef _WIN32
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <windows.h>
  5. #else
  6. #include <dlfcn.h>
  7. #endif
  8. #include "AsioUDPNetDLL.h"
  9. #include "UDPServer.h"
  10. #include "CustomLog.h"
  11. #include "ConfigReader.h"
  12. #include "asio.hpp"
  13. #include <thread>
  14. #include <string>
  15. using asio::ip::udp;
  16. asio::io_context io_context;
  17. UDPServer* g_pUDPServer = nullptr;
  18. std::thread g_Thread;
  19. ConfigReader g_Config;
  20. std::string getExecutablePath() {
  21. #ifdef _WIN32
  22. char path[MAX_PATH];
  23. GetModuleFileNameA(NULL, path, MAX_PATH);
  24. std::string fullPath(path);
  25. return fullPath.substr(0, fullPath.find_last_of("\\/"));
  26. #else
  27. char path[PATH_MAX];
  28. ssize_t count = readlink("/proc/self/exe", path, PATH_MAX);
  29. if (count <= 0) return "";
  30. std::string fullPath(path, count);
  31. return fullPath.substr(0, fullPath.find_last_of("/"));
  32. #endif
  33. }
  34. bool CreateFilePath(const std::string& logDirName) {
  35. std::string workPath = getExecutablePath();
  36. if (workPath.empty()) {
  37. std::cerr << "Failed to get executable path" << std::endl;
  38. return false;
  39. }
  40. std::string logDir = workPath + "/" + logDirName;
  41. return CustomLogger::getInstance().initialize(logDir);
  42. }
  43. void InitParam() {
  44. std::string configPath = getExecutablePath() + "/config";
  45. if (!g_Config.load(configPath)) {
  46. LOG_WARNING("Failed to load config file: " + configPath);
  47. }
  48. }
  49. extern "C" bool NetDll_InitUDPNet() {
  50. try {
  51. if (g_pUDPServer) {
  52. LOG_WARNING("已初始化过网络模块跳过初始化。");
  53. return false;
  54. }
  55. std::string configPath = getExecutablePath() + "/config";
  56. if (!g_Config.load(configPath)) {
  57. LOG_WARNING("Failed to load config file: " + configPath);
  58. }
  59. int nLocalPort = g_Config.getInt("NetData.LocalPort", 12000);
  60. int nRemoteSize = g_Config.getInt("NetData.RemoteCount", 0);
  61. if (nRemoteSize==0)
  62. {
  63. return false;
  64. }
  65. g_pUDPServer = new UDPServer(io_context, nLocalPort);
  66. if (nRemoteSize <= 1)
  67. {
  68. std::string remoteIP = g_Config.getString("NetData.RemoteIP", "127.0.0.1");
  69. int nRemotePort = g_Config.getInt("NetData.RemotePort", 12001);
  70. LOG_INFO("读取配置:本地localPort:" + std::to_string(nLocalPort) +
  71. " 远端RemoteIP:" + remoteIP +
  72. " 远端RemotePort" + std::to_string(nRemotePort));
  73. g_pUDPServer->addSendTarget(remoteIP.c_str(), nRemotePort);
  74. }
  75. else
  76. {
  77. for (int nIndex=0;nIndex<nRemoteSize;nIndex++)
  78. {
  79. std::string strNumber = std::to_string(nIndex+1);
  80. std::string remoteIP = g_Config.getString("NetData.RemoteIP"+ strNumber, "127.0.0.1");
  81. int nRemotePort = g_Config.getInt("NetData.RemotePort"+ strNumber, 12001);
  82. g_pUDPServer->addSendTarget(remoteIP.c_str(), nRemotePort);
  83. }
  84. }
  85. LOG_INFO("始化过网络模块");
  86. return true;
  87. }
  88. catch (std::exception& e) {
  89. LOG_ERROR("异常: " + std::string(e.what()));
  90. return false;
  91. }
  92. return false;
  93. }
  94. extern "C" bool NetDll_DestroyUDPNet() {
  95. if (g_pUDPServer) {
  96. NetDll_StopThread();
  97. delete g_pUDPServer;
  98. g_pUDPServer = nullptr;
  99. LOG_INFO("网络模块销毁成功。");
  100. return true;
  101. }
  102. LOG_WARNING("网络模块已销毁跳过销毁。");
  103. return false;
  104. }
  105. extern "C" void NetDll_StartThread() {
  106. g_Thread = std::thread([]() {
  107. io_context.run();
  108. });
  109. return;
  110. }
  111. extern "C" void NetDll_StopThread() {
  112. io_context.stop();
  113. if (g_Thread.joinable()) {
  114. g_Thread.join();
  115. }
  116. return;
  117. }
  118. extern "C" bool NetDll_SendUDPData(HDDSType nType, void* pData, long nLen) {
  119. if (!g_pUDPServer) {
  120. LOG_WARNING("未初始化网络");
  121. return false;
  122. }
  123. HDDSCMD pCmd;
  124. pCmd.flags = 0;
  125. if (pCmd.SetcData(nType, pData, nLen)) {
  126. if (g_pUDPServer) {
  127. g_pUDPServer->blockingSendData(&pCmd, pCmd.GetBufLen());
  128. return true;
  129. }
  130. LOG_WARNING("未初始化网络");
  131. return true;
  132. }
  133. else {
  134. LOG_WARNING("数据包长度超出网络包大小");
  135. return false;
  136. }
  137. }
  138. extern "C" bool NetDll_SetNetQueue(HDDSCMDQueue * pCMDQueue) {
  139. if (!g_pUDPServer) {
  140. LOG_WARNING("未初始化网络");
  141. return false;
  142. }
  143. g_pUDPServer->setCMDQueue(pCMDQueue);
  144. return true;
  145. }
  146. extern "C" bool NetDll_LibraryInit()
  147. {
  148. CreateFilePath("logs");
  149. InitParam();
  150. return true;
  151. }
  152. extern "C" void NetDll_LibraryCleanup()
  153. {
  154. NetDll_DestroyUDPNet();
  155. CustomLogger::getInstance().shutdown();
  156. }
  157. // 跨平台的库初始化和清理
  158. #ifdef _WIN32
  159. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
  160. switch (ul_reason_for_call) {
  161. case DLL_PROCESS_ATTACH:
  162. CreateFilePath("logs");
  163. InitParam();
  164. NetDll_InitUDPNet();
  165. break;
  166. case DLL_PROCESS_DETACH:
  167. NetDll_DestroyUDPNet();
  168. CustomLogger::getInstance().shutdown();
  169. break;
  170. }
  171. return TRUE;
  172. }
  173. #else
  174. // Linux 共享库构造函数和析构函数
  175. /*
  176. __attribute__((constructor))
  177. static void library_init() {
  178. CreateFilePath("logs");
  179. InitParam();
  180. NetDll_InitUDPNet();
  181. }
  182. __attribute__((destructor))
  183. static void library_cleanup() {
  184. NetDll_DestroyUDPNet();
  185. CustomLogger::getInstance().shutdown();
  186. }*/
  187. #endif
  188. extern "C" bool NetDll_SetMultiSendParams(UDPTargetConfig * targets, int count) {
  189. if (!g_pUDPServer) {
  190. LOG_WARNING("网络未初始化");
  191. return false;
  192. }
  193. if (!targets || count <= 0) {
  194. LOG_WARNING("无效的目标参数");
  195. return false;
  196. }
  197. std::vector<UDPTarget> udp_targets;
  198. for (int i = 0; i < count; i++) {
  199. UDPTarget target;
  200. target.ip = targets[i].ip;
  201. target.port = targets[i].port;
  202. target.resolved = false;
  203. udp_targets.push_back(target);
  204. }
  205. g_pUDPServer->setMultiSendParams(udp_targets);
  206. return true;
  207. }
  208. extern "C" bool NetDll_AddSendTarget(const char* hostIP, int nPort) {
  209. if (!g_pUDPServer) {
  210. LOG_WARNING("网络未初始化");
  211. return false;
  212. }
  213. if (!hostIP || nPort <= 0) {
  214. LOG_WARNING("无效的目标参数");
  215. return false;
  216. }
  217. g_pUDPServer->addSendTarget(hostIP, nPort);
  218. return true;
  219. }
  220. extern "C" bool NetDll_RemoveSendTarget(const char* hostIP, int nPort) {
  221. if (!g_pUDPServer) {
  222. LOG_WARNING("网络未初始化");
  223. return false;
  224. }
  225. if (!hostIP || nPort <= 0) {
  226. LOG_WARNING("无效的目标参数");
  227. return false;
  228. }
  229. g_pUDPServer->removeSendTarget(hostIP, nPort);
  230. return true;
  231. }
  232. extern "C" void NetDll_ClearSendTargets() {
  233. if (g_pUDPServer) {
  234. g_pUDPServer->clearSendTargets();
  235. }
  236. }
  237. extern "C" bool NetDll_SendUDPDataToMulti(HDDSType nType, void* pData, long nLen) {
  238. if (!g_pUDPServer) {
  239. LOG_WARNING("网络未初始化");
  240. return false;
  241. }
  242. HDDSCMD pCmd;
  243. pCmd.flags = 0;
  244. if (pCmd.SetcData(nType, pData, nLen)) {
  245. int result = g_pUDPServer->blockingSendDataToMulti(&pCmd, pCmd.GetBufLen());
  246. return result > 0;
  247. }
  248. else {
  249. LOG_WARNING("数据格式错误无法发送网络包");
  250. return false;
  251. }
  252. }