Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
IsListening and ownership fix (#1246)
Browse files Browse the repository at this point in the history
* Initial commit

* Cleaned outdated code

* Iterate through an Actor's children when its ownership changes.

* Update SpatialActorChannel.h

* Update SpatialActorChannel.cpp

* Changed Schema_GetBool -> GetBoolFromSchema

* Update SpatialGDK/Source/SpatialGDK/Public/Schema/ServerRPCEndpoint.h

Co-Authored-By: Michael Samiec <michaelsamiec@improbable.io>

* Use 0 NetGuid for Actors that are not replicated
  • Loading branch information
aleximprobable authored and m-samiec committed Aug 7, 2019
1 parent 6bc4fa6 commit 6fa4f79
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#include "EngineClasses/SpatialNetConnection.h"
#include "EngineClasses/SpatialNetDriver.h"
#include "EngineClasses/SpatialPackageMapClient.h"
#include "Interop/SpatialSender.h"
#include "Interop/SpatialReceiver.h"
#include "Interop/GlobalStateManager.h"
#include "Interop/SpatialReceiver.h"
#include "Interop/SpatialSender.h"
#include "Schema/ClientRPCEndpoint.h"
#include "Schema/ServerRPCEndpoint.h"
#include "SpatialConstants.h"
#include "SpatialGDKSettings.h"
#include "Utils/RepLayoutUtils.h"
Expand Down Expand Up @@ -77,7 +79,6 @@ USpatialActorChannel::USpatialActorChannel(const FObjectInitializer& ObjectIniti
, bCreatingNewEntity(false)
, EntityId(SpatialConstants::INVALID_ENTITY_ID)
, bInterestDirty(false)
, bIsListening(false)
, bNetOwned(false)
, NetDriver(nullptr)
, LastPositionSinceUpdate(FVector::ZeroVector)
Expand Down Expand Up @@ -549,6 +550,26 @@ void USpatialActorChannel::DynamicallyAttachSubobject(UObject* Object)
}
}

bool USpatialActorChannel::IsListening() const
{
if (NetDriver->IsServer())
{
if (SpatialGDK::ClientRPCEndpoint* Endpoint = NetDriver->StaticComponentView->GetComponentData<SpatialGDK::ClientRPCEndpoint>(EntityId))
{
return Endpoint->bReady;
}
}
else
{
if (SpatialGDK::ServerRPCEndpoint* Endpoint = NetDriver->StaticComponentView->GetComponentData<SpatialGDK::ServerRPCEndpoint>(EntityId))
{
return Endpoint->bReady;
}
}

return false;
}

const FClassInfo* USpatialActorChannel::TryResolveNewDynamicSubobjectAndGetClassInfo(UObject* Object)
{
const FClassInfo* Info = nullptr;
Expand Down Expand Up @@ -1014,6 +1035,21 @@ void USpatialActorChannel::ServerProcessOwnershipChange()
return;
}

UpdateEntityACLToNewOwner();

for (AActor* Child : Actor->Children)
{
Worker_EntityId ChildEntityId = NetDriver->PackageMap->GetEntityIdFromObject(Child);

if (USpatialActorChannel* Channel = NetDriver->GetActorChannelByEntityId(ChildEntityId))
{
Channel->ServerProcessOwnershipChange();
}
}
}

void USpatialActorChannel::UpdateEntityACLToNewOwner()
{
FString NewOwnerWorkerAttribute = SpatialGDK::GetOwnerWorkerAttribute(Actor);

if (SavedOwnerWorkerAttribute != NewOwnerWorkerAttribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ FNetworkGUID USpatialPackageMapClient::TryResolveObjectAsEntity(UObject* Value)
}

AActor* Actor = Value->IsA<AActor>() ? Cast<AActor>(Value) : Cast<AActor>(Value->GetOuter());
if (!Actor->GetIsReplicated())
{
return NetGUID;
}

if (Actor->GetClass()->HasAnySpatialClassFlags(SPATIALCLASS_Singleton))
{
Expand Down
33 changes: 2 additions & 31 deletions SpatialGDK/Source/SpatialGDK/Private/Interop/SpatialReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ void USpatialReceiver::OnAddComponent(const Worker_AddComponentOp& Op)
case SpatialConstants::RPCS_ON_ENTITY_CREATION_ID:
case SpatialConstants::DEBUG_METRICS_COMPONENT_ID:
case SpatialConstants::ALWAYS_RELEVANT_COMPONENT_ID:
case SpatialConstants::CLIENT_RPC_ENDPOINT_COMPONENT_ID:
case SpatialConstants::SERVER_RPC_ENDPOINT_COMPONENT_ID:
// Ignore static spatial components as they are managed by the SpatialStaticComponentView.
return;
case SpatialConstants::SINGLETON_MANAGER_COMPONENT_ID:
Expand All @@ -131,11 +133,6 @@ void USpatialReceiver::OnAddComponent(const Worker_AddComponentOp& Op)
case SpatialConstants::STARTUP_ACTOR_MANAGER_COMPONENT_ID:
GlobalStateManager->ApplyStartupActorManagerData(Op.data);
return;
case SpatialConstants::CLIENT_RPC_ENDPOINT_COMPONENT_ID:
case SpatialConstants::SERVER_RPC_ENDPOINT_COMPONENT_ID:
Schema_Object* FieldsObject = Schema_GetComponentDataFields(Op.data.schema_type);
RegisterListeningEntityIfReady(Op.entity_id, FieldsObject);
return;
}

if (ClassInfoManager->IsSublevelComponent(Op.data.component_id))
Expand Down Expand Up @@ -1058,13 +1055,6 @@ void USpatialReceiver::ApplyComponentData(UObject* TargetObject, USpatialActorCh

void USpatialReceiver::OnComponentUpdate(const Worker_ComponentUpdateOp& Op)
{
if (Op.update.component_id == SpatialConstants::SERVER_RPC_ENDPOINT_COMPONENT_ID ||
Op.update.component_id == SpatialConstants::CLIENT_RPC_ENDPOINT_COMPONENT_ID)
{
Schema_Object* FieldsObject = Schema_GetComponentUpdateFields(Op.update.schema_type);
RegisterListeningEntityIfReady(Op.entity_id, FieldsObject);
}

if (StaticComponentView->GetAuthority(Op.entity_id, Op.update.component_id) == WORKER_AUTHORITY_AUTHORITATIVE)
{
UE_LOG(LogSpatialReceiver, Verbose, TEXT("Entity: %d Component: %d - Skipping update because this was short circuited"), Op.entity_id, Op.update.component_id);
Expand Down Expand Up @@ -1451,25 +1441,6 @@ void USpatialReceiver::ApplyComponentUpdate(const Worker_ComponentUpdate& Compon
QueueIncomingRepUpdates(ChannelObjectPair, ObjectReferencesMap, UnresolvedRefs);
}

void USpatialReceiver::RegisterListeningEntityIfReady(Worker_EntityId EntityId, Schema_Object* Object)
{
if (Schema_GetBoolCount(Object, SpatialConstants::UNREAL_RPC_ENDPOINT_READY_ID) > 0)
{
bool bReady = GetBoolFromSchema(Object, SpatialConstants::UNREAL_RPC_ENDPOINT_READY_ID);
if (bReady)
{
if (USpatialActorChannel* Channel = NetDriver->GetActorChannelByEntityId(EntityId))
{
Channel->StartListening();
if (UObject* TargetObject = Channel->GetActor())
{
Sender->SendOutgoingRPCs();
}
}
}
}
}

bool USpatialReceiver::ApplyRPC(UObject* TargetObject, UFunction* Function, const RPCPayload& Payload, const FString& SenderWorkerId)
{
bool bApplied = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include "Interop/SpatialStaticComponentView.h"

#include "Schema/ClientRPCEndpoint.h"
#include "Schema/Component.h"
#include "Schema/Heartbeat.h"
#include "Schema/Interest.h"
#include "Schema/RPCPayload.h"
#include "Schema/ServerRPCEndpoint.h"
#include "Schema/Singleton.h"
#include "Schema/SpawnData.h"

Expand Down Expand Up @@ -73,6 +75,12 @@ void USpatialStaticComponentView::OnAddComponent(const Worker_AddComponentOp& Op
case SpatialConstants::RPCS_ON_ENTITY_CREATION_ID:
Data = MakeUnique<SpatialGDK::ComponentStorage<SpatialGDK::RPCsOnEntityCreation>>(Op.data);
break;
case SpatialConstants::CLIENT_RPC_ENDPOINT_COMPONENT_ID:
Data = MakeUnique<SpatialGDK::ComponentStorage<SpatialGDK::ClientRPCEndpoint>>(Op.data);
break;
case SpatialConstants::SERVER_RPC_ENDPOINT_COMPONENT_ID:
Data = MakeUnique<SpatialGDK::ComponentStorage<SpatialGDK::ServerRPCEndpoint>>(Op.data);
break;
default:
// Component is not hand written, but we still want to know the existence of it on this entity.
Data = nullptr;
Expand Down Expand Up @@ -111,6 +119,12 @@ void USpatialStaticComponentView::OnComponentUpdate(const Worker_ComponentUpdate
case SpatialConstants::POSITION_COMPONENT_ID:
Component = GetComponentData<SpatialGDK::Position>(Op.entity_id);
break;
case SpatialConstants::CLIENT_RPC_ENDPOINT_COMPONENT_ID:
Component = GetComponentData<SpatialGDK::ClientRPCEndpoint>(Op.entity_id);
break;
case SpatialConstants::SERVER_RPC_ENDPOINT_COMPONENT_ID:
Component = GetComponentData<SpatialGDK::ServerRPCEndpoint>(Op.entity_id);
break;
default:
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ class SPATIALGDK_API USpatialActorChannel : public UActorChannel
FORCEINLINE void MarkInterestDirty() { bInterestDirty = true; }
FORCEINLINE bool GetInterestDirty() const { return bInterestDirty; }

FORCEINLINE void StartListening() { bIsListening = true; }
FORCEINLINE bool IsListening() { return bIsListening; }
bool IsListening() const;
const FClassInfo* TryResolveNewDynamicSubobjectAndGetClassInfo(UObject* Object);

protected:
Expand All @@ -174,6 +173,8 @@ class SPATIALGDK_API USpatialActorChannel : public UActorChannel

void InitializeHandoverShadowData(TArray<uint8>& ShadowData, UObject* Object);
FHandoverChangeState GetHandoverChangeList(TArray<uint8>& ShadowData, UObject* Object);

void UpdateEntityACLToNewOwner();

public:
// If this actor channel is responsible for creating a new entity, this will be set to true once the entity is created.
Expand All @@ -187,7 +188,6 @@ class SPATIALGDK_API USpatialActorChannel : public UActorChannel
private:
Worker_EntityId EntityId;
bool bInterestDirty;
bool bIsListening;

// Used on the client to track gaining/losing ownership.
bool bNetOwned;
Expand Down
2 changes: 0 additions & 2 deletions SpatialGDK/Source/SpatialGDK/Public/Interop/SpatialReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ class USpatialReceiver : public UObject

void ApplyComponentUpdate(const Worker_ComponentUpdate& ComponentUpdate, UObject* TargetObject, USpatialActorChannel* Channel, bool bIsHandover);

void RegisterListeningEntityIfReady(Worker_EntityId EntityId, Schema_Object* Object);

bool ApplyRPC(const FPendingRPCParams& Params);
bool ApplyRPC(UObject* TargetObject, UFunction* Function, const SpatialGDK::RPCPayload& Payload, const FString& SenderWorkerId);

Expand Down
15 changes: 15 additions & 0 deletions SpatialGDK/Source/SpatialGDK/Public/Schema/ClientRPCEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ struct ClientRPCEndpoint : Component

ClientRPCEndpoint() = default;

ClientRPCEndpoint(const Worker_ComponentData& Data)
{
Schema_Object* EndpointObject = Schema_GetComponentDataFields(Data.schema_type);
bReady = GetBoolFromSchema(EndpointObject, SpatialConstants::UNREAL_RPC_ENDPOINT_READY_ID);
}

void ApplyComponentUpdate(const Worker_ComponentUpdate& Update)
{
Schema_Object* EndpointObject = Schema_GetComponentUpdateFields(Update.schema_type);
if (Schema_GetBoolCount(EndpointObject, SpatialConstants::UNREAL_RPC_ENDPOINT_READY_ID) > 0)
{
bReady = GetBoolFromSchema(EndpointObject, SpatialConstants::UNREAL_RPC_ENDPOINT_READY_ID);
}
}

Worker_ComponentData CreateRPCEndpointData()
{
Worker_ComponentData Data{};
Expand Down
15 changes: 15 additions & 0 deletions SpatialGDK/Source/SpatialGDK/Public/Schema/ServerRPCEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ struct ServerRPCEndpoint : Component

ServerRPCEndpoint() = default;

ServerRPCEndpoint(const Worker_ComponentData& Data)
{
Schema_Object* EndpointObject = Schema_GetComponentDataFields(Data.schema_type);
bReady = GetBoolFromSchema(EndpointObject, SpatialConstants::UNREAL_RPC_ENDPOINT_READY_ID);
}

void ApplyComponentUpdate(const Worker_ComponentUpdate& Update)
{
Schema_Object* EndpointObject = Schema_GetComponentUpdateFields(Update.schema_type);
if (Schema_GetBoolCount(EndpointObject, SpatialConstants::UNREAL_RPC_ENDPOINT_READY_ID) > 0)
{
bReady = GetBoolFromSchema(EndpointObject, SpatialConstants::UNREAL_RPC_ENDPOINT_READY_ID);
}
}

Worker_ComponentData CreateRPCEndpointData()
{
Worker_ComponentData Data{};
Expand Down

0 comments on commit 6fa4f79

Please sign in to comment.