From d68d6eeec0b9337bc797a857b06822790e2b8225 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ <2824517378@qq.com> Date: Mon, 4 Jan 2021 21:15:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20Server=20=E5=92=8C=20Client=20?= =?UTF-8?q?=E4=BB=8E=20UActorComponent=20=E9=87=8D=E6=9E=84=E4=B8=BA=20UOb?= =?UTF-8?q?ject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RSHWNetwork.uplugin | 5 +++ .../RSHWNetwork/Private/RSHWNetworkClient.cpp | 45 ++++--------------- .../RSHWNetwork/Private/RSHWNetworkServer.cpp | 39 +++------------- Source/RSHWNetwork/Public/RSHWNetworkClient.h | 36 +++++++++------ Source/RSHWNetwork/Public/RSHWNetworkServer.h | 33 ++++++++------ 5 files changed, 61 insertions(+), 97 deletions(-) diff --git a/RSHWNetwork.uplugin b/RSHWNetwork.uplugin index 595b0a4..0e1d314 100644 --- a/RSHWNetwork.uplugin +++ b/RSHWNetwork.uplugin @@ -19,6 +19,11 @@ "Name": "RSHWNetwork", "Type": "Runtime", "LoadingPhase": "Default" + }, + { + "Name": "KCP", + "Type": "Runtime", + "LoadingPhase": "Default" } ] } \ No newline at end of file diff --git a/Source/RSHWNetwork/Private/RSHWNetworkClient.cpp b/Source/RSHWNetwork/Private/RSHWNetworkClient.cpp index b713790..3c96164 100644 --- a/Source/RSHWNetwork/Private/RSHWNetworkClient.cpp +++ b/Source/RSHWNetwork/Private/RSHWNetworkClient.cpp @@ -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& 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(ClientPass.ID, GetName()); + KCPUnit = MakeShared(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() diff --git a/Source/RSHWNetwork/Private/RSHWNetworkServer.cpp b/Source/RSHWNetwork/Private/RSHWNetworkServer.cpp index f3a4687..684374c 100644 --- a/Source/RSHWNetwork/Private/RSHWNetworkServer.cpp +++ b/Source/RSHWNetwork/Private/RSHWNetworkServer.cpp @@ -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& 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(NewRegistration.Pass.ID, FString::Printf(TEXT("%s[%i]"), *GetPathName(), NewRegistration.Pass.ID)); + NewRegistration.KCPUnit = MakeShared(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 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() diff --git a/Source/RSHWNetwork/Public/RSHWNetworkClient.h b/Source/RSHWNetwork/Public/RSHWNetworkClient.h index 4cf4a82..09608ec 100644 --- a/Source/RSHWNetwork/Public/RSHWNetworkClient.h +++ b/Source/RSHWNetwork/Public/RSHWNetworkClient.h @@ -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& Data); @@ -56,6 +64,8 @@ public: private: + bool bIsActive = false; + TSharedPtr 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; diff --git a/Source/RSHWNetwork/Public/RSHWNetworkServer.h b/Source/RSHWNetwork/Public/RSHWNetworkServer.h index 9e4afa3..699c798 100644 --- a/Source/RSHWNetwork/Public/RSHWNetworkServer.h +++ b/Source/RSHWNetwork/Public/RSHWNetworkServer.h @@ -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& Data); @@ -56,6 +61,8 @@ public: private: + bool bIsActive = false; + FSocket* SocketPtr; TArray 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;