Feature: Implement KCP protocol - based on UDP
This commit is contained in:
118
client/IOCPKCPClient.cpp
Normal file
118
client/IOCPKCPClient.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
#include "IOCPKCPClient.h"
|
||||
#include <windows.h>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
IOCPKCPClient::IOCPKCPClient(State& bExit, bool exit_while_disconnect)
|
||||
: IOCPUDPClient(bExit, exit_while_disconnect), kcp_(nullptr), running_(false)
|
||||
{
|
||||
}
|
||||
|
||||
IOCPKCPClient::~IOCPKCPClient()
|
||||
{
|
||||
running_ = false;
|
||||
if (updateThread_.joinable())
|
||||
updateThread_.join();
|
||||
|
||||
if (kcp_)
|
||||
ikcp_release(kcp_);
|
||||
}
|
||||
|
||||
BOOL IOCPKCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
{
|
||||
BOOL ret = IOCPUDPClient::ConnectServer(szServerIP, uPort);
|
||||
if (!ret)
|
||||
return FALSE;
|
||||
|
||||
// <20><>ʼ<EFBFBD><CABC>KCP
|
||||
uint32_t conv = KCP_SESSION_ID; // conv Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
|
||||
kcp_ = ikcp_create(conv, this);
|
||||
if (!kcp_)
|
||||
return FALSE;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>KCP<43><50><EFBFBD><EFBFBD>
|
||||
ikcp_nodelay(kcp_, 1, 40, 2, 0);
|
||||
kcp_->rx_minrto = 30;
|
||||
kcp_->snd_wnd = 128;
|
||||
kcp_->rcv_wnd = 128;
|
||||
|
||||
// <20><><EFBFBD>÷<EFBFBD><C3B7>ͻص<CDBB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>KCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ã<EFBFBD>
|
||||
kcp_->output = IOCPKCPClient::kcpOutput;
|
||||
|
||||
running_ = true;
|
||||
updateThread_ = std::thread(&IOCPKCPClient::KCPUpdateLoop, this);
|
||||
m_bConnected = TRUE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// UDP<44>հ<EFBFBD><D5B0>̵߳<DFB3><CCB5>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD>UDP<44><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>KCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٳ<EFBFBD><D9B3>Զ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ӧ<EFBFBD>ð<EFBFBD>
|
||||
int IOCPKCPClient::ReceiveData(char* buffer, int bufSize, int flags)
|
||||
{
|
||||
// <20>ȵ<EFBFBD><C8B5>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UDPԭʼ<D4AD><CABC><EFBFBD><EFBFBD>
|
||||
char udpBuffer[1500] = { 0 };
|
||||
int recvLen = IOCPUDPClient::ReceiveData(udpBuffer, sizeof(udpBuffer), flags);
|
||||
if (recvLen <= 0)
|
||||
return recvLen;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>KCPЭ<50><D0AD>ջ
|
||||
int inputRet = ikcp_input(kcp_, udpBuffer, recvLen);
|
||||
if (inputRet < 0)
|
||||
return -1;
|
||||
|
||||
// <20><>KCP<43>ж<EFBFBD>ȡӦ<C8A1>ò<EFBFBD><C3B2><EFBFBD><EFBFBD>ݣ<EFBFBD>д<EFBFBD><D0B4>buffer
|
||||
int kcpRecvLen = ikcp_recv(kcp_, buffer, bufSize);
|
||||
return kcpRecvLen; // >0<><30>ʾ<EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>0<EFBFBD><30>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
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;
|
||||
//<2F><>ȷ<EFBFBD><C8B7><EFBFBD>վ͵<D5BE><CDB5><EFBFBD>OnRead<61><64><EFBFBD><EFBFBD>,ת<><D7AA>OnRead
|
||||
OnServerReceiving(m_CompressedBuffer, szBuffer, iReceivedLength);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ã<EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>KCPЭ<50><D0AD>ջ
|
||||
int IOCPKCPClient::SendTo(const char* buf, int len, int flags)
|
||||
{
|
||||
if (!kcp_)
|
||||
return -1;
|
||||
|
||||
int ret = ikcp_send(kcp_, buf, len);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>flush<73><68><EFBFBD>ӿ췢<D3BF><ECB7A2>
|
||||
ikcp_flush(kcp_);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// KCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻص<DDBB><D8B5><EFBFBD><EFBFBD><EFBFBD>KCP<43><50><EFBFBD>ɵ<EFBFBD>UDP<44><50><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>ȥ
|
||||
int IOCPKCPClient::kcpOutput(const char* buf, int len, struct IKCPCB* kcp, void* user)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>̶߳<DFB3>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ikcp_update<74><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>KCPЭ<50><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void IOCPKCPClient::KCPUpdateLoop()
|
||||
{
|
||||
while (running_ && !g_bExit)
|
||||
{
|
||||
IUINT32 current = GetTickCount64();
|
||||
ikcp_update(kcp_, current);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(20)); // 20ms<6D><73><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user