2026-01-02 20:00:18 +01:00
|
|
|
|
#pragma once
|
2025-07-20 04:42:29 +08:00
|
|
|
|
#include "IOCPUDPClient.h"
|
|
|
|
|
|
#include "ikcp.h"
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
|
|
|
|
class IOCPKCPClient : public IOCPUDPClient
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
2025-10-15 04:32:59 +08:00
|
|
|
|
IOCPKCPClient(State& bExit, bool exit_while_disconnect = false);
|
|
|
|
|
|
virtual ~IOCPKCPClient();
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
virtual BOOL ConnectServer(const char* szServerIP, unsigned short uPort) override;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 重写接收函数:输入UDP数据给KCP,输出KCP层解包后的数据
|
2025-10-15 04:32:59 +08:00
|
|
|
|
virtual int ReceiveData(char* buffer, int bufSize, int flags) override;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
virtual bool ProcessRecvData(CBuffer* m_CompressedBuffer, char* szBuffer, int len, int flag) override;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 重写发送函数:将应用数据通过KCP发送
|
2025-10-15 04:32:59 +08:00
|
|
|
|
virtual int SendTo(const char* buf, int len, int flags) override;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// KCP发送数据的回调函数,负责调用UDP的sendto
|
2025-10-15 04:32:59 +08:00
|
|
|
|
static int kcpOutput(const char* buf, int len, struct IKCPCB* kcp, void* user);
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 定时调用ikcp_update的线程函数
|
2025-10-15 04:32:59 +08:00
|
|
|
|
void KCPUpdateLoop();
|
2025-07-20 04:42:29 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
2025-10-15 04:32:59 +08:00
|
|
|
|
ikcpcb* kcp_;
|
|
|
|
|
|
std::thread updateThread_;
|
|
|
|
|
|
std::atomic<bool> running_;
|
2025-07-20 04:42:29 +08:00
|
|
|
|
};
|