Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "Connection/DbConnectionBase.h"
#include "Connection/DbConnectionBuilder.h"
#include "Connection/DbConnectionBuilder.h"
#include "Connection/Credentials.h"
#include "Connection/LogCategory.h"
#include "Containers/Ticker.h"
#include "ModuleBindings/Types/ClientMessageType.g.h"
#include "ModuleBindings/Types/SubscriptionErrorType.g.h"
#include "Misc/Compression.h"
Expand Down Expand Up @@ -65,13 +66,48 @@ static FString DecodeReducerErrorMessage(const TArray<uint8>& ErrorBytes)
}
}

UDbConnectionBase::UDbConnectionBase(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
NextRequestId = 1;
NextSubscriptionId = 1;
ProcedureCallbacks = CreateDefaultSubobject<UProcedureCallbacks>(TEXT("ProcedureCallbacks"));
}
UDbConnectionBase::UDbConnectionBase(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
NextRequestId = 1;
NextSubscriptionId = 1;
ProcedureCallbacks = CreateDefaultSubobject<UProcedureCallbacks>(TEXT("ProcedureCallbacks"));
}

void UDbConnectionBase::SetAutoTicking(bool bAutoTick)
{
if (bIsAutoTicking == bAutoTick)
{
return;
}

bIsAutoTicking = bAutoTick;

if (bIsAutoTicking)
{
if (!TickerHandle.IsValid())
{
TickerHandle = FTSTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateUObject(this, &UDbConnectionBase::OnTickerTick));
}
}
else if (TickerHandle.IsValid())
{
FTSTicker::GetCoreTicker().RemoveTicker(TickerHandle);
TickerHandle.Reset();
}
}

void UDbConnectionBase::BeginDestroy()
{
if (TickerHandle.IsValid())
{
FTSTicker::GetCoreTicker().RemoveTicker(TickerHandle);
TickerHandle.Reset();
}
bIsAutoTicking = false;

Super::BeginDestroy();
}

void UDbConnectionBase::Disconnect()
{
Expand Down Expand Up @@ -216,8 +252,8 @@ void UDbConnectionBase::HandleWSBinaryMessage(const TArray<uint8>& Message)
});
}

void UDbConnectionBase::FrameTick()
{
void UDbConnectionBase::FrameTick()
{
TArray<FServerMessageType> Local;
{
FScopeLock Lock(&PendingMessagesMutex);
Expand All @@ -236,31 +272,19 @@ void UDbConnectionBase::FrameTick()
{
//process the message, this will call DbUpdate or trigger subscription events as needed
ProcessServerMessage(Msg);
}
}
void UDbConnectionBase::Tick(float DeltaTime)
{
if (bIsAutoTicking)
{
FrameTick();
}
}

TStatId UDbConnectionBase::GetStatId() const
{
// This is used by the engine to track tickables, we return a unique stat ID for this class
RETURN_QUICK_DECLARE_CYCLE_STAT(UMyTickableObject, STATGROUP_Tickables);
}

bool UDbConnectionBase::IsTickable() const
{
return bIsAutoTicking;
}

bool UDbConnectionBase::IsTickableInEditor() const
{
return bIsAutoTicking;
}
}
}

bool UDbConnectionBase::OnTickerTick(float DeltaTime)
{
if (HasAnyFlags(RF_BeginDestroyed | RF_FinishDestroyed) || !bIsAutoTicking)
{
return false;
}

FrameTick();
return true;
}


void UDbConnectionBase::ProcessServerMessage(const FServerMessageType& Message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "CoreMinimal.h"
#include "Containers/Ticker.h"
#include "UObject/NoExportTypes.h"
#include "Types/Builtins.h"
#include "Websocket.h"
#include "Subscription.h"
Expand Down Expand Up @@ -114,8 +115,8 @@ struct THasOnUpdateDelegate<T, std::void_t<decltype(&T::OnUpdate)>> : std::true_
{
};

UCLASS()
class SPACETIMEDBSDK_API UDbConnectionBase : public UObject, public FTickableGameObject
UCLASS()
class SPACETIMEDBSDK_API UDbConnectionBase : public UObject
{
GENERATED_BODY()

Expand All @@ -135,8 +136,8 @@ class SPACETIMEDBSDK_API UDbConnectionBase : public UObject, public FTickableGam
UFUNCTION(BlueprintCallable, Category="SpacetimeDB")
void FrameTick();

UFUNCTION(BlueprintCallable, Category="SpacetimeDB")
void SetAutoTicking(bool bAutoTick) { bIsAutoTicking = bAutoTick; }
UFUNCTION(BlueprintCallable, Category="SpacetimeDB")
void SetAutoTicking(bool bAutoTick);

/** Send a raw JSON message to the server. */
bool SendRawMessage(const FString& Message);
Expand Down Expand Up @@ -257,9 +258,11 @@ class SPACETIMEDBSDK_API UDbConnectionBase : public UObject, public FTickableGam
}


protected:
protected:

friend class UDbConnectionBuilderBase;
virtual void BeginDestroy() override;

friend class UDbConnectionBuilderBase;
friend class UDbConnectionBuilder;
friend class USubscriptionHandleBase;
friend class USubscriptionBuilder;
Expand All @@ -276,13 +279,7 @@ class SPACETIMEDBSDK_API UDbConnectionBase : public UObject, public FTickableGam
UFUNCTION()
void HandleWSBinaryMessage(const TArray<uint8>& Message);

virtual void Tick(float DeltaTime) override;

virtual TStatId GetStatId() const override;

virtual bool IsTickable() const override;

virtual bool IsTickableInEditor() const override;
bool OnTickerTick(float DeltaTime);

/** Internal handler that processes a single server message. */
void ProcessServerMessage(const FServerMessageType& Message);
Expand Down Expand Up @@ -407,6 +404,7 @@ class SPACETIMEDBSDK_API UDbConnectionBase : public UObject, public FTickableGam

UPROPERTY()
bool bIsAutoTicking = false;
FTSTicker::FDelegateHandle TickerHandle;
/** Guard to avoid repeatedly handling the same fatal protocol error. */
FThreadSafeBool bProtocolViolationHandled = false;

Expand Down
Loading