2026-01-02 20:00:18 +01:00
|
|
|
|
#include "IOCPKCPClient.h"
|
2025-07-20 04:42:29 +08:00
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
IOCPKCPClient::IOCPKCPClient(State& bExit, bool exit_while_disconnect)
|
2025-10-15 04:32:59 +08:00
|
|
|
|
: IOCPUDPClient(bExit, exit_while_disconnect), kcp_(nullptr), running_(false)
|
2025-07-20 04:42:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IOCPKCPClient::~IOCPKCPClient()
|
|
|
|
|
|
{
|
2025-10-15 04:32:59 +08:00
|
|
|
|
running_ = false;
|
|
|
|
|
|
if (updateThread_.joinable())
|
|
|
|
|
|
updateThread_.join();
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
if (kcp_)
|
|
|
|
|
|
ikcp_release(kcp_);
|
2025-07-20 04:42:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL IOCPKCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
|
|
|
|
|
{
|
2025-10-15 04:32:59 +08:00
|
|
|
|
BOOL ret = IOCPUDPClient::ConnectServer(szServerIP, uPort);
|
|
|
|
|
|
if (!ret)
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 初始化KCP
|
|
|
|
|
|
uint32_t conv = KCP_SESSION_ID; // conv 要与服务端匹配
|
2025-10-15 04:32:59 +08:00
|
|
|
|
kcp_ = ikcp_create(conv, this);
|
|
|
|
|
|
if (!kcp_)
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 设置KCP参数
|
2025-10-15 04:32:59 +08:00
|
|
|
|
ikcp_nodelay(kcp_, 1, 40, 2, 0);
|
|
|
|
|
|
kcp_->rx_minrto = 30;
|
|
|
|
|
|
kcp_->snd_wnd = 128;
|
|
|
|
|
|
kcp_->rcv_wnd = 128;
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 设置发送回调函数(KCP发送数据时调用)
|
2025-10-15 04:32:59 +08:00
|
|
|
|
kcp_->output = IOCPKCPClient::kcpOutput;
|
|
|
|
|
|
|
|
|
|
|
|
running_ = true;
|
|
|
|
|
|
updateThread_ = std::thread(&IOCPKCPClient::KCPUpdateLoop, this);
|
|
|
|
|
|
m_bConnected = TRUE;
|
|
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// UDP收包线程调用,将收到的UDP包送入KCP处理,再尝试读取完整应用包
|
2025-07-20 04:42:29 +08:00
|
|
|
|
int IOCPKCPClient::ReceiveData(char* buffer, int bufSize, int flags)
|
|
|
|
|
|
{
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 先调用基类接收UDP原始数据
|
2025-10-15 04:32:59 +08:00
|
|
|
|
char udpBuffer[1500] = { 0 };
|
|
|
|
|
|
int recvLen = IOCPUDPClient::ReceiveData(udpBuffer, sizeof(udpBuffer), flags);
|
|
|
|
|
|
if (recvLen <= 0)
|
|
|
|
|
|
return recvLen;
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 输入KCP协议栈
|
2025-10-15 04:32:59 +08:00
|
|
|
|
int inputRet = ikcp_input(kcp_, udpBuffer, recvLen);
|
|
|
|
|
|
if (inputRet < 0)
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 从KCP中读取应用层数据,写入buffer
|
2025-10-15 04:32:59 +08:00
|
|
|
|
int kcpRecvLen = ikcp_recv(kcp_, buffer, bufSize);
|
2026-01-02 20:00:18 +01:00
|
|
|
|
return kcpRecvLen; // >0表示收到完整应用数据,0表示无完整包
|
2025-07-20 04:42:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
bool IOCPKCPClient::ProcessRecvData(CBuffer* m_CompressedBuffer, char* szBuffer, int len, int flag)
|
|
|
|
|
|
{
|
|
|
|
|
|
int iReceivedLength = ReceiveData(szBuffer, len, flag);
|
|
|
|
|
|
if (iReceivedLength <= 0)
|
|
|
|
|
|
{}
|
|
|
|
|
|
else {
|
|
|
|
|
|
szBuffer[iReceivedLength] = 0;
|
2026-01-02 20:00:18 +01:00
|
|
|
|
//正确接收就调用OnRead处理,转到OnRead
|
2025-10-15 04:32:59 +08:00
|
|
|
|
OnServerReceiving(m_CompressedBuffer, szBuffer, iReceivedLength);
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 发送应用层数据时调用,转发给KCP协议栈
|
2025-07-20 04:42:29 +08:00
|
|
|
|
int IOCPKCPClient::SendTo(const char* buf, int len, int flags)
|
|
|
|
|
|
{
|
2025-10-15 04:32:59 +08:00
|
|
|
|
if (!kcp_)
|
|
|
|
|
|
return -1;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
int ret = ikcp_send(kcp_, buf, len);
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
|
return -1;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 主动调用flush,加快发送
|
2025-10-15 04:32:59 +08:00
|
|
|
|
ikcp_flush(kcp_);
|
|
|
|
|
|
return ret;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// KCP发送数据回调,将KCP生成的UDP包发送出去
|
2025-07-20 04:42:29 +08:00
|
|
|
|
int IOCPKCPClient::kcpOutput(const char* buf, int len, struct IKCPCB* kcp, void* user)
|
|
|
|
|
|
{
|
2025-10-15 04:32:59 +08:00
|
|
|
|
IOCPKCPClient* client = reinterpret_cast<IOCPKCPClient*>(user);
|
|
|
|
|
|
if (client->m_sClientSocket == INVALID_SOCKET)
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
int sentLen = sendto(client->m_sClientSocket, buf, len, 0, (sockaddr*)&client->m_ServerAddr, sizeof(client->m_ServerAddr));
|
|
|
|
|
|
if (sentLen == len)
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
else
|
|
|
|
|
|
return -1;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 独立线程定时调用ikcp_update,保持KCP协议正常工作
|
2025-07-20 04:42:29 +08:00
|
|
|
|
void IOCPKCPClient::KCPUpdateLoop()
|
|
|
|
|
|
{
|
2025-10-15 04:32:59 +08:00
|
|
|
|
while (running_ && !g_bExit) {
|
|
|
|
|
|
IUINT32 current = GetTickCount64();
|
|
|
|
|
|
ikcp_update(kcp_, current);
|
2026-01-02 20:00:18 +01:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(20)); // 20ms周期,视需求调整
|
2025-10-15 04:32:59 +08:00
|
|
|
|
}
|
2025-07-20 04:42:29 +08:00
|
|
|
|
}
|