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
129 lines
4.0 KiB
C++
129 lines
4.0 KiB
C++
#include "IOCPKCPClient.h"
|
|
#include <windows.h>
|
|
#include <chrono>
|
|
#include <iostream>
|
|
|
|
IOCPKCPClient::IOCPKCPClient(State& bExit, bool exit_while_disconnect)
|
|
: IOCPUDPClient(bExit, exit_while_disconnect), kcp_(nullptr), running_(false)
|
|
{
|
|
}
|
|
|
|
IOCPKCPClient::~IOCPKCPClient()
|
|
{
|
|
running_ = false;
|
|
if (updateThread_.joinable())
|
|
updateThread_.join();
|
|
|
|
if (kcp_)
|
|
ikcp_release(kcp_);
|
|
}
|
|
|
|
BOOL IOCPKCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
|
{
|
|
BOOL ret = IOCPUDPClient::ConnectServer(szServerIP, uPort);
|
|
if (!ret)
|
|
return FALSE;
|
|
|
|
// 初始化KCP
|
|
uint32_t conv = KCP_SESSION_ID; // conv 要与服务端匹配
|
|
kcp_ = ikcp_create(conv, this);
|
|
if (!kcp_)
|
|
return FALSE;
|
|
|
|
// 设置KCP参数
|
|
// 与服务端一致的“快模式”:10ms 一刷、关闭拥塞控制。原来 40ms + 开拥塞控制时,
|
|
// 预览响应这种几十个分片的大消息要好几秒才发完,服务端 4 秒的预览等待经常超时。
|
|
ikcp_nodelay(kcp_, 1, 10, 2, 1);
|
|
kcp_->rx_minrto = 30;
|
|
kcp_->snd_wnd = 128;
|
|
kcp_->rcv_wnd = 128;
|
|
|
|
// 设置发送回调函数(KCP发送数据时调用)
|
|
kcp_->output = IOCPKCPClient::kcpOutput;
|
|
|
|
running_ = true;
|
|
updateThread_ = std::thread(&IOCPKCPClient::KCPUpdateLoop, this);
|
|
m_bConnected = TRUE;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// UDP收包线程调用,将收到的UDP包送入KCP处理,再尝试读取完整应用包
|
|
int IOCPKCPClient::ReceiveData(char* buffer, int bufSize, int flags)
|
|
{
|
|
// 先调用基类接收UDP原始数据
|
|
char udpBuffer[1500] = { 0 };
|
|
int recvLen = IOCPUDPClient::ReceiveData(udpBuffer, sizeof(udpBuffer), flags);
|
|
if (recvLen <= 0)
|
|
return recvLen;
|
|
|
|
std::lock_guard<std::mutex> lock(kcpMutex_);
|
|
// 输入KCP协议栈
|
|
int inputRet = ikcp_input(kcp_, udpBuffer, recvLen);
|
|
if (inputRet < 0)
|
|
return -1;
|
|
|
|
// 从KCP中读取应用层数据,写入buffer
|
|
int kcpRecvLen = ikcp_recv(kcp_, buffer, bufSize);
|
|
return kcpRecvLen; // >0表示收到完整应用数据,0表示无完整包
|
|
}
|
|
|
|
bool IOCPKCPClient::ProcessRecvData(CBuffer* m_CompressedBuffer, char* szBuffer, int len, int flag)
|
|
{
|
|
int iReceivedLength = ReceiveData(szBuffer, len, flag);
|
|
if (iReceivedLength <= 0)
|
|
{}
|
|
else {
|
|
szBuffer[iReceivedLength] = 0;
|
|
//正确接收就调用OnRead处理,转到OnRead
|
|
OnServerReceiving(m_CompressedBuffer, szBuffer, iReceivedLength);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 发送应用层数据时调用,转发给KCP协议栈
|
|
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_);
|
|
// ikcp_send 成功时返回 0,而调用方(SendWithSplit / SendLoginInfo)按
|
|
// “返回值 = 已发送字节数,> 0 才算成功”判断;返回 0 会被当成失败重试 15 次,
|
|
// 导致登录包每 1~3 秒无限重发、心跳循环永远进不去。这里返回 len。
|
|
return len;
|
|
}
|
|
|
|
// KCP发送数据回调,将KCP生成的UDP包发送出去
|
|
int IOCPKCPClient::kcpOutput(const char* buf, int len, struct IKCPCB* kcp, void* user)
|
|
{
|
|
IOCPKCPClient* client = reinterpret_cast<IOCPKCPClient*>(user);
|
|
if (client->m_sClientSocket == INVALID_SOCKET)
|
|
return -1;
|
|
|
|
int sentLen = sendto(client->m_sClientSocket, buf, len, 0, (sockaddr*)&client->m_ServerAddr, sizeof(client->m_ServerAddr));
|
|
if (sentLen == len)
|
|
return 0;
|
|
else
|
|
return -1;
|
|
}
|
|
|
|
// 独立线程定时调用ikcp_update,保持KCP协议正常工作
|
|
void IOCPKCPClient::KCPUpdateLoop()
|
|
{
|
|
while (running_ && !g_bExit) {
|
|
{
|
|
std::lock_guard<std::mutex> lock(kcpMutex_);
|
|
IUINT32 current = GetTickCount64();
|
|
ikcp_update(kcp_, current);
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(20)); // 20ms周期,视需求调整
|
|
}
|
|
}
|