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
51 lines
1.9 KiB
C++
51 lines
1.9 KiB
C++
#pragma once
|
||
#include "IOCPUDPClient.h"
|
||
#include "ikcp.h"
|
||
#include <thread>
|
||
#include <atomic>
|
||
#include <vector>
|
||
|
||
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;
|
||
|
||
bool IsKcpTransport() const override { return true; }
|
||
|
||
// 重写发送函数:将应用数据通过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();
|
||
|
||
// 以下三个都在持有 kcpMutex_ 时调用/检查。
|
||
int DrainPendingLocked(char* buffer, int bufSize);
|
||
int RecvOneLocked(char* buffer, int bufSize);
|
||
bool HasPendingAppData();
|
||
|
||
private:
|
||
ikcpcb* kcp_;
|
||
std::thread updateThread_;
|
||
std::atomic<bool> running_;
|
||
// KCP 的多线程保护:网络线程(send/flush、input/recv)和更新线程(update)
|
||
// 共用同一份 ikcp 状态,不加锁会在大消息(预览等几十个分片)时把内部队列搞乱。
|
||
std::mutex kcpMutex_;
|
||
// 大消息分片交付:ikcp_recv 一次返回整条消息,而调用方缓冲只有
|
||
// MAX_RECV_BUFFER-1;大于缓冲的消息若直接 ikcp_recv 会返回 -3 且不消费,
|
||
// 队头永久卡住。这里整条取出后分几次交给上层(与 TCP 的分片读取语义一致)。
|
||
std::vector<char> m_pendingRecv;
|
||
size_t m_pendingPos = 0;
|
||
};
|