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
79 lines
2.7 KiB
C++
79 lines
2.7 KiB
C++
#include "IOCPUDPClient.h"
|
||
|
||
|
||
BOOL IOCPUDPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||
{
|
||
// 与基类一致:每次建连都清掉上一条连接的登录确认。UDP/KCP 重连复用同一个
|
||
// 对象,残留的 ack 会让协议切换的“登录被接受才撤销回退计时”立刻误判成功。
|
||
SetLoginAcked(false);
|
||
if (szServerIP != NULL && uPort != 0) {
|
||
SetServerAddress(szServerIP, uPort);
|
||
}
|
||
m_sCurIP = m_Domain.SelectIP();
|
||
unsigned short port = m_nHostPort;
|
||
|
||
// 创建 UDP socket
|
||
m_sClientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||
if (m_sClientSocket == INVALID_SOCKET) {
|
||
Mprintf("Failed to create UDP socket\n");
|
||
return FALSE;
|
||
}
|
||
|
||
// 初始化服务器地址结构
|
||
memset(&m_ServerAddr, 0, sizeof(m_ServerAddr));
|
||
m_ServerAddr.sin_family = AF_INET;
|
||
m_ServerAddr.sin_port = htons(port);
|
||
|
||
#ifdef _WIN32
|
||
m_ServerAddr.sin_addr.S_un.S_addr = inet_addr(m_sCurIP.c_str());
|
||
#else
|
||
if (inet_pton(AF_INET, m_sCurIP.c_str(), &m_ServerAddr.sin_addr) <= 0) {
|
||
Mprintf("Invalid address or address not supported\n");
|
||
closesocket(m_sClientSocket);
|
||
m_sClientSocket = INVALID_SOCKET;
|
||
return FALSE;
|
||
}
|
||
#endif
|
||
|
||
// UDP不调用 connect(),也不设置 TCP keep-alive 相关选项
|
||
Mprintf("UDP client socket created and ready to send.\n");
|
||
m_bConnected = TRUE;
|
||
|
||
// 创建工作线程(如果需要)
|
||
if (m_hWorkThread == NULL) {
|
||
#ifdef _WIN32
|
||
m_bIsRunning = TRUE;
|
||
m_hWorkThread = (HANDLE)__CreateThread(NULL, 0, WorkThreadProc, (LPVOID)this, 0, NULL);
|
||
m_bWorkThread = m_hWorkThread ? S_RUN : S_STOP;
|
||
m_bIsRunning = m_hWorkThread ? TRUE : FALSE;
|
||
#else
|
||
pthread_t id = 0;
|
||
int ret = (HANDLE)pthread_create(&id, nullptr, (void* (*)(void*))IOCPClient::WorkThreadProc, this);
|
||
if (ret == 0) {
|
||
m_bWorkThread = S_RUN;
|
||
m_bIsRunning = TRUE;
|
||
// 同 IOCPClient:macOS 的 pthread_t 是 _opaque_pthread_t*,不能经
|
||
// static_cast<uintptr_t> 中转,reinterpret_cast 对整型/指针两种实现都成立。
|
||
m_hWorkThread = reinterpret_cast<HANDLE>(id);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
int IOCPUDPClient::ReceiveData(char* buffer, int bufSize, int flags)
|
||
{
|
||
sockaddr_in fromAddr;
|
||
int fromLen = sizeof(fromAddr);
|
||
return recvfrom(m_sClientSocket, buffer, bufSize - 1, flags, (sockaddr*)&fromAddr, &fromLen);
|
||
}
|
||
|
||
int IOCPUDPClient::SendTo(const char* buf, int len, int flags)
|
||
{
|
||
if (len > 1200) {
|
||
Mprintf("UDP large packet may lost: %d bytes\n", len);
|
||
}
|
||
return ::sendto(m_sClientSocket, buf, len, flags, (sockaddr*)&m_ServerAddr, sizeof(m_ServerAddr));
|
||
}
|