Skip to content

Commit

Permalink
Fix issues with GetRegionalPing (#14)
Browse files Browse the repository at this point in the history
* fix issues caught by Linux compiler

* ensure the UHathoraSDK instance doesn't get GC during GetRegionalPings
  • Loading branch information
mikeseese authored Nov 3, 2023
1 parent 28360e8 commit 0b15f30
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
void UHathoraSDK::GetRegionalPings(const FHathoraOnGetRegionalPings& OnComplete, int32 NumPingsPerRegion)
{
UHathoraSDK* SDK = UHathoraSDK::CreateHathoraSDK("", FHathoraSDKSecurity());
SDK->DiscoveryV1->GetRegionalPings(OnComplete, NumPingsPerRegion);
SDK->AddToRoot(); // make sure this doesn't get garbage collected
SDK->OnGetRegionalPingsComplete = OnComplete;
FHathoraOnGetRegionalPings WrapperCallback;
WrapperCallback.BindDynamic(SDK, &UHathoraSDK::OnGetRegionalPingsCompleteWrapper);
SDK->DiscoveryV1->GetRegionalPings(WrapperCallback, NumPingsPerRegion);
}

UHathoraSDK* UHathoraSDK::CreateHathoraSDK(FString AppId, FHathoraSDKSecurity Security)
Expand All @@ -27,3 +31,9 @@ void UHathoraSDK::SetCredentials(FString AppId, FHathoraSDKSecurity Security)
{
DiscoveryV1->SetCredentials(AppId, Security);
}

void UHathoraSDK::OnGetRegionalPingsCompleteWrapper(FHathoraRegionPings Result)
{
OnGetRegionalPingsComplete.ExecuteIfBound(Result);
RemoveFromRoot(); // Allow this SDK reference to be garbage collected
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

void UHathoraSDKAPI::SetCredentials(FString InAppId, FHathoraSDKSecurity InSecurity)
{
this->AppId = AppId;
this->Security = Security;
this->AppId = InAppId;
this->Security = InSecurity;
}

void UHathoraSDKAPI::SendRequest(FString Method, FString Endpoint, TFunction<void(FHttpRequestPtr, FHttpResponsePtr, bool)> OnComplete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ void UHathoraSDKDiscoveryV1::GetPingServiceEndpoints(const FHathoraOnGetPingServ
SendRequest(
TEXT("GET"),
TEXT("/discovery/v1/ping"),
[&, OnComplete](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bSuccess) mutable
[OnComplete](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bSuccess) mutable
{
TArray<FHathoraDiscoveredPingEndpoint> PingEndpoints;
TArray<FHathoraDiscoveredPingEndpoint> PingEndpointsResult;
if (bSuccess && Response.IsValid())
{
FJsonObjectConverter::JsonArrayStringToUStruct(Response->GetContentAsString(), &PingEndpoints);
FJsonObjectConverter::JsonArrayStringToUStruct(Response->GetContentAsString(), &PingEndpointsResult);
}
else
{
UE_LOG(LogHathoraSDK, Warning, TEXT("Could not retrieve ping endpoints"));
}

if (!OnComplete.ExecuteIfBound(PingEndpoints))
if (!OnComplete.ExecuteIfBound(PingEndpointsResult))
{
UE_LOG(LogHathoraSDK, Warning, TEXT("[GetRegionalPings] function pointer was not valid, so OnComplete will not be called"));
UE_LOG(LogHathoraSDK, Warning, TEXT("[GetPingServiceEndpoints] function pointer was not valid, so OnComplete will not be called"));
}
});
}
Expand Down Expand Up @@ -94,7 +94,7 @@ void UHathoraSDKDiscoveryV1::PingEachRegion()
FIcmp::IcmpEcho(
PingEndpoint.Host,
GetDefault<UHathoraSDKConfig>()->GetPingTimeoutSeconds(),
[&, PingEndpoint, CompletedPings, PingsToComplete](FIcmpEchoResult Result)
[this, PingEndpoint, CompletedPings, PingsToComplete](FIcmpEchoResult Result)
{
if (Result.Status == EIcmpResponseStatus::Success)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ class HATHORASDK_API UHathoraSDK : public UBlueprintFunctionLibrary

UPROPERTY(BlueprintReadOnly)
UHathoraSDKDiscoveryV1* DiscoveryV1;

private:
FHathoraOnGetRegionalPings OnGetRegionalPingsComplete;

UFUNCTION()
void OnGetRegionalPingsCompleteWrapper(FHathoraRegionPings Result);
};

0 comments on commit 0b15f30

Please sign in to comment.