From 1fce74e06ebbb5a24efc2c19bcf7b6e67a7c0266 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Fri, 7 Feb 2025 18:59:15 +0800 Subject: [PATCH] fix #46 Using ZSTD context to improve performance --- client/ClientDll.cpp | 14 +++++++++----- client/IOCPClient.cpp | 15 +++++++++++++++ client/IOCPClient.h | 9 ++++++++- client/StdAfx.h | 6 ++++++ common/commands.h | 2 +- server/2015Remote/IOCPServer.cpp | 16 ++++++++++++++++ server/2015Remote/IOCPServer.h | 8 ++++++++ server/2015Remote/stdafx.h | 4 ++++ 8 files changed, 67 insertions(+), 7 deletions(-) diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index 57fcc79..fa0687d 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -288,14 +288,18 @@ extern "C" __declspec(dllexport) void Run(HWND hwnd, HINSTANCE hinst, LPSTR lpsz return; } - do - { + do { TestRun((char*)result[0].c_str(), atoi(result[1].c_str())); while (!IsStoped()) Sleep(20); - } while (!IsExit()); - if (IsExit() == 1) - return; + if (g_bExit == 1) + return; + else if (g_bExit == 2) + continue; + else // 3 + break; + } while (true); + sprintf_s(message, "%s:%d", g_SETTINGS.ServerIP(), g_SETTINGS.ServerPort()); RunNewDll(message); } diff --git a/client/IOCPClient.cpp b/client/IOCPClient.cpp index 2738a38..d6a51ce 100644 --- a/client/IOCPClient.cpp +++ b/client/IOCPClient.cpp @@ -26,10 +26,16 @@ #endif #define Z_FAILED(p) ZSTD_isError(p) #define Z_SUCCESS(p) (!Z_FAILED(p)) +#define ZSTD_CLEVEL 5 +#if USING_CTX +#define compress(dest, destLen, source, sourceLen) ZSTD_compress2(m_Cctx, dest, *(destLen), source, sourceLen) +#define uncompress(dest, destLen, source, sourceLen) ZSTD_decompressDCtx(m_Dctx, dest, *(destLen), source, sourceLen) +#else #define compress(dest, destLen, source, sourceLen) ZSTD_compress(dest, *(destLen), source, sourceLen, ZSTD_CLEVEL_DEFAULT) #define uncompress(dest, destLen, source, sourceLen) ZSTD_decompress(dest, *(destLen), source, sourceLen) #endif #endif +#endif #include #include "Manager.h" @@ -63,6 +69,11 @@ IOCPClient::IOCPClient(bool exit_while_disconnect) InitializeCriticalSection(&m_cs); m_exit_while_disconnect = exit_while_disconnect; +#if USING_CTX + m_Cctx = ZSTD_createCCtx(); + m_Dctx = ZSTD_createDCtx(); + ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_compressionLevel, ZSTD_CLEVEL); +#endif } IOCPClient::~IOCPClient() @@ -89,6 +100,10 @@ IOCPClient::~IOCPClient() DeleteCriticalSection(&m_cs); m_bWorkThread = S_END; +#if USING_CTX + ZSTD_freeCCtx(m_Cctx); + ZSTD_freeDCtx(m_Dctx); +#endif } // 从域名获取IP地址 diff --git a/client/IOCPClient.h b/client/IOCPClient.h index ae55eb3..c6fff2e 100644 --- a/client/IOCPClient.h +++ b/client/IOCPClient.h @@ -15,6 +15,10 @@ #include "Buffer.h" #include "Manager.h" +#if USING_CTX +#include "zstd/zstd.h" +#endif + #pragma comment(lib,"ws2_32.lib") #define MAX_RECV_BUFFER 1024*32 @@ -33,7 +37,10 @@ public: CBuffer m_CompressedBuffer; BOOL m_bWorkThread; HANDLE m_hWorkThread; - +#if USING_CTX + ZSTD_CCtx* m_Cctx; // 压缩上下文 + ZSTD_DCtx* m_Dctx; // 解压上下文 +#endif BOOL ConnectServer(const char* szServerIP, unsigned short uPort); static DWORD WINAPI WorkThreadProc(LPVOID lParam); diff --git a/client/StdAfx.h b/client/StdAfx.h index 2f91602..0342535 100644 --- a/client/StdAfx.h +++ b/client/StdAfx.h @@ -15,6 +15,12 @@ #if !USING_ZLIB // 是否使用LZ4 #define USING_LZ4 0 + +#if !USING_LZ4 +#define USING_ZSTD 1 +#define USING_CTX 1 +#endif + #endif #if _MSC_VER > 1000 diff --git a/common/commands.h b/common/commands.h index e460863..3754b3c 100644 --- a/common/commands.h +++ b/common/commands.h @@ -15,7 +15,7 @@ #define FLAG_GHOST 0x7654321 // 当程序功能明显发生变化时,应该更新这个值,以便对被控程序进行区分 -#define DLL_VERSION "20241229" // DLL版本 +#define DLL_VERSION __DATE__ // DLL版本 #define TALK_DLG_MAXLEN 1024 // 最大输入字符长度 diff --git a/server/2015Remote/IOCPServer.cpp b/server/2015Remote/IOCPServer.cpp index 8413d2f..8d84d09 100644 --- a/server/2015Remote/IOCPServer.cpp +++ b/server/2015Remote/IOCPServer.cpp @@ -25,10 +25,16 @@ #endif #define Z_FAILED(p) ZSTD_isError(p) #define Z_SUCCESS(p) (!Z_FAILED(p)) +#define ZSTD_CLEVEL 5 +#if USING_CTX +#define compress(dest, destLen, source, sourceLen) ZSTD_compress2(m_Cctx, dest, *(destLen), source, sourceLen) +#define uncompress(dest, destLen, source, sourceLen) ZSTD_decompressDCtx(m_Dctx, dest, *(destLen), source, sourceLen) +#else #define compress(dest, destLen, source, sourceLen) ZSTD_compress(dest, *(destLen), source, sourceLen, ZSTD_CLEVEL_DEFAULT) #define uncompress(dest, destLen, source, sourceLen) ZSTD_decompress(dest, *(destLen), source, sourceLen) #endif #endif +#endif using namespace std; CRITICAL_SECTION IOCPServer::m_cs = {0}; @@ -79,6 +85,11 @@ IOCPServer::IOCPServer(void) m_NotifyProc = NULL; m_OfflineProc = NULL; +#if USING_CTX + m_Cctx = ZSTD_createCCtx(); + m_Dctx = ZSTD_createDCtx(); + ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_compressionLevel, ZSTD_CLEVEL); +#endif } @@ -143,6 +154,11 @@ IOCPServer::~IOCPServer(void) m_ulBusyThread = 0; m_ulKeepLiveTime = 0; +#if USING_CTX + ZSTD_freeCCtx(m_Cctx); + ZSTD_freeDCtx(m_Dctx); +#endif + WSACleanup(); } diff --git a/server/2015Remote/IOCPServer.h b/server/2015Remote/IOCPServer.h index 1b90a0b..35f0df5 100644 --- a/server/2015Remote/IOCPServer.h +++ b/server/2015Remote/IOCPServer.h @@ -4,6 +4,9 @@ #pragma comment(lib,"ws2_32.lib") #include "CpuUseage.h" #include "Buffer.h" +#if USING_CTX +#include "zstd/zstd.h" +#endif #include #define PACKET_LENGTH 0x2000 @@ -78,6 +81,11 @@ public: ULONG m_ulCurrentThread; ULONG m_ulBusyThread; +#if USING_CTX + ZSTD_CCtx* m_Cctx; // 压缩上下文 + ZSTD_DCtx* m_Dctx; // 解压上下文 +#endif + CCpuUsage m_cpu; ULONG m_ulKeepLiveTime; diff --git a/server/2015Remote/stdafx.h b/server/2015Remote/stdafx.h index 194d46f..c4d1b2d 100644 --- a/server/2015Remote/stdafx.h +++ b/server/2015Remote/stdafx.h @@ -16,6 +16,10 @@ #if !USING_ZLIB // 是否使用LZ4 #define USING_LZ4 0 +#if !USING_LZ4 +#define USING_ZSTD 1 +#define USING_CTX 1 +#endif #endif #ifndef _SECURE_ATL