将 Server 和 Client 从 UActorComponent 重构为 UObject
This commit is contained in:
parent
4860bc70d4
commit
d68d6eeec0
@ -19,6 +19,11 @@
|
|||||||
"Name": "RSHWNetwork",
|
"Name": "RSHWNetwork",
|
||||||
"Type": "Runtime",
|
"Type": "Runtime",
|
||||||
"LoadingPhase": "Default"
|
"LoadingPhase": "Default"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "KCP",
|
||||||
|
"Type": "Runtime",
|
||||||
|
"LoadingPhase": "Default"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -6,12 +6,6 @@
|
|||||||
#include "IPAddress.h"
|
#include "IPAddress.h"
|
||||||
#include "SocketSubsystem.h"
|
#include "SocketSubsystem.h"
|
||||||
|
|
||||||
URSHWNetworkClient::URSHWNetworkClient(const FObjectInitializer& ObjectInitializer)
|
|
||||||
: Super(ObjectInitializer)
|
|
||||||
{
|
|
||||||
PrimaryComponentTick.bCanEverTick = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool URSHWNetworkClient::Send(const TArray<uint8>& Data)
|
bool URSHWNetworkClient::Send(const TArray<uint8>& Data)
|
||||||
{
|
{
|
||||||
if (!IsActive() || !(ClientPass.ID | ClientPass.Key)) return false;
|
if (!IsActive() || !(ClientPass.ID | ClientPass.Key)) return false;
|
||||||
@ -42,22 +36,8 @@ int32 URSHWNetworkClient::UDPSend(const uint8 * Data, int32 Count)
|
|||||||
return 0;
|
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;
|
if (!IsActive()) return;
|
||||||
|
|
||||||
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
|
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
|
||||||
@ -126,11 +106,11 @@ void URSHWNetworkClient::TickComponent(float DeltaTime, ELevelTick TickType, FAc
|
|||||||
SourcePass.Key |= (int32)RecvBuffer[7] << 24;
|
SourcePass.Key |= (int32)RecvBuffer[7] << 24;
|
||||||
|
|
||||||
// is registration request
|
// is registration request
|
||||||
if (!(ClientPass.ID | ClientPass.Key))
|
if (!IsLogged())
|
||||||
{
|
{
|
||||||
ClientPass = SourcePass;
|
ClientPass = SourcePass;
|
||||||
|
|
||||||
KCPUnit = MakeShared<FKCPWrap>(ClientPass.ID, GetName());
|
KCPUnit = MakeShared<FKCPWrap>(ClientPass.ID, FString::Printf(TEXT("Client-%i"), ClientPass.ID));
|
||||||
KCPUnit->SetTurboMode();
|
KCPUnit->SetTurboMode();
|
||||||
KCPUnit->GetKCPCB().logmask = KCPLogMask;
|
KCPUnit->GetKCPCB().logmask = KCPLogMask;
|
||||||
|
|
||||||
@ -180,7 +160,7 @@ void URSHWNetworkClient::TickComponent(float DeltaTime, ELevelTick TickType, FAc
|
|||||||
|
|
||||||
// handle timeout
|
// handle timeout
|
||||||
{
|
{
|
||||||
if ((ClientPass.ID | ClientPass.Key) && NowTime - LastRecvTime > TimeoutLimit)
|
if (IsLogged() && NowTime - LastRecvTime > TimeoutLimit)
|
||||||
{
|
{
|
||||||
ClientPass.ID = 0;
|
ClientPass.ID = 0;
|
||||||
ClientPass.Key = 0;
|
ClientPass.Key = 0;
|
||||||
@ -196,9 +176,8 @@ void URSHWNetworkClient::TickComponent(float DeltaTime, ELevelTick TickType, FAc
|
|||||||
|
|
||||||
void URSHWNetworkClient::Activate(bool bReset)
|
void URSHWNetworkClient::Activate(bool bReset)
|
||||||
{
|
{
|
||||||
if (!GetOwner()->GetGameInstance()) return;
|
|
||||||
if (bReset) Deactivate();
|
if (bReset) Deactivate();
|
||||||
if (!ShouldActivate()) return;
|
if (bIsActive) return;
|
||||||
|
|
||||||
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
|
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
|
||||||
|
|
||||||
@ -242,17 +221,14 @@ void URSHWNetworkClient::Activate(bool bReset)
|
|||||||
LastHeartbeat = FDateTime::MinValue();
|
LastHeartbeat = FDateTime::MinValue();
|
||||||
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Client activate."));
|
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Client activate."));
|
||||||
|
|
||||||
SetComponentTickEnabled(true);
|
bIsActive = true;
|
||||||
SetActiveFlag(true);
|
|
||||||
|
|
||||||
OnComponentActivated.Broadcast(this, bReset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void URSHWNetworkClient::Deactivate()
|
void URSHWNetworkClient::Deactivate()
|
||||||
{
|
{
|
||||||
if (ShouldActivate()) return;
|
if (!bIsActive) return;
|
||||||
|
|
||||||
if (ClientPass.ID | ClientPass.Key)
|
if (IsLogged())
|
||||||
{
|
{
|
||||||
OnUnlogin.Broadcast();
|
OnUnlogin.Broadcast();
|
||||||
}
|
}
|
||||||
@ -272,10 +248,7 @@ void URSHWNetworkClient::Deactivate()
|
|||||||
|
|
||||||
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Client deactivate."));
|
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Client deactivate."));
|
||||||
|
|
||||||
SetComponentTickEnabled(false);
|
bIsActive = false;
|
||||||
SetActiveFlag(false);
|
|
||||||
|
|
||||||
OnComponentDeactivated.Broadcast(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void URSHWNetworkClient::BeginDestroy()
|
void URSHWNetworkClient::BeginDestroy()
|
||||||
|
@ -7,12 +7,6 @@
|
|||||||
#include "SocketSubsystem.h"
|
#include "SocketSubsystem.h"
|
||||||
#include "HAL/UnrealMemory.h"
|
#include "HAL/UnrealMemory.h"
|
||||||
|
|
||||||
URSHWNetworkServer::URSHWNetworkServer(const FObjectInitializer& ObjectInitializer)
|
|
||||||
: Super(ObjectInitializer)
|
|
||||||
{
|
|
||||||
PrimaryComponentTick.bCanEverTick = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool URSHWNetworkServer::Send(int32 ClientID, const TArray<uint8>& Data)
|
bool URSHWNetworkServer::Send(int32 ClientID, const TArray<uint8>& Data)
|
||||||
{
|
{
|
||||||
if (!IsActive() || !Registration.Contains(ClientID)) return false;
|
if (!IsActive() || !Registration.Contains(ClientID)) return false;
|
||||||
@ -47,22 +41,8 @@ int32 URSHWNetworkServer::UDPSend(int32 ClientID, const uint8* Data, int32 Count
|
|||||||
return 0;
|
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;
|
if (!IsActive()) return;
|
||||||
|
|
||||||
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
|
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
|
||||||
@ -214,7 +194,7 @@ void URSHWNetworkServer::TickComponent(float DeltaTime, ELevelTick TickType, FAc
|
|||||||
NewRegistration.Heartbeat = FDateTime::MinValue();
|
NewRegistration.Heartbeat = FDateTime::MinValue();
|
||||||
NewRegistration.Addr = SourceAddr;
|
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->SetTurboMode();
|
||||||
NewRegistration.KCPUnit->GetKCPCB().logmask = KCPLogMask;
|
NewRegistration.KCPUnit->GetKCPCB().logmask = KCPLogMask;
|
||||||
|
|
||||||
@ -313,9 +293,8 @@ void URSHWNetworkServer::TickComponent(float DeltaTime, ELevelTick TickType, FAc
|
|||||||
|
|
||||||
void URSHWNetworkServer::Activate(bool bReset)
|
void URSHWNetworkServer::Activate(bool bReset)
|
||||||
{
|
{
|
||||||
if (!GetOwner()->GetGameInstance()) return;
|
|
||||||
if (bReset) Deactivate();
|
if (bReset) Deactivate();
|
||||||
if (!ShouldActivate()) return;
|
if (bIsActive) return;
|
||||||
|
|
||||||
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
|
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get();
|
||||||
|
|
||||||
@ -356,15 +335,12 @@ void URSHWNetworkServer::Activate(bool bReset)
|
|||||||
|
|
||||||
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Server activate."));
|
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Server activate."));
|
||||||
|
|
||||||
SetComponentTickEnabled(true);
|
bIsActive = true;
|
||||||
SetActiveFlag(true);
|
|
||||||
|
|
||||||
OnComponentActivated.Broadcast(this, bReset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void URSHWNetworkServer::Deactivate()
|
void URSHWNetworkServer::Deactivate()
|
||||||
{
|
{
|
||||||
if (ShouldActivate()) return;
|
if (!bIsActive) return;
|
||||||
|
|
||||||
TArray<int32> RegistrationAddr;
|
TArray<int32> RegistrationAddr;
|
||||||
Registration.GetKeys(RegistrationAddr);
|
Registration.GetKeys(RegistrationAddr);
|
||||||
@ -387,10 +363,7 @@ void URSHWNetworkServer::Deactivate()
|
|||||||
|
|
||||||
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Server deactivate."));
|
UE_LOG(LogRSHWNetwork, Log, TEXT("RSHW Network Server deactivate."));
|
||||||
|
|
||||||
SetComponentTickEnabled(false);
|
bIsActive = false;
|
||||||
SetActiveFlag(false);
|
|
||||||
|
|
||||||
OnComponentDeactivated.Broadcast(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void URSHWNetworkServer::BeginDestroy()
|
void URSHWNetworkServer::BeginDestroy()
|
||||||
|
@ -2,22 +2,18 @@
|
|||||||
|
|
||||||
#include "CoreMinimal.h"
|
#include "CoreMinimal.h"
|
||||||
#include "Misc/DateTime.h"
|
#include "Misc/DateTime.h"
|
||||||
|
#include "UObject/Object.h"
|
||||||
#include "RSHWNetworkType.h"
|
#include "RSHWNetworkType.h"
|
||||||
#include "Components/ActorComponent.h"
|
|
||||||
#include "RSHWNetworkClient.generated.h"
|
#include "RSHWNetworkClient.generated.h"
|
||||||
|
|
||||||
class FKCPWrap;
|
class FKCPWrap;
|
||||||
class FInternetAddr;
|
class FInternetAddr;
|
||||||
|
|
||||||
UCLASS(BlueprintType, hidecategories = ("Cooking", "ComponentReplication"), meta = (BlueprintSpawnableComponent))
|
UCLASS(BlueprintType)
|
||||||
class RSHWNETWORK_API URSHWNetworkClient : public UActorComponent
|
class RSHWNETWORK_API URSHWNetworkClient : public UObject, public FTickableGameObject
|
||||||
{
|
{
|
||||||
GENERATED_BODY()
|
GENERATED_BODY()
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
URSHWNetworkClient(const FObjectInitializer& ObjectInitializer);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE(FLoginSignature, URSHWNetworkClient, OnLogin);
|
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE(FLoginSignature, URSHWNetworkClient, OnLogin);
|
||||||
@ -37,6 +33,18 @@ public:
|
|||||||
|
|
||||||
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")
|
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
|
||||||
bool Send(const TArray<uint8>& Data);
|
bool Send(const TArray<uint8>& Data);
|
||||||
|
|
||||||
@ -56,6 +64,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
bool bIsActive = false;
|
||||||
|
|
||||||
TSharedPtr<FInternetAddr> ServerAddrPtr;
|
TSharedPtr<FInternetAddr> ServerAddrPtr;
|
||||||
|
|
||||||
FSocket* SocketPtr;
|
FSocket* SocketPtr;
|
||||||
@ -75,13 +85,11 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
//~ Begin UActorComponent Interface
|
//~ Begin FTickableGameObject Interface
|
||||||
virtual void BeginPlay() override;
|
virtual void Tick(float DeltaTime) override;
|
||||||
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
|
virtual bool IsTickable() const override { return !IsTemplate() && IsActive(); }
|
||||||
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
|
virtual TStatId GetStatId() const override { return GetStatID(); }
|
||||||
virtual void Activate(bool bReset = false) override;
|
//~ End FTickableGameObject Interface
|
||||||
virtual void Deactivate() override;
|
|
||||||
//~ End UActorComponent Interface
|
|
||||||
|
|
||||||
//~ Begin UObject Interface
|
//~ Begin UObject Interface
|
||||||
virtual void BeginDestroy() override;
|
virtual void BeginDestroy() override;
|
||||||
|
@ -2,22 +2,18 @@
|
|||||||
|
|
||||||
#include "CoreMinimal.h"
|
#include "CoreMinimal.h"
|
||||||
#include "Misc/DateTime.h"
|
#include "Misc/DateTime.h"
|
||||||
|
#include "UObject/Object.h"
|
||||||
#include "RSHWNetworkType.h"
|
#include "RSHWNetworkType.h"
|
||||||
#include "Components/ActorComponent.h"
|
|
||||||
#include "RSHWNetworkServer.generated.h"
|
#include "RSHWNetworkServer.generated.h"
|
||||||
|
|
||||||
class FSocket;
|
class FSocket;
|
||||||
class FKCPWrap;
|
class FKCPWrap;
|
||||||
|
|
||||||
UCLASS(BlueprintType, hidecategories = ("Cooking", "ComponentReplication"), meta = (BlueprintSpawnableComponent))
|
UCLASS(BlueprintType)
|
||||||
class RSHWNETWORK_API URSHWNetworkServer : public UActorComponent
|
class RSHWNETWORK_API URSHWNetworkServer : public UObject, public FTickableGameObject
|
||||||
{
|
{
|
||||||
GENERATED_BODY()
|
GENERATED_BODY()
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
URSHWNetworkServer(const FObjectInitializer& ObjectInitializer);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_OneParam(FLoginSignature, URSHWNetworkServer, OnLogin, int32, ClientID);
|
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_OneParam(FLoginSignature, URSHWNetworkServer, OnLogin, int32, ClientID);
|
||||||
@ -37,6 +33,15 @@ public:
|
|||||||
|
|
||||||
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")
|
UFUNCTION(BlueprintCallable, Category = "RSHW|Network")
|
||||||
bool Send(int32 ClientID, const TArray<uint8>& Data);
|
bool Send(int32 ClientID, const TArray<uint8>& Data);
|
||||||
|
|
||||||
@ -56,6 +61,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
bool bIsActive = false;
|
||||||
|
|
||||||
FSocket* SocketPtr;
|
FSocket* SocketPtr;
|
||||||
|
|
||||||
TArray<uint8> SendBuffer;
|
TArray<uint8> SendBuffer;
|
||||||
@ -87,13 +94,11 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
//~ Begin UActorComponent Interface
|
//~ Begin FTickableGameObject Interface
|
||||||
virtual void BeginPlay() override;
|
virtual void Tick(float DeltaTime) override;
|
||||||
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
|
virtual bool IsTickable() const override { return !IsTemplate() && IsActive(); }
|
||||||
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
|
virtual TStatId GetStatId() const override { return GetStatID(); }
|
||||||
virtual void Activate(bool bReset = false) override;
|
//~ End FTickableGameObject Interface
|
||||||
virtual void Deactivate() override;
|
|
||||||
//~ End UActorComponent Interface
|
|
||||||
|
|
||||||
//~ Begin UObject Interface
|
//~ Begin UObject Interface
|
||||||
virtual void BeginDestroy() override;
|
virtual void BeginDestroy() override;
|
||||||
|
Reference in New Issue
Block a user