添加 KCPWrap

This commit is contained in:
_Redstone_c_ 2021-01-03 12:09:39 +08:00
parent 2b44e60c56
commit d2877f6b10
4 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,95 @@
#include "KCPWrap.h"
#include "Logging.h"
namespace FKCPFuncWrap
{
int Output(const char* buf, int len, ikcpcb* kcp, void* user)
{
FKCPWrap* KCPWrap = (FKCPWrap*)user;
if (KCPWrap->OutputFunc) return KCPWrap->OutputFunc((const uint8*)buf, len);
return 0;
}
void Writelog(const char* log, ikcpcb* kcp, void* user)
{
UE_LOG(LogKCP, Log, TEXT("[%i]: %s"), kcp->conv, log);
}
}
FKCPWrap::FKCPWrap(uint32 Conv)
{
KCPPtr = ikcp_create(Conv, this);
KCPPtr->output = &FKCPFuncWrap::Output;
KCPPtr->writelog = &FKCPFuncWrap::Writelog;
}
FKCPWrap::~FKCPWrap()
{
ikcp_release(KCPPtr);
}
int FKCPWrap::Recv(uint8 * Data, int32 Count)
{
return ikcp_recv(KCPPtr, (char*)Data, Count);
}
int FKCPWrap::Send(const uint8 * Data, int32 Count)
{
return ikcp_send(KCPPtr, (const char*)Data, Count);
}
void FKCPWrap::Update(uint32 Current)
{
ikcp_update(KCPPtr, Current);
}
void FKCPWrap::Check(uint32 Current) const
{
ikcp_check(KCPPtr, Current);
}
int FKCPWrap::Input(const uint8 * Data, int32 Count)
{
return ikcp_input(KCPPtr, (const char*)Data, Count);
}
void FKCPWrap::Flush()
{
ikcp_flush(KCPPtr);
}
int FKCPWrap::PeekSize() const
{
return ikcp_peeksize(KCPPtr);
}
int FKCPWrap::SetMTU(int32 MTU)
{
return ikcp_setmtu(KCPPtr, MTU);
}
int FKCPWrap::SetWindowSize(int32 SentWindow, int32 RecvWindow)
{
return ikcp_wndsize(KCPPtr, SentWindow, RecvWindow);
}
int FKCPWrap::GetWaitSent() const
{
return ikcp_waitsnd(KCPPtr);
}
int FKCPWrap::SetNoDelay(int32 NoDelay, int32 Internal, int32 FastResend, int32 NC)
{
return ikcp_nodelay(KCPPtr, NoDelay, Internal, FastResend, NC);
}
int FKCPWrap::SetNormalMode()
{
return SetNoDelay(0, 40, 0, 0);
}
int FKCPWrap::SetTurboMode()
{
return SetNoDelay(1, 10, 2, 1);
}

View File

@ -0,0 +1,3 @@
#include "Logging.h"
DEFINE_LOG_CATEGORY(LogKCP);

View File

@ -0,0 +1,6 @@
#pragma once
#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"
DECLARE_LOG_CATEGORY_EXTERN(LogKCP, Log, All);

46
Source/ThirdParty/KCP/Public/KCPWrap.h vendored Normal file
View File

@ -0,0 +1,46 @@
#pragma once
#include "CoreMinimal.h"
#include "ikcp.h"
class KCP_API FKCPWrap
{
public:
FKCPWrap(uint32 Conv);
~FKCPWrap();
int Recv(uint8* Data, int32 Count);
int Send(const uint8* Data, int32 Count);
void Update(uint32 Current);
void Check(uint32 Current) const;
int Input(const uint8* Data, int32 Count);
void Flush();
int PeekSize() const;
int SetMTU(int32 MTU = 1400);
int SetWindowSize(int32 SentWindow = -1, int32 RecvWindow = -1);
int GetWaitSent() const;
int SetNoDelay(int32 NoDelay = -1, int32 Internal = -1, int32 FastResend = -1, int32 NC = -1);
int SetNormalMode();
int SetTurboMode();
TFunction<int(const uint8* Data, int32 Count)> OutputFunc;
private:
ikcpcb* KCPPtr;
};