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
92 lines
2.3 KiB
C++
92 lines
2.3 KiB
C++
#pragma once
|
|
#include <winsock2.h>
|
|
#include <windows.h>
|
|
#include <mswsock.h>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include "Server.h"
|
|
|
|
|
|
class IOCPUDPServer : public Server
|
|
{
|
|
struct IO_CONTEXT {
|
|
OVERLAPPED ol = {};
|
|
CONTEXT_UDP* pContext = nullptr;
|
|
IO_CONTEXT() : ol({}), pContext(new CONTEXT_UDP)
|
|
{
|
|
}
|
|
~IO_CONTEXT()
|
|
{
|
|
SAFE_DELETE(pContext);
|
|
}
|
|
};
|
|
public:
|
|
IOCPUDPServer();
|
|
~IOCPUDPServer();
|
|
|
|
int GetPort() const override
|
|
{
|
|
return m_port;
|
|
}
|
|
UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) override;
|
|
BOOL Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) override;
|
|
VOID Destroy() override;
|
|
void Disconnect(CONTEXT_OBJECT* ctx) override;
|
|
virtual void UpdateMaxConnection(int maxConn) override
|
|
{
|
|
m_locker.lock();
|
|
m_maxConn = maxConn;
|
|
m_locker.unlock();
|
|
}
|
|
|
|
private:
|
|
void WorkerThread();
|
|
void PostRecv();
|
|
// 按来源地址复用同一个上下文:登录时存进宿主列表的就是它,后续心跳等
|
|
// 数据报必须命中同一对象,否则 clientID / 客户端数据都是空的。
|
|
CONTEXT_UDP* FindOrCreateClient(const sockaddr_in& addr);
|
|
static std::string MakeKey(const sockaddr_in& addr);
|
|
IO_CONTEXT* AddCount()
|
|
{
|
|
m_locker.lock();
|
|
if (m_count > m_maxConn) {
|
|
m_locker.unlock();
|
|
return nullptr;
|
|
}
|
|
IO_CONTEXT* ioCtx = new IO_CONTEXT();
|
|
ioCtx->pContext->InitMember(m_socket, this);
|
|
m_count++;
|
|
m_locker.unlock();
|
|
return ioCtx;
|
|
}
|
|
void DelCount()
|
|
{
|
|
m_locker.lock();
|
|
m_count--;
|
|
m_locker.unlock();
|
|
}
|
|
int GetCount()
|
|
{
|
|
m_locker.lock();
|
|
int n = m_count;
|
|
m_locker.unlock();
|
|
return n;
|
|
}
|
|
private:
|
|
int m_maxConn = 10000;
|
|
int m_port = 6543;
|
|
int m_count = 0;
|
|
CLocker m_locker;
|
|
SOCKET m_socket = INVALID_SOCKET;
|
|
HANDLE m_hIOCP = NULL;
|
|
HANDLE m_hThread = NULL;
|
|
bool m_running = false;
|
|
|
|
pfnNotifyProc m_notify = nullptr;
|
|
pfnOfflineProc m_offline = nullptr;
|
|
|
|
std::map<std::string, CONTEXT_UDP*> m_clients; // 地址 -> 持久上下文
|
|
std::mutex m_clientsMutex;
|
|
};
|