Feature: switch hosts to the experimental UDP and KCP transports

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
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-29 20:34:08 +01:00
1 parent 914553ab4e
commit dfac2b5179
21 files changed
+534 -91

No files matched your search

+29
View File
@@ -23,8 +23,21 @@ public:
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
{
@@ -42,6 +55,17 @@ public:
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
@@ -58,6 +82,7 @@ public:
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;
@@ -65,12 +90,16 @@ private:
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;