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
@@ -31,7 +31,9 @@ BOOL IOCPKCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
return FALSE;
|
||||
|
||||
// 设置KCP参数
|
||||
ikcp_nodelay(kcp_, 1, 40, 2, 0);
|
||||
// 与服务端一致的“快模式”:10ms 一刷、关闭拥塞控制。原来 40ms + 开拥塞控制时,
|
||||
// 预览响应这种几十个分片的大消息要好几秒才发完,服务端 4 秒的预览等待经常超时。
|
||||
ikcp_nodelay(kcp_, 1, 10, 2, 1);
|
||||
kcp_->rx_minrto = 30;
|
||||
kcp_->snd_wnd = 128;
|
||||
kcp_->rcv_wnd = 128;
|
||||
@@ -55,6 +57,7 @@ int IOCPKCPClient::ReceiveData(char* buffer, int bufSize, int flags)
|
||||
if (recvLen <= 0)
|
||||
return recvLen;
|
||||
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
// 输入KCP协议栈
|
||||
int inputRet = ikcp_input(kcp_, udpBuffer, recvLen);
|
||||
if (inputRet < 0)
|
||||
@@ -84,13 +87,17 @@ int IOCPKCPClient::SendTo(const char* buf, int len, int flags)
|
||||
if (!kcp_)
|
||||
return -1;
|
||||
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
int ret = ikcp_send(kcp_, buf, len);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
// 主动调用flush,加快发送
|
||||
ikcp_flush(kcp_);
|
||||
return ret;
|
||||
// ikcp_send 成功时返回 0,而调用方(SendWithSplit / SendLoginInfo)按
|
||||
// “返回值 = 已发送字节数,> 0 才算成功”判断;返回 0 会被当成失败重试 15 次,
|
||||
// 导致登录包每 1~3 秒无限重发、心跳循环永远进不去。这里返回 len。
|
||||
return len;
|
||||
}
|
||||
|
||||
// KCP发送数据回调,将KCP生成的UDP包发送出去
|
||||
@@ -111,8 +118,11 @@ int IOCPKCPClient::kcpOutput(const char* buf, int len, struct IKCPCB* kcp, void*
|
||||
void IOCPKCPClient::KCPUpdateLoop()
|
||||
{
|
||||
while (running_ && !g_bExit) {
|
||||
IUINT32 current = GetTickCount64();
|
||||
ikcp_update(kcp_, current);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
IUINT32 current = GetTickCount64();
|
||||
ikcp_update(kcp_, current);
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(20)); // 20ms周期,视需求调整
|
||||
}
|
||||
}
|
||||
@@ -31,4 +31,7 @@ private:
|
||||
ikcpcb* kcp_;
|
||||
std::thread updateThread_;
|
||||
std::atomic<bool> running_;
|
||||
// KCP 的多线程保护:网络线程(send/flush、input/recv)和更新线程(update)
|
||||
// 共用同一份 ikcp 状态,不加锁会在大消息(预览等几十个分片)时把内部队列搞乱。
|
||||
std::mutex kcpMutex_;
|
||||
};
|
||||
Reference in new issue
Block a user