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:
1 parent
914553ab4e
commit
dfac2b5179
21 files changed
+534
-91
No files matched your search
+121
-31
@@ -14,32 +14,61 @@ IOCPKCPClient::~IOCPKCPClient()
|
||||
if (updateThread_.joinable())
|
||||
updateThread_.join();
|
||||
|
||||
if (kcp_)
|
||||
// 基类析构才会停 UDP 收包线程,本析构先于它执行:收包线程可能正卡在
|
||||
// ReceiveData 里等着拿 kcpMutex_。加锁后再释放,并让 ReceiveData 在锁内
|
||||
// 检查空指针,避免在已释放的 ikcp 上调用 ikcp_input。
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
m_pendingRecv.clear();
|
||||
m_pendingPos = 0;
|
||||
if (kcp_) {
|
||||
ikcp_release(kcp_);
|
||||
kcp_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL IOCPKCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
{
|
||||
BOOL ret = IOCPUDPClient::ConnectServer(szServerIP, uPort);
|
||||
if (!ret)
|
||||
// 同一传输内重连会复用这个对象(ClientDll 只在传输类变化时重建):必须先停掉
|
||||
// 旧的更新线程并释放旧 KCP 会话。直接给仍 joinable 的 std::thread 赋值会
|
||||
// std::terminate;旧 kcp_ 不释放则泄漏。
|
||||
running_ = false;
|
||||
if (updateThread_.joinable())
|
||||
updateThread_.join();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
if (kcp_) {
|
||||
ikcp_release(kcp_);
|
||||
kcp_ = nullptr;
|
||||
}
|
||||
m_pendingRecv.clear(); // 上一条连接遗留的未交付分片不再有效
|
||||
m_pendingPos = 0;
|
||||
// 先建好 KCP 会话,再让基类 ConnectServer 启动 UDP 收包线程:否则收包线程
|
||||
// 可能在 kcp_ 还是空指针时调用 ikcp_input。
|
||||
kcp_ = ikcp_create(KCP_SESSION_ID, this); // conv 要与服务端匹配
|
||||
if (!kcp_)
|
||||
return FALSE;
|
||||
|
||||
// 设置KCP参数
|
||||
// 与服务端一致的“快模式”:10ms 一刷、关闭拥塞控制。原来 40ms + 开拥塞控制时,
|
||||
// 预览响应这种几十个分片的大消息要好几秒才发完,服务端 4 秒的预览等待经常超时。
|
||||
ikcp_nodelay(kcp_, 1, 10, 2, 1);
|
||||
kcp_->rx_minrto = 30;
|
||||
kcp_->snd_wnd = 128;
|
||||
kcp_->rcv_wnd = 128;
|
||||
|
||||
// 设置发送回调函数(KCP发送数据时调用)
|
||||
kcp_->output = IOCPKCPClient::kcpOutput;
|
||||
}
|
||||
|
||||
if (!IOCPUDPClient::ConnectServer(szServerIP, uPort)) {
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
if (kcp_) {
|
||||
ikcp_release(kcp_);
|
||||
kcp_ = nullptr;
|
||||
}
|
||||
return FALSE;
|
||||
|
||||
// 初始化KCP
|
||||
uint32_t conv = KCP_SESSION_ID; // conv 要与服务端匹配
|
||||
kcp_ = ikcp_create(conv, this);
|
||||
if (!kcp_)
|
||||
return FALSE;
|
||||
|
||||
// 设置KCP参数
|
||||
// 与服务端一致的“快模式”:10ms 一刷、关闭拥塞控制。原来 40ms + 开拥塞控制时,
|
||||
// 预览响应这种几十个分片的大消息要好几秒才发完,服务端 4 秒的预览等待经常超时。
|
||||
ikcp_nodelay(kcp_, 1, 10, 2, 1);
|
||||
kcp_->rx_minrto = 30;
|
||||
kcp_->snd_wnd = 128;
|
||||
kcp_->rcv_wnd = 128;
|
||||
|
||||
// 设置发送回调函数(KCP发送数据时调用)
|
||||
kcp_->output = IOCPKCPClient::kcpOutput;
|
||||
}
|
||||
|
||||
running_ = true;
|
||||
updateThread_ = std::thread(&IOCPKCPClient::KCPUpdateLoop, this);
|
||||
@@ -48,35 +77,96 @@ BOOL IOCPKCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// 取出(或继续交付)一条已完整的应用消息。调用方缓冲装不下时整条读出、
|
||||
// 分片交付,避免 ikcp_recv 返回 -3 且不消费导致队头永久卡死。
|
||||
int IOCPKCPClient::RecvOneLocked(char* buffer, int bufSize)
|
||||
{
|
||||
int peek = ikcp_peeksize(kcp_);
|
||||
if (peek <= 0)
|
||||
return 0;
|
||||
if (peek <= bufSize) {
|
||||
int n = ikcp_recv(kcp_, buffer, bufSize);
|
||||
return n > 0 ? n : 0;
|
||||
}
|
||||
std::vector<char> big(peek);
|
||||
int n = ikcp_recv(kcp_, big.data(), peek);
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
m_pendingRecv.assign(big.begin(), big.begin() + n);
|
||||
m_pendingPos = 0;
|
||||
return DrainPendingLocked(buffer, bufSize);
|
||||
}
|
||||
|
||||
int IOCPKCPClient::DrainPendingLocked(char* buffer, int bufSize)
|
||||
{
|
||||
if (bufSize <= 0)
|
||||
return 0;
|
||||
size_t remain = m_pendingRecv.size() - m_pendingPos;
|
||||
int take = (remain < (size_t)bufSize) ? (int)remain : bufSize;
|
||||
memcpy(buffer, m_pendingRecv.data() + m_pendingPos, take);
|
||||
m_pendingPos += take;
|
||||
if (m_pendingPos >= m_pendingRecv.size()) {
|
||||
m_pendingRecv.clear();
|
||||
m_pendingPos = 0;
|
||||
}
|
||||
return take;
|
||||
}
|
||||
|
||||
bool IOCPKCPClient::HasPendingAppData()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
// ikcp_peeksize 只读 KCP 队列,不消费数据;用它在不进 recvfrom 的前提下
|
||||
// 判断是否还有已到齐的消息。
|
||||
return kcp_ && (!m_pendingRecv.empty() || ikcp_peeksize(kcp_) > 0);
|
||||
}
|
||||
|
||||
// UDP收包线程调用,将收到的UDP包送入KCP处理,再尝试读取完整应用包
|
||||
int IOCPKCPClient::ReceiveData(char* buffer, int bufSize, int flags)
|
||||
{
|
||||
// 先调用基类接收UDP原始数据
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
if (!kcp_)
|
||||
return 0;
|
||||
// 先交付上一轮大消息的剩余分片、以及 KCP 里已经到齐的消息。这时不能
|
||||
// 先去 recvfrom:分片可能都已到齐而没有后续数据报,recvfrom 会一直阻塞。
|
||||
if (!m_pendingRecv.empty())
|
||||
return DrainPendingLocked(buffer, bufSize);
|
||||
if (ikcp_peeksize(kcp_) > 0)
|
||||
return RecvOneLocked(buffer, bufSize);
|
||||
}
|
||||
|
||||
// 没有待交付数据,再去收新的 UDP 数据报(外层已用 select 确认可读)
|
||||
char udpBuffer[1500] = { 0 };
|
||||
int recvLen = IOCPUDPClient::ReceiveData(udpBuffer, sizeof(udpBuffer), flags);
|
||||
if (recvLen <= 0)
|
||||
return recvLen;
|
||||
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
// 重连/析构可能在 recvfrom 期间把会话释放掉(kcp_ 置空),必须锁内复查。
|
||||
if (!kcp_)
|
||||
return 0;
|
||||
// 输入KCP协议栈
|
||||
int inputRet = ikcp_input(kcp_, udpBuffer, recvLen);
|
||||
if (inputRet < 0)
|
||||
return -1;
|
||||
|
||||
// 从KCP中读取应用层数据,写入buffer
|
||||
int kcpRecvLen = ikcp_recv(kcp_, buffer, bufSize);
|
||||
return kcpRecvLen; // >0表示收到完整应用数据,0表示无完整包
|
||||
if (ikcp_peeksize(kcp_) <= 0)
|
||||
return 0; // 数据报已收下,但还没有完整应用消息
|
||||
return RecvOneLocked(buffer, bufSize);
|
||||
}
|
||||
|
||||
bool IOCPKCPClient::ProcessRecvData(CBuffer* m_CompressedBuffer, char* szBuffer, int len, int flag)
|
||||
{
|
||||
int iReceivedLength = ReceiveData(szBuffer, len, flag);
|
||||
if (iReceivedLength <= 0)
|
||||
{}
|
||||
else {
|
||||
// 一条应用消息可能被切成多个分片交付(见 RecvOneLocked)。这里连续交付
|
||||
// 直到没有已到齐的数据为止,避免大消息要等下一个心跳/数据报才继续。
|
||||
for (;;) {
|
||||
int iReceivedLength = ReceiveData(szBuffer, len, flag);
|
||||
if (iReceivedLength <= 0)
|
||||
break;
|
||||
szBuffer[iReceivedLength] = 0;
|
||||
//正确接收就调用OnRead处理,转到OnRead
|
||||
OnServerReceiving(m_CompressedBuffer, szBuffer, iReceivedLength);
|
||||
if (!HasPendingAppData())
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -84,10 +174,10 @@ bool IOCPKCPClient::ProcessRecvData(CBuffer* m_CompressedBuffer, char* szBuffer,
|
||||
// 发送应用层数据时调用,转发给KCP协议栈
|
||||
int IOCPKCPClient::SendTo(const char* buf, int len, int flags)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
// 判空必须在锁内:重连/析构会在锁内把 kcp_ 置空,锁外判空后再取用会踩空指针。
|
||||
if (!kcp_)
|
||||
return -1;
|
||||
|
||||
std::lock_guard<std::mutex> lock(kcpMutex_);
|
||||
int ret = ikcp_send(kcp_, buf, len);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
Reference in new issue
Block a user