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:
yuanyuanxianganddeepseek-flash committed 2026-11-23 17:46:36 +01:00
1 parent 340fb20518
commit 0e20e0bbdb
20 files changed
+303 -84

No files matched your search

+123 -31
View File
@@ -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();