The switch only offered direct TCP, WS and WSS; UDP and KCP are now experimental menu entries, and the radio item follows the real transport. The server maps ghost/UDPOption to real listeners (a "UDP;KCP" setup used to collapse to one UDP port). A switch sends the master address and that transport's port, refused behind an upper-level FRP. The client persists the override, recreates its connection object when the transport changes, and disconnects an unacknowledged switch after 60 s so the 300 s revert can run. TCP/WS/WSS behaviour is unchanged. Reconnecting a KCP client reused the same object: a fresh std::thread over the old one aborted the process and leaked the ikcp session; ConnectServer now joins the update thread and releases the session first, and the UDP one also clears the stale login-ack flag. KCP contexts no longer touch the shared listener socket: CancelIO is a no-op, Disconnect looks up by IP:port and defers deletion behind the receive thread's reference, COMMAND_BYE leaves the socket alone, and Disconnect stays silent like UDP's. Oversized messages are delivered in chunks. A datagram host has no disconnect event, so a new login for the same id was parked as standby for up to 120 s while the client gave up after 60 s; a UDP/KCP or different-transport login now replaces it immediately. The new labels, prompts and settings hint are translated in the en_US/de_DE/zh_TW packs; UDPOption also accepts 0/1 in port order. Verified: Debug build; switch directions plus KCP delete/reconnect/file-transfer tested. Co-Authored-By: deepseek-flash
114 lines
3.6 KiB
C++
114 lines
3.6 KiB
C++
#pragma once
|
||
|
||
#include "Server.h"
|
||
|
||
class CONTEXT_KCP : public CONTEXT_OBJECT
|
||
{
|
||
public:
|
||
int addrLen = 0;
|
||
sockaddr_in clientAddr = {};
|
||
CONTEXT_KCP()
|
||
{
|
||
}
|
||
virtual ~CONTEXT_KCP()
|
||
{
|
||
}
|
||
std::string GetProtocol() const override
|
||
{
|
||
return "KCP";
|
||
}
|
||
VOID InitMember(SOCKET s, VOID* svr) override
|
||
{
|
||
CONTEXT_OBJECT::InitMember(s, svr);
|
||
clientAddr = {};
|
||
addrLen = sizeof(sockaddr_in);
|
||
}
|
||
void CancelIO() override
|
||
{
|
||
// 与 UDP 相同:sClientSocket 是服务端共享的 KCP 接收 socket,基类
|
||
// SAFE_CANCELIO 会 CancelIo + closesocket,关掉它等于打死同端口所有
|
||
// KCP 客户端。KCP 上下文没有独占连接,这里什么都不做。
|
||
}
|
||
void Destroy() override
|
||
{
|
||
// 与 UDP 相同:上下文归服务端 m_clients 所有,必须经 Disconnect 先摘除
|
||
// 映射再延迟释放,否则映射里会留下悬空指针。
|
||
if (server != nullptr) {
|
||
server->Disconnect(this);
|
||
return;
|
||
}
|
||
delete this;
|
||
}
|
||
virtual std::string GetPeerName() const override
|
||
{
|
||
char client_ip[INET_ADDRSTRLEN];
|
||
#if (defined(_WIN32_WINNT) && _WIN32_WINNT <= 0x0501)
|
||
strncpy(client_ip, inet_ntoa(clientAddr.sin_addr), INET_ADDRSTRLEN - 1);
|
||
client_ip[INET_ADDRSTRLEN - 1] = '\0';
|
||
#else
|
||
inet_ntop(AF_INET, &clientAddr.sin_addr, client_ip, INET_ADDRSTRLEN);
|
||
#endif
|
||
return client_ip;
|
||
}
|
||
virtual int GetPort() const override
|
||
{
|
||
int client_port = ntohs(clientAddr.sin_port);
|
||
return client_port;
|
||
}
|
||
// m_clients 的键("IP:port")。Disconnect 必须用它查表:GetPeerName() 只有 IP,
|
||
// 原来拿它去查 "IP:port" 的映射永远查不到,上下文既不摘除也不释放。
|
||
std::string MakeKey() const
|
||
{
|
||
// inet_ntop 写入调用方缓冲,不在多线程间共用 inet_ntoa 的静态缓冲。
|
||
char ip[INET_ADDRSTRLEN] = { 0 };
|
||
inet_ntop(AF_INET, &clientAddr.sin_addr, ip, sizeof(ip));
|
||
char buf[64];
|
||
sprintf_s(buf, "%s:%d", ip, ntohs(clientAddr.sin_port));
|
||
return buf;
|
||
}
|
||
};
|
||
|
||
class IOCPKCPServer : public Server
|
||
{
|
||
public:
|
||
IOCPKCPServer() {}
|
||
virtual ~IOCPKCPServer() {}
|
||
|
||
virtual int GetPort() const override
|
||
{
|
||
return m_port;
|
||
}
|
||
virtual UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) override;
|
||
virtual BOOL Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) override;
|
||
virtual void Destroy() override;
|
||
virtual void Disconnect(CONTEXT_OBJECT* ctx) override;
|
||
virtual void UpdateMaxConnection(int maxConn) override { m_maxConn = maxConn; }
|
||
|
||
private:
|
||
SOCKET m_socket = INVALID_SOCKET;
|
||
HANDLE m_hIOCP = NULL;
|
||
HANDLE m_hThread = NULL;
|
||
bool m_running = false;
|
||
USHORT m_port = 0;
|
||
int m_maxConn = 10000; // 由 ServerPair::UpdateMaxConnection 下发
|
||
|
||
pfnNotifyProc m_notify = nullptr;
|
||
pfnOfflineProc m_offline = nullptr;
|
||
|
||
std::mutex m_contextsMutex;
|
||
std::unordered_map<std::string, CONTEXT_KCP*> m_clients; // key: "IP:port"
|
||
// WorkerThread 的整包接收缓冲。不能再用函数内 static:多个 KCP 端口实例
|
||
// 各自有 WorkerThread,会同时 resize/读取同一个对象导致串包或堆损坏。
|
||
std::vector<char> m_recvBuf;
|
||
|
||
std::thread m_kcpUpdateThread;
|
||
|
||
CONTEXT_KCP* FindOrCreateClient(const sockaddr_in& addr, SOCKET sClientSocket);
|
||
|
||
void WorkerThread();
|
||
|
||
void KCPUpdateLoop();
|
||
|
||
static IUINT32 iclock();
|
||
};
|