Feature: update zlib to 1.4 and pick wire compression at runtime
The client selected zlib or zstd for the wire protocol at compile time (USING_ZLIB), so changing the mode required a rebuild, and the vendored zlib was still 1.3.2. Vendored zlib is now 1.4 (headers and both static libs rebuilt from the upstream source root). The client links zlib and zstd together and reads settings\compress from its own registry key when a connection is created: "zlib" selects zlib, anything else keeps the zstd default. Every connection reads it once, so sub-connections follow the main link. The online list context menu gains "Experimental -> Wire compression: ZSTD / ZLIB". It sends COMMAND_SET_COMPRESS (181) only to hosts that declare CLIENT_CAP_COMPRESS_SWITCH; the client persists the choice and restarts its connection thread, so the new mode applies in about two seconds without restarting the process. The online-info tooltip now marks the negotiated algorithm with [ZSTD] / [ZLIB] / [NONE]. Also fixes zlib buffer sizing on both ends: 1.001n+12 is one byte short of compressBound for packets under about 1 KB, which silently dropped small commands. Verified: both modes connect and receive MasterSettings; menu switching to zlib and back reconnects in each mode; Debug/Release x64, Release x86, ServerDll and server builds pass. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
340fb20518
commit
0e20e0bbdb
20 files changed
+303
-84
No files matched your search
+123
-31
@@ -28,30 +28,46 @@ inline int WSAGetLastError()
|
||||
#include <assert.h>
|
||||
#include <string>
|
||||
#include <random>
|
||||
#if USING_ZLIB
|
||||
#include "zlib/zlib.h"
|
||||
#define Z_FAILED(p) (Z_OK != (p))
|
||||
#define Z_SUCCESS(p) (!Z_FAILED(p))
|
||||
// 线缆压缩:Windows 客户端同时携带 zlib 与 zstd,运行期由 settings\compress 选择
|
||||
// (默认 zstd,配置为 zlib 时使用 zlib)。POSIX 客户端只链接 libzstd。
|
||||
#if defined(_WIN32)
|
||||
# include "zlib/zlib.h"
|
||||
# define CLIENT_HAS_ZLIB 1
|
||||
#else
|
||||
# define CLIENT_HAS_ZLIB 0
|
||||
#endif
|
||||
#include "common/zstd_wrapper.h"
|
||||
#define ZSTD_CLEVEL ZSTD_CLEVEL_DEFAULT
|
||||
|
||||
// ZSTD 库在客户端始终要链接:文件管理器的 ZSTA 归档(common/ZstdArchive.h)、
|
||||
// common/zstd_wrapper.c 以及上面的版本日志都无条件引用 ZSTD_*,与线缆压缩
|
||||
// 选择 zlib 还是 zstd 无关。此处在两种模式下都生效。
|
||||
#ifdef _WIN32
|
||||
# ifdef _WIN64
|
||||
# pragma comment(lib, "zstd/zstd_x64.lib")
|
||||
# else
|
||||
# pragma comment(lib, "zstd/zstd.lib")
|
||||
# endif
|
||||
# include "common/iniFile.h" // CLIENT_PATH + settings\compress
|
||||
#endif
|
||||
#define Z_FAILED(p) ZSTD_isError(p)
|
||||
#define Z_SUCCESS(p) (!Z_FAILED(p))
|
||||
#define ZSTD_CLEVEL ZSTD_CLEVEL_DEFAULT
|
||||
#if USING_CTX
|
||||
#define compress(dest, destLen, source, sourceLen) zstd_compress_auto(m_Cctx, dest, *(destLen), source, sourceLen, 1024*1024)
|
||||
#define uncompress(dest, destLen, source, sourceLen) ZSTD_decompressDCtx(m_Dctx, dest, *(destLen), source, sourceLen)
|
||||
|
||||
// 读取 settings\compress:默认 zstd,值为 "zlib"(大小写不敏感)时使用 zlib。
|
||||
// 配置存放在客户端注册表(CLIENT_PATH)。服务端下发 COMMAND_SET_COMPRESS 会写同一个
|
||||
// 键并触发重连;新连接在构造函数里重新读取,从而无需重启进程即可生效。
|
||||
static bool ClientCompressUsesZlib()
|
||||
{
|
||||
#if defined(_WIN32) && CLIENT_HAS_ZLIB
|
||||
iniFile cfg(CLIENT_PATH);
|
||||
std::string mode = cfg.GetStr("settings", "compress", "zstd");
|
||||
for (size_t i = 0; i < mode.size(); ++i) {
|
||||
if (mode[i] >= 'A' && mode[i] <= 'Z')
|
||||
mode[i] = (char)(mode[i] - 'A' + 'a');
|
||||
}
|
||||
return mode == "zlib";
|
||||
#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
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Construction/Destruction
|
||||
@@ -244,6 +260,11 @@ IOCPClient::IOCPClient(const State&bExit, bool exit_while_disconnect, int mask,
|
||||
#endif
|
||||
}
|
||||
|
||||
// 线缆压缩模式:运行期读取配置(默认 zstd)。每个连接实例只读一次;
|
||||
// 服务端下发 COMMAND_SET_COMPRESS 后客户端会重启 StartClient 线程,新实例重新读取。
|
||||
m_useZlib = ClientCompressUsesZlib();
|
||||
Mprintf("[IOCPClient] wire compress: %s\n", m_useZlib ? "zlib" : "zstd");
|
||||
|
||||
m_main = main;
|
||||
m_conn = conn; // 保存 CONNECT_ADDRESS 指针。子连接 auth 在每次连接时通过
|
||||
// m_conn->clientID 现取主连接 ID(同一指针,主连接登录后填好的最新值)。
|
||||
@@ -296,7 +317,7 @@ IOCPClient::IOCPClient(const State&bExit, bool exit_while_disconnect, int mask,
|
||||
m_Cctx = ZSTD_createCCtx();
|
||||
m_Dctx = ZSTD_createDCtx();
|
||||
auto n = ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_nbWorkers, 0);
|
||||
if (Z_FAILED(n)) {
|
||||
if (ZSTD_isError(n)) {
|
||||
ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_nbWorkers, 0);
|
||||
}
|
||||
ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_compressionLevel, ZSTD_CLEVEL);
|
||||
@@ -307,6 +328,74 @@ IOCPClient::IOCPClient(const State&bExit, bool exit_while_disconnect, int mask,
|
||||
#endif
|
||||
}
|
||||
|
||||
// 线缆压缩/解压:按本连接构造时选定的 m_useZlib 走 zlib 或 zstd。
|
||||
// 成功返回 true 并写回实际长度;失败返回 false。
|
||||
bool IOCPClient::CompressData(BYTE* dst, ULONG& dstLen, const void* src, ULONG srcLen)
|
||||
{
|
||||
if (m_useZlib) {
|
||||
#if CLIENT_HAS_ZLIB
|
||||
uLongf n = (uLongf)dstLen;
|
||||
int r = compress(dst, &n, (const Bytef*)src, (uLong)srcLen);
|
||||
if (r != Z_OK)
|
||||
return false;
|
||||
dstLen = (ULONG)n;
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
#if USING_CTX
|
||||
{
|
||||
size_t r = zstd_compress_auto(m_Cctx, dst, (size_t)dstLen, src, (size_t)srcLen, 1024 * 1024);
|
||||
if (ZSTD_isError(r))
|
||||
return false;
|
||||
dstLen = (ULONG)r;
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
{
|
||||
size_t r = ZSTD_compress(dst, (size_t)dstLen, src, (size_t)srcLen, ZSTD_CLEVEL_DEFAULT);
|
||||
if (ZSTD_isError(r))
|
||||
return false;
|
||||
dstLen = (ULONG)r;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool IOCPClient::DecompressData(BYTE* dst, ULONG& dstLen, const void* src, ULONG srcLen)
|
||||
{
|
||||
if (m_useZlib) {
|
||||
#if CLIENT_HAS_ZLIB
|
||||
uLongf n = (uLongf)dstLen;
|
||||
int r = uncompress(dst, &n, (const Bytef*)src, (uLong)srcLen);
|
||||
if (r != Z_OK)
|
||||
return false;
|
||||
dstLen = (ULONG)n;
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
#if USING_CTX
|
||||
{
|
||||
size_t r = ZSTD_decompressDCtx(m_Dctx, dst, (size_t)dstLen, src, (size_t)srcLen);
|
||||
if (ZSTD_isError(r))
|
||||
return false;
|
||||
dstLen = (ULONG)r;
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
{
|
||||
size_t r = ZSTD_decompress(dst, (size_t)dstLen, src, (size_t)srcLen);
|
||||
if (ZSTD_isError(r))
|
||||
return false;
|
||||
dstLen = (ULONG)r;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void IOCPClient::PrepareHeaderEncoderForConnect()
|
||||
{
|
||||
if (!m_EncoderType) {
|
||||
@@ -358,7 +447,7 @@ void IOCPClient::SetMultiThreadCompress(int threadNum)
|
||||
#if USING_CTX
|
||||
BOOL failed = TRUE;
|
||||
if (threadNum > 1) {
|
||||
failed = Z_FAILED(ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_nbWorkers, threadNum));
|
||||
failed = ZSTD_isError(ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_nbWorkers, threadNum));
|
||||
}
|
||||
if (failed) {
|
||||
ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_nbWorkers, 0);
|
||||
@@ -1007,9 +1096,15 @@ VOID IOCPClient::OnServerReceiving(CBuffer* m_CompressedBuffer, char* szBuffer,
|
||||
|
||||
m_CompressedBuffer->ReadBuffer(CompressedBuffer, ulCompressedLength);
|
||||
m_Encoder->Decode(CompressedBuffer, ulCompressedLength, (LPBYTE)szPacketFlag);
|
||||
size_t iRet = uncompress(DeCompressedBuffer, &ulOriginalLength, CompressedBuffer, ulCompressedLength);
|
||||
|
||||
if (Z_SUCCESS(iRet)) { //如果解压成功
|
||||
// 与发送同一模式:服务端按客户端首个压缩包识别算法,回包使用同一算法,
|
||||
// 因此这里固定用本连接的 m_useZlib 解压。
|
||||
ULONG ulDecompressedLength = ulOriginalLength;
|
||||
bool decompressed = DecompressData(DeCompressedBuffer, ulDecompressedLength,
|
||||
CompressedBuffer, ulCompressedLength);
|
||||
ulOriginalLength = ulDecompressedLength;
|
||||
|
||||
if (decompressed) { //如果解压成功
|
||||
// 优先看是不是 TOKEN_CONN_AUTH 响应;只有当 PerformConnAuth 正在等待时才消费。
|
||||
// 不在等待状态时返回 false,包透传给 manager(manager 一般也不识别此 token,
|
||||
// 走 default 路径忽略,无副作用)。
|
||||
@@ -1058,27 +1153,24 @@ BOOL IOCPClient::OnServerSending(const char* szBuffer, ULONG ulOriginalLength, P
|
||||
std::lock_guard<std::mutex> lock(m_Locker);
|
||||
{
|
||||
int cmd = BYTE(szBuffer[0]);
|
||||
//乘以1.001是以最坏的也就是数据压缩后占用的内存空间和原先一样 +12
|
||||
//防止缓冲区溢出// HelloWorld 10 22
|
||||
//数据压缩 压缩算法 微软提供
|
||||
//nSize = 436
|
||||
//destLen = 448
|
||||
#if USING_ZLIB
|
||||
unsigned long ulCompressedLength = (double)ulOriginalLength * 1.001 + 12;
|
||||
// 线缆压缩:运行期选择 zlib / zstd(默认 zstd)。zlib 用 compressBound 而不是
|
||||
// 1.001n+12 —— 后者对小于约 1KB 的包会比 compressBound 少 1 字节,导致
|
||||
// compress() 返回 Z_BUF_ERROR、小包(心跳 ACK、控制命令)被静默丢弃。
|
||||
#if CLIENT_HAS_ZLIB
|
||||
unsigned long ulCompressedLength = m_useZlib
|
||||
? (unsigned long)compressBound(ulOriginalLength)
|
||||
: (unsigned long)ZSTD_compressBound(ulOriginalLength);
|
||||
#else
|
||||
unsigned long ulCompressedLength = ZSTD_compressBound(ulOriginalLength);
|
||||
unsigned long ulCompressedLength = (unsigned long)ZSTD_compressBound(ulOriginalLength);
|
||||
#endif
|
||||
BYTE buf[1024];
|
||||
LPBYTE CompressedBuffer = ulCompressedLength>1024 ? new BYTE[ulCompressedLength] : buf;
|
||||
int iRet = compress(CompressedBuffer, &ulCompressedLength, (PBYTE)szBuffer, ulOriginalLength);
|
||||
if (Z_FAILED(iRet)) {
|
||||
Mprintf("[ERROR] compress failed: srcLen %d, dstLen %d \n", ulOriginalLength, ulCompressedLength);
|
||||
if (!CompressData(CompressedBuffer, ulCompressedLength, szBuffer, ulOriginalLength)) {
|
||||
Mprintf("[ERROR] compress failed: srcLen %d, dstLen %d, mode=%s\n",
|
||||
ulOriginalLength, ulCompressedLength, m_useZlib ? "zlib" : "zstd");
|
||||
if (CompressedBuffer != buf) delete [] CompressedBuffer;
|
||||
return FALSE;
|
||||
}
|
||||
#if !USING_ZLIB
|
||||
ulCompressedLength = iRet;
|
||||
#endif
|
||||
ULONG ulPackTotalLength = ulCompressedLength + m_Encoder->GetHeadLen();
|
||||
CBuffer m_WriteBuffer;
|
||||
HeaderFlag H = m_Encoder->GetHead();
|
||||
|
||||
@@ -437,6 +437,12 @@ protected:
|
||||
// ConnectServer 成功后自动 auth 的 opt-in 标志。子连接构造后调 EnableSubConnAuth() 设为 true。
|
||||
bool m_subConnAuthEnabled = false;
|
||||
uint64_t m_subConnAuthClientID = 0; // 0 表示从 m_conn->clientID 现取
|
||||
|
||||
// 线缆压缩:运行期由 settings\compress 选择(默认 zstd;配置 zlib 时用 zlib)。
|
||||
// 每个连接实例在构造时读取一次;COMMAND_SET_COMPRESS 触发重连后重新读取。
|
||||
bool m_useZlib = false;
|
||||
bool CompressData(BYTE* dst, ULONG& dstLen, const void* src, ULONG srcLen);
|
||||
bool DecompressData(BYTE* dst, ULONG& dstLen, const void* src, ULONG srcLen);
|
||||
#if USING_CTX
|
||||
ZSTD_CCtx* m_Cctx; // 压缩上下文
|
||||
ZSTD_DCtx* m_Dctx; // 解压上下文
|
||||
|
||||
@@ -1126,6 +1126,33 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
|
||||
break;
|
||||
}
|
||||
|
||||
case COMMAND_SET_COMPRESS: {
|
||||
// 线缆压缩切换(zstd / zlib):写本机配置后重启 StartClient 线程。
|
||||
// 注意不能只断开当前连接:IOCPClient 的压缩模式在构造时读取,复用旧实例重连
|
||||
// 不会重新读配置。置 S_CLIENT_UPDATE 让 StartClientApp 重建连接对象。
|
||||
std::string mode;
|
||||
if (ulLength > 1)
|
||||
mode.assign((const char*)szBuffer + 1, ulLength - 1);
|
||||
while (!mode.empty() && (mode.back() == '\0' || mode.back() == ' ' ||
|
||||
mode.back() == '\r' || mode.back() == '\n'))
|
||||
mode.pop_back();
|
||||
for (size_t i = 0; i < mode.size(); ++i) {
|
||||
if (mode[i] >= 'A' && mode[i] <= 'Z')
|
||||
mode[i] = (char)(mode[i] - 'A' + 'a');
|
||||
}
|
||||
if (mode != "zlib" && mode != "zstd") {
|
||||
Mprintf("[compress-switch] unsupported mode '%s', ignored\n", mode.c_str());
|
||||
break;
|
||||
}
|
||||
if (m_cfg)
|
||||
m_cfg->SetStr("settings", "compress", mode);
|
||||
Mprintf("[compress-switch] set to %s, restarting client\n", mode.c_str());
|
||||
g_bExit = S_CLIENT_UPDATE;
|
||||
if (m_ClientObject)
|
||||
m_ClientObject->Disconnect();
|
||||
break;
|
||||
}
|
||||
|
||||
case COMMAND_DOWN_EXEC: {
|
||||
std::thread(DownExecute, std::string((char*)szBuffer + 1), this).detach();
|
||||
break;
|
||||
|
||||
@@ -729,11 +729,7 @@ DWORD WINAPI CScreenManager::WorkThreadProc(LPVOID lParam)
|
||||
This->WaitForDialogOpen();
|
||||
|
||||
clock_t last = clock();
|
||||
#if USING_ZLIB
|
||||
const int fps = 8;// 帧率
|
||||
#else
|
||||
const int fps = 8;// 帧率
|
||||
#endif
|
||||
const int sleep = 1000 / fps;// 间隔时间(ms)
|
||||
int c1 = 0; // 连续耗时长的次数
|
||||
int c2 = 0; // 连续耗时短的次数
|
||||
|
||||
+3
-7
@@ -6,16 +6,12 @@
|
||||
#if !defined(AFX_STDAFX_H__46CA6496_AAD6_4658_B6E9_D7AEB26CDCD5__INCLUDED_)
|
||||
#define AFX_STDAFX_H__46CA6496_AAD6_4658_B6E9_D7AEB26CDCD5__INCLUDED_
|
||||
|
||||
// 是否使用ZLIB
|
||||
#define USING_ZLIB 0
|
||||
|
||||
#if !USING_ZLIB
|
||||
|
||||
// 客户端线缆压缩:Windows 客户端同时编译 zlib 与 zstd,运行期由 settings\compress
|
||||
// 选择(默认 zstd;配置为 zlib 时使用 zlib)。服务端可通过 COMMAND_SET_COMPRESS
|
||||
// 写入该配置并触发重连,见 client/IOCPClient.cpp 与 KernelManager 的处理分支。
|
||||
#define USING_ZSTD 1
|
||||
#define USING_CTX 1
|
||||
|
||||
#endif
|
||||
|
||||
// 是否使用 Opus 音频压缩 (需要 opus 库)
|
||||
// 设为 1 启用 Opus 压缩,设为 0 使用 PCM 无压缩
|
||||
#define USING_OPUS 1
|
||||
|
||||
@@ -55,11 +55,7 @@ DWORD CVideoManager::WorkThread(LPVOID lParam)
|
||||
|
||||
// 等控制端对话框打开
|
||||
This->WaitForDialogOpen();
|
||||
#if USING_ZLIB
|
||||
const int fps = 8;// 帧率
|
||||
#else
|
||||
const int fps = 8;// 帧率
|
||||
#endif
|
||||
const int sleep = 1000 / fps;// 间隔时间(ms)
|
||||
|
||||
timeBeginPeriod(1);
|
||||
|
||||
Reference in new issue
Block a user