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
@@ -73,6 +73,13 @@ UINT IOCPKCPServer::StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc
|
||||
m_socket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, WSA_FLAG_OVERLAPPED);
|
||||
if (m_socket == INVALID_SOCKET) return 2;
|
||||
|
||||
// 大消息(预览响应)一次 flush 会连发几十个分片;默认接收缓冲偏小,
|
||||
// 用户态来不及读就会丢包,KCP 只能重传,表现为“特别慢”。加大到 1MB。
|
||||
{
|
||||
int rcvbuf = 1024 * 1024;
|
||||
setsockopt(m_socket, SOL_SOCKET, SO_RCVBUF, (const char*)&rcvbuf, sizeof(rcvbuf));
|
||||
}
|
||||
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(uPort);
|
||||
@@ -115,18 +122,27 @@ void IOCPKCPServer::WorkerThread()
|
||||
ikcp_input(ctx->kcp, buf, ret);
|
||||
}
|
||||
|
||||
char recvbuf[4096];
|
||||
int n = 0;
|
||||
do {
|
||||
// 一条 KCP 消息可能远大于 4KB:客户端按 128KB 分块发送,预览响应
|
||||
// 可达数十 KB。ikcp_recv 缓冲不足时返回 -2 且不消费消息,队头卡住
|
||||
// 后整个连接再也收不到包。这里按 peeksize 动态扩容,并直接把缓冲
|
||||
// 交给解析器(ctx->szBuffer 只有 8KB,不能拿来接大包)。
|
||||
static std::vector<char> recvbuf;
|
||||
for (;;) {
|
||||
int peek = 0, n = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_contextsMutex);
|
||||
n = ikcp_recv(ctx->kcp, recvbuf, sizeof(recvbuf));
|
||||
peek = ikcp_peeksize(ctx->kcp);
|
||||
if (peek <= 0)
|
||||
break;
|
||||
if ((int)recvbuf.size() < peek)
|
||||
recvbuf.resize(peek);
|
||||
n = ikcp_recv(ctx->kcp, recvbuf.data(), (int)recvbuf.size());
|
||||
}
|
||||
if (n > 0&& m_notify) {
|
||||
memcpy(ctx->szBuffer, recvbuf, n);
|
||||
BOOL ret = ParseReceivedData(ctx, (PBYTE)ctx->szBuffer, n, m_notify);
|
||||
}
|
||||
} while (n>0);
|
||||
if (n <= 0)
|
||||
break;
|
||||
if (m_notify)
|
||||
ParseReceivedData(ctx, (PBYTE)recvbuf.data(), n, m_notify);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
DWORD err = WSAGetLastError();
|
||||
|
||||
Reference in new issue
Block a user