优化 FKCPWrap

修复 Log 乱码
允许直接获取 KCPCB
添加 DebugName 区分不同 KCP 对象
This commit is contained in:
_Redstone_c_ 2021-01-03 17:28:53 +08:00
parent d2877f6b10
commit 39be28a997
2 changed files with 36 additions and 3 deletions

View File

@ -13,22 +13,35 @@ namespace FKCPFuncWrap
void Writelog(const char* log, ikcpcb* kcp, void* user)
{
UE_LOG(LogKCP, Log, TEXT("[%i]: %s"), kcp->conv, log);
FKCPWrap* KCPWrap = (FKCPWrap*)user;
UE_LOG(LogKCP, Log, TEXT("%s: %s"), *KCPWrap->GetDebugName(), ANSI_TO_TCHAR(log));
}
}
FKCPWrap::FKCPWrap(uint32 Conv)
: KCPPtr(ikcp_create(Conv, this))
, DebugName(FString::Printf(TEXT("[%i]"), Conv))
{
KCPPtr = ikcp_create(Conv, this);
KCPPtr->output = &FKCPFuncWrap::Output;
KCPPtr->writelog = &FKCPFuncWrap::Writelog;
}
FKCPWrap::FKCPWrap(uint32 Conv, const FString & InDebugName)
: FKCPWrap(Conv)
{
DebugName = InDebugName;
}
FKCPWrap::~FKCPWrap()
{
ikcp_release(KCPPtr);
}
ikcpcb & FKCPWrap::GetKCPCB()
{
return *KCPPtr;
}
int FKCPWrap::Recv(uint8 * Data, int32 Count)
{
return ikcp_recv(KCPPtr, (char*)Data, Count);
@ -93,3 +106,13 @@ int FKCPWrap::SetTurboMode()
{
return SetNoDelay(1, 10, 2, 1);
}
void FKCPWrap::SetDebugName(const FString & InDebugName)
{
DebugName = InDebugName;
}
const FString & FKCPWrap::GetDebugName() const
{
return DebugName;
}

View File

@ -9,8 +9,12 @@ public:
FKCPWrap(uint32 Conv);
FKCPWrap(uint32 Conv, const FString& InDebugName);
~FKCPWrap();
ikcpcb& GetKCPCB();
int Recv(uint8* Data, int32 Count);
int Send(const uint8* Data, int32 Count);
@ -37,10 +41,16 @@ public:
int SetTurboMode();
TFunction<int(const uint8* Data, int32 Count)> OutputFunc;
TFunction<int32(const uint8* Data, int32 Count)> OutputFunc;
void SetDebugName(const FString& InDebugName);
const FString& GetDebugName() const;
private:
ikcpcb* KCPPtr;
FString DebugName;
};