Fix: keep zlib compression and host sharing mutually exclusive

The wire compression mode is a machine-wide setting (settings\compress),
so a zlib switch is inherited by share instances and by the
sub-connections they open later. A share peer that only speaks zstd
(the Go server has no zlib path) then loses the link on the next
connect. A shared instance could also rewrite the host's setting on
behalf of the sharing master.

The client now refuses COMMAND_SET_COMPRESS on a non-main instance,
refuses a switch to zlib while share_list is non-empty (zstd stays
allowed), and refuses a new share while the configured mode is zlib.
The share check reads settings\compress rather than the current
connection's m_useZlib: parallel-mode links can diverge from the
machine-wide value the new share instance will read. Refusals answer
with a ClientMsg so the operator sees why.

Verified: Debug x64 ghost and Release x64 ServerDll build with only the
pre-existing warnings; review flagged the live-flag divergence, fixed.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-27 12:23:22 +01:00
1 parent c9a18856e5
commit 1bd7fa5b2d
3 files changed
+40

No files matched your search

+7
View File
@@ -69,6 +69,13 @@ static bool ClientCompressUsesZlib()
#endif
}
// 对外暴露同一判定:分享前要确认新建的分享实例会用什么算法(它构造时读的就是
// 这个配置),不能用某条现有连接的 m_useZlib 代替。
bool IOCPClient::ConfiguredWireZlib()
{
return ClientCompressUsesZlib();
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
+5
View File
@@ -214,6 +214,11 @@ public:
// COMMAND_SET_COMPRESS 用它原地切换,避免 S_CLIENT_UPDATE 把进程内测试客户端停死。
void SetWireCompression(bool useZlib) { m_useZlib = useZlib; }
// 整机配置里选定的线缆压缩模式(新连接构造时读的同一个键)。分享实例会继承它,
// 所以分享前按它判断,而不是按当前连接的 m_useZlib:并行上线时不同连接的模式
// 可以和配置不一致。
static bool ConfiguredWireZlib();
void SetServerAddress(const char* szServerIP, unsigned short uPort)
{
m_Domain = szServerIP ? szServerIP : "127.0.0.1";
+28
View File
@@ -1154,6 +1154,23 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
Mprintf("[compress-switch] unsupported mode '%s', ignored\n", mode.c_str());
break;
}
// 分享实例不能改写整机的线缆压缩配置:它跑的是同一个 OnReceive,放行的话
// 分享目标主控就能改掉主机连归属方的模式,主机下次重启会按别家的选择连回来。
if (!m_ClientApp || !m_ClientApp->IsMainInstance()) {
Mprintf("[compress-switch] refused: not the main instance\n");
ClientMsg msg("线缆压缩", "Compression switch refused: only the owning master may switch");
SendData((LPBYTE)&msg, sizeof(msg));
break;
}
// 存在分享链路时只允许切回 zstd。压缩模式是整机配置(settings\compress),
// 分享实例及后续子连接都会继承;对端若只支持 zstd(如 Go 服务端),切到
// zlib 会让分享链路在下次建连后失联。zstd 对所有对端都兼容。
if (mode == "zlib" && m_cfg && !m_cfg->GetStr("settings", "share_list").empty()) {
Mprintf("[compress-switch] refused zlib: share links present\n");
ClientMsg msg("线缆压缩", "Compression switch refused: this host has share links; cancel sharing before switching to zlib");
SendData((LPBYTE)&msg, sizeof(msg));
break;
}
if (m_cfg)
m_cfg->SetStr("settings", "compress", mode);
Mprintf("[compress-switch] set to %s, reconnecting\n", mode.c_str());
@@ -1470,6 +1487,17 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
SendData((LPBYTE)&msg, sizeof(msg));
break;
}
// 主机当前走 zlib 时不接受分享:分享实例构造时会读整机 compress
// 配置,带着 zlib 去连分享目标;对端若只支持 zstd(如 Go 服务端),
// 这条分享链路一建就连不上。先切回 zstd 再分享。
// 按配置判断而非当前连接:并行上线时当前连接的模式可能已不是新实例
// 将要使用的模式(配置由收到切换命令的那条连接写入,是整机共享的)。
if (IOCPClient::ConfiguredWireZlib()) {
Mprintf("Share ignored: client uses zlib wire compression\n");
ClientMsg msg("分享主机", "Client uses zlib wire compression, switch it back to zstd before sharing");
SendData((LPBYTE)&msg, sizeof(msg));
break;
}
if (targetKey.empty())
break;
auto share = targetKey;