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
38 lines
1.3 KiB
C++
38 lines
1.3 KiB
C++
#pragma once
|
||
#include "IOCPUDPClient.h"
|
||
#include "ikcp.h"
|
||
#include <thread>
|
||
#include <atomic>
|
||
|
||
class IOCPKCPClient : public IOCPUDPClient
|
||
{
|
||
public:
|
||
IOCPKCPClient(State& bExit, bool exit_while_disconnect = false);
|
||
virtual ~IOCPKCPClient();
|
||
|
||
virtual BOOL ConnectServer(const char* szServerIP, unsigned short uPort) override;
|
||
|
||
// 重写接收函数:输入UDP数据给KCP,输出KCP层解包后的数据
|
||
virtual int ReceiveData(char* buffer, int bufSize, int flags) override;
|
||
|
||
virtual bool ProcessRecvData(CBuffer* m_CompressedBuffer, char* szBuffer, int len, int flag) override;
|
||
|
||
// 重写发送函数:将应用数据通过KCP发送
|
||
virtual int SendTo(const char* buf, int len, int flags) override;
|
||
|
||
private:
|
||
// KCP发送数据的回调函数,负责调用UDP的sendto
|
||
static int kcpOutput(const char* buf, int len, struct IKCPCB* kcp, void* user);
|
||
|
||
// 定时调用ikcp_update的线程函数
|
||
void KCPUpdateLoop();
|
||
|
||
private:
|
||
ikcpcb* kcp_;
|
||
std::thread updateThread_;
|
||
std::atomic<bool> running_;
|
||
// KCP 的多线程保护:网络线程(send/flush、input/recv)和更新线程(update)
|
||
// 共用同一份 ikcp 状态,不加锁会在大消息(预览等几十个分片)时把内部队列搞乱。
|
||
std::mutex kcpMutex_;
|
||
};
|