将 Server 和 Client 从 UActorComponent 重构为 UObject

This commit is contained in:
_Redstone_c_ 2021-01-04 21:15:41 +08:00
parent 4860bc70d4
commit d68d6eeec0
5 changed files with 61 additions and 97 deletions

View File

@ -19,6 +19,11 @@
"Name": "RSHWNetwork",
"Type": "Runtime",
"LoadingPhase": "Default"
},
{
"Name": "KCP",
"Type": "Runtime",
"LoadingPhase": "Default"
}
]
}

View File

@ -6,12 +6,6 @@
#include "IPAddress.h"
#include "SocketSubsystem.h"
URSHWNetworkClient::URSHWNetworkClient(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryComponentTick.bCanEverTick = true;
}
bool URSHWNetworkClient::Send(const TArray<uint8>& Data)
{
if (!IsActive() || !(ClientPass.ID | ClientPass.Key)) return false;
@ -42,22 +36,8 @@ int32 URSHWNetworkClient::UDPSend(const uint8 * Data, int32 Count)
return 0;
}
void URSHWNetworkClient::BeginPlay()
void URSHWNetworkClient::Tick(float DeltaTime)
{
Super::BeginPlay();
}
void URSHWNetworkClient::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Deactivate();
Super::EndPlay(EndPlayReason);
}
void URSHWNetworkClient::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction * ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (!IsActive()) return;
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
@ -126,11 +106,11 @@ void URSHWNetworkClient::TickComponent(float DeltaTime, ELevelTick TickType, FAc
SourcePass.Key |= (int32)RecvBuffer[7] << 24;
// is registration request
if (!(ClientPass.ID | ClientPass.Key))
if (!IsLogged())
{
ClientPass = SourcePass;
KCPUnit = MakeShared<FKCPWrap>(ClientPass.ID, GetName());
KCPUnit = MakeShared<FKCPWrap>(ClientPass.ID, FString::Printf(TEXT("Client-%i"), ClientPass.ID));
KCPUnit->SetTurboMode();
KCPUnit->GetKCPCB().logmask = KCPLogMask;
@ -180,7 +160,7 @@ void URSHWNetworkClient::TickComponent(float DeltaTime, ELevelTick TickType, FAc
// handle timeout
{
if ((ClientPass.ID | ClientPass.Key) && NowTime - LastRecvTime > TimeoutLimit)
if (IsLogged() && NowTime - LastRecvTime > TimeoutLimit)
{
ClientPass.ID = 0;
ClientPass.Key = 0;
@ -196,9 +176,8 @@ void URSHWNetworkClient::TickComponent(float DeltaTime, ELevelTick TickType, FAc
void URSHWNetworkClient::Activate(bool bReset)
{
if (!GetOwner()->GetGameInstance()) return;
if (bReset) Deactivate();
if (!ShouldActivate()) return;
if (bIsActive) return;
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
@ -242,17 +221,14 @@ void URSHWNetworkClient::Activate(bool bReset)
LastHeartbeat = FDateTime::MinValue();
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Client activate."));
SetComponentTickEnabled(true);
SetActiveFlag(true);
OnComponentActivated.Broadcast(this, bReset);
bIsActive = true;
}
void URSHWNetworkClient::Deactivate()
{
if (ShouldActivate()) return;
if (!bIsActive) return;
if (ClientPass.ID | ClientPass.Key)
if (IsLogged())
{
OnUnlogin.Broadcast();
}
@ -272,10 +248,7 @@ void URSHWNetworkClient::Deactivate()
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Client deactivate."));
SetComponentTickEnabled(false);
SetActiveFlag(false);
OnComponentDeactivated.Broadcast(this);
bIsActive = false;
}
void URSHWNetworkClient::BeginDestroy()

View File

@ -7,12 +7,6 @@
#include "SocketSubsystem.h"
#include "HAL/UnrealMemory.h"
URSHWNetworkServer::URSHWNetworkServer(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryComponentTick.bCanEverTick = true;
}
bool URSHWNetworkServer::Send(int32 ClientID, const TArray<uint8>& Data)
{
if (!IsActive() || !Registration.Contains(ClientID)) return false;
@ -47,22 +41,8 @@ int32 URSHWNetworkServer::UDPSend(int32 ClientID, const uint8* Data, int32 Count
return 0;
}
void URSHWNetworkServer::BeginPlay()
void URSHWNetworkServer::Tick(float DeltaTime)
{
Super::BeginPlay();
}
void URSHWNetworkServer::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Deactivate();
Super::EndPlay(EndPlayReason);
}
void URSHWNetworkServer::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction * ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (!IsActive()) return;
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
@ -214,7 +194,7 @@ void URSHWNetworkServer::TickComponent(float DeltaTime, ELevelTick TickType, FAc
NewRegistration.Heartbeat = FDateTime::MinValue();
NewRegistration.Addr = SourceAddr;
NewRegistration.KCPUnit = MakeShared<FKCPWrap>(NewRegistration.Pass.ID, FString::Printf(TEXT("%s[%i]"), *GetPathName(), NewRegistration.Pass.ID));
NewRegistration.KCPUnit = MakeShared<FKCPWrap>(NewRegistration.Pass.ID, FString::Printf(TEXT("Server-%i"), NewRegistration.Pass.ID));
NewRegistration.KCPUnit->SetTurboMode();
NewRegistration.KCPUnit->GetKCPCB().logmask = KCPLogMask;
@ -313,9 +293,8 @@ void URSHWNetworkServer::TickComponent(float DeltaTime, ELevelTick TickType, FAc
void URSHWNetworkServer::Activate(bool bReset)
{
if (!GetOwner()->GetGameInstance()) return;
if (bReset) Deactivate();
if (!ShouldActivate()) return;
if (bIsActive) return;
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
@ -356,15 +335,12 @@ void URSHWNetworkServer::Activate(bool bReset)
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Server activate."));
SetComponentTickEnabled(true);
SetActiveFlag(true);
OnComponentActivated.Broadcast(this, bReset);
bIsActive = true;
}
void URSHWNetworkServer::Deactivate()
{
if (ShouldActivate()) return;
if (!bIsActive) return;
TArray<int32> RegistrationAddr;
Registration.GetKeys(RegistrationAddr);
@ -387,10 +363,7 @@ void URSHWNetworkServer::Deactivate()
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Server deactivate."));
SetComponentTickEnabled(false);
SetActiveFlag(false);
OnComponentDeactivated.Broadcast(this);
bIsActive = false;
}
void URSHWNetworkServer::BeginDestroy()

View File

@ -2,22 +2,18 @@
#include "CoreMinimal.h"
#include "Misc/DateTime.h"
#include "UObject/Object.h"
#include "RSHWNetworkType.h"
#include "Components/ActorComponent.h"
#include "RSHWNetworkClient.generated.h"
class FKCPWrap;
class FInternetAddr;
UCLASS(BlueprintType, hidecategories = ("Cooking", "ComponentReplication"), meta = (BlueprintSpawnableComponent))
class RSHWNETWORK_API URSHWNetworkClient : public UActorComponent
UCLASS(BlueprintType)
class RSHWNETWORK_API URSHWNetworkClient : public UObject, public FTickableGameObject
{
GENERATED_BODY()
public:
URSHWNetworkClient(const FObjectInitializer& ObjectInitializer);
public:
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE(FLoginSignature, URSHWNetworkClient, OnLogin);
@ -37,6 +33,18 @@ public:
public:
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
bool IsActive() const { return bIsActive; }
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
void Activate(bool bReset = false);
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
void Deactivate();
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
bool IsLogged() const { return ClientPass.ID | ClientPass.Key; }
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
bool Send(const TArray<uint8>& Data);
@ -56,6 +64,8 @@ public:
private:
bool bIsActive = false;
TSharedPtr<FInternetAddr> ServerAddrPtr;
FSocket* SocketPtr;
@ -75,13 +85,11 @@ private:
public:
//~ Begin UActorComponent Interface
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
virtual void Activate(bool bReset = false) override;
virtual void Deactivate() override;
//~ End UActorComponent Interface
//~ Begin FTickableGameObject Interface
virtual void Tick(float DeltaTime) override;
virtual bool IsTickable() const override { return !IsTemplate() && IsActive(); }
virtual TStatId GetStatId() const override { return GetStatID(); }
//~ End FTickableGameObject Interface
//~ Begin UObject Interface
virtual void BeginDestroy() override;

View File

@ -2,22 +2,18 @@
#include "CoreMinimal.h"
#include "Misc/DateTime.h"
#include "UObject/Object.h"
#include "RSHWNetworkType.h"
#include "Components/ActorComponent.h"
#include "RSHWNetworkServer.generated.h"
class FSocket;
class FKCPWrap;
UCLASS(BlueprintType, hidecategories = ("Cooking", "ComponentReplication"), meta = (BlueprintSpawnableComponent))
class RSHWNETWORK_API URSHWNetworkServer : public UActorComponent
UCLASS(BlueprintType)
class RSHWNETWORK_API URSHWNetworkServer : public UObject, public FTickableGameObject
{
GENERATED_BODY()
public:
URSHWNetworkServer(const FObjectInitializer& ObjectInitializer);
public:
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_OneParam(FLoginSignature, URSHWNetworkServer, OnLogin, int32, ClientID);
@ -37,6 +33,15 @@ public:
public:
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
bool IsActive() const { return bIsActive; }
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
void Activate(bool bReset = false);
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
void Deactivate();
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
bool Send(int32 ClientID, const TArray<uint8>& Data);
@ -56,6 +61,8 @@ public:
private:
bool bIsActive = false;
FSocket* SocketPtr;
TArray<uint8> SendBuffer;
@ -87,13 +94,11 @@ private:
public:
//~ Begin UActorComponent Interface
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
virtual void Activate(bool bReset = false) override;
virtual void Deactivate() override;
//~ End UActorComponent Interface
//~ Begin FTickableGameObject Interface
virtual void Tick(float DeltaTime) override;
virtual bool IsTickable() const override { return !IsTemplate() && IsActive(); }
virtual TStatId GetStatId() const override { return GetStatID(); }
//~ End FTickableGameObject Interface
//~ Begin UObject Interface
virtual void BeginDestroy() override;