diff --git a/Source/ThirdParty/KCP/Private/KCPWrap.cpp b/Source/ThirdParty/KCP/Private/KCPWrap.cpp new file mode 100644 index 0000000..2b7d74f --- /dev/null +++ b/Source/ThirdParty/KCP/Private/KCPWrap.cpp @@ -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); +} diff --git a/Source/ThirdParty/KCP/Private/Logging.cpp b/Source/ThirdParty/KCP/Private/Logging.cpp new file mode 100644 index 0000000..d422e94 --- /dev/null +++ b/Source/ThirdParty/KCP/Private/Logging.cpp @@ -0,0 +1,3 @@ +#include "Logging.h" + +DEFINE_LOG_CATEGORY(LogKCP); diff --git a/Source/ThirdParty/KCP/Private/Logging.h b/Source/ThirdParty/KCP/Private/Logging.h new file mode 100644 index 0000000..86905dd --- /dev/null +++ b/Source/ThirdParty/KCP/Private/Logging.h @@ -0,0 +1,6 @@ +#pragma once + +#include "CoreMinimal.h" +#include "Modules/ModuleManager.h" + +DECLARE_LOG_CATEGORY_EXTERN(LogKCP, Log, All); diff --git a/Source/ThirdParty/KCP/Public/KCPWrap.h b/Source/ThirdParty/KCP/Public/KCPWrap.h new file mode 100644 index 0000000..9f0e5c9 --- /dev/null +++ b/Source/ThirdParty/KCP/Public/KCPWrap.h @@ -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 OutputFunc; + +private: + + ikcpcb* KCPPtr; + +};