Fix: make UDP and KCP clients stay online
Plain UDP gave every datagram a fresh context and destroyed it after parsing, while the heartbeat handler posted that pointer to the UI thread, so UpdateActiveWindow read freed memory. Login also stored that short-lived context, so later heartbeats carried a zero client id and every UDP host timed out. The UDP server now owns one persistent context per client address and dispatches every datagram through it: the receive buffer is cleared per packet, the map is capped and idle addresses evicted, and Disconnect only erases and marks the context so the last reference deletes it. COMMAND_BYE no longer closes the shared receive socket, a failed receive re-posts itself, and UDP previews are capped to thumbnail size. The KCP client returned ikcp_send's 0 for a successful send, so the caller resent login forever and never started heartbeats; it now reports the accepted length, and a duplicate login re-sends MasterSettings. Two follow-up fixes in the same area: ReleaseIoRef keeps fetch_sub paired with exchange so a concurrent Disconnect cannot strand a marked context at zero refs; UpdateUserEvent drops posted heartbeats while closing instead of touching a context the UDP server may have destroyed. TCP behavior is otherwise unchanged. Verified: Debug build, UDP previews and heartbeats work, KCP logs in once and reports its active window. Server link hits the known LNK1104 from the running exe. Co-Authored-By: Tencent Hunyuan Hy
This commit is contained in:
1 parent
d48f236385
commit
914553ab4e
8 files changed
+280
-30
No files matched your search
@@ -412,6 +412,23 @@ public:
|
||||
// 引用计数:跟踪正在处理此对象的工作线程数量,防止竞态条件 (#215)
|
||||
std::atomic<int> IoRefCount{0}; // I/O 处理引用计数
|
||||
std::atomic<bool> IsRemoved{false}; // 标记是否已被标记为移除
|
||||
// 仅 UDP 上下文使用:对象已从服务器映射移除,等最后一次引用释放时由
|
||||
// 释放者删除(不能在 Disconnect 里等待,调用方可能正持有引用)。
|
||||
std::atomic<bool> DeleteWhenIdle{false};
|
||||
|
||||
// 释放一枚 IoRefCount;若这是最后一枚且容器已要求删除,则在此处销毁。
|
||||
void ReleaseIoRef()
|
||||
{
|
||||
// 必须先 fetch_sub 再用 exchange 读标记:两者作为一个整体才不会漏删。
|
||||
// 曾经用 `if (!DeleteWhenIdle.load()) { fetch_sub; return; }` 做 TCP 短路,
|
||||
// 但 load 与 fetch_sub 之间存在窗口:worker 读到 false 后,Disconnect
|
||||
// 可以完成 store(true) 并把计数降到 1,worker 恢复后走短路把计数减到 0
|
||||
// 直接返回,此时既没人 delete、标记也永远是 true —— 对象泄漏。
|
||||
// TCP 上下文 DeleteWhenIdle 恒为 false,exchange(false) 只是把 false 写回
|
||||
// false,对可能被连接池复用的对象没有副作用,与原来的裸 fetch_sub 等价。
|
||||
if (IoRefCount.fetch_sub(1) == 1 && DeleteWhenIdle.exchange(false))
|
||||
delete this;
|
||||
}
|
||||
|
||||
// 内核测得的纯网络 RTT(μs),由 IOCPServer 的 RTT 轮询线程通过
|
||||
// WSAIoctl(SIO_TCP_INFO) 周期性写入;任何线程可通过 GetRttUs() 安全读取。
|
||||
@@ -880,8 +897,20 @@ public:
|
||||
}
|
||||
void Destroy() override
|
||||
{
|
||||
// 上下文归 UDP 服务端的地址映射所有:必须经 Disconnect 先摘除映射,
|
||||
// 否则映射里留下悬空指针(后续数据报或服务端退出时会 UAF/重复释放)。
|
||||
if (server != nullptr) {
|
||||
server->Disconnect(this);
|
||||
return;
|
||||
}
|
||||
delete this;
|
||||
}
|
||||
void CancelIO() override
|
||||
{
|
||||
// UDP 的 sClientSocket 是服务端共享的接收 socket,取消 IO 会打断
|
||||
// 所有在途的 WSARecvFrom(且工作线程不会补投)。UDP 上下文没有
|
||||
// 独占连接,这里什么都不做。
|
||||
}
|
||||
virtual std::string GetPeerName() const override
|
||||
{
|
||||
char client_ip[INET_ADDRSTRLEN];
|
||||
|
||||
Reference in new issue
Block a user