Skip to content

Commit d3c4bb0

Browse files
committed
improvements to localhost listener
1 parent 5f51499 commit d3c4bb0

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

Plugins/SequencePlugin/Source/SequencePlugin/Private/EcosystemWallet/Authentication/LocalhostListener.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@ void ULocalhostListener::WaitForResponse(TSuccessCallback<FString> OnSuccess, FF
4747
return;
4848
}
4949

50+
TWeakObjectPtr<ULocalhostListener> WeakThis(this);
5051

5152
const FHttpPath RootPath(TEXT("/api"));
5253
RouteHandle = Router->BindRoute(
5354
RootPath,
5455
EHttpServerRequestVerbs::VERB_GET,
55-
FHttpRequestHandler::CreateLambda([this](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) -> bool
56+
FHttpRequestHandler::CreateLambda([WeakThis](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) -> bool
5657
{
57-
return this->HandleAnyRequest(Request, OnComplete);
58+
if (!WeakThis.IsValid())
59+
{
60+
return false;
61+
}
62+
63+
return WeakThis->HandleAnyRequest(Request, OnComplete);
5864
})
5965
);
6066

@@ -85,29 +91,42 @@ bool ULocalhostListener::HandleAnyRequest(const FHttpServerRequest& Request, con
8591

8692
if (Request.QueryParams.Contains("error"))
8793
{
88-
if (CurrentOnFailure.IsValid() && *CurrentOnFailure)
89-
{
90-
(*CurrentOnFailure)(FSequenceError(EErrorType::InvalidArgument, Request.QueryParams["error"]));
91-
}
92-
else
93-
{
94-
UE_LOG(LogTemp, Error, TEXT("CurrentOnFailure is invalid"));
95-
}
94+
if (TSharedPtr<FFailureCallback> Failure = CurrentOnFailure.Pin())
95+
{
96+
if (*Failure)
97+
{
98+
const FString* Error = Request.QueryParams.Find("error");
99+
(*Failure)(FSequenceError(EErrorType::InvalidArgument, *Error));
100+
}
101+
}
102+
else
103+
{
104+
UE_LOG(LogTemp, Warning, TEXT("Failure callback expired"));
105+
}
96106
}
97107
else
98108
{
99-
const FString EncodedPayload = Request.QueryParams["payload"];
109+
const FString* EncodedPayload = Request.QueryParams.Find("payload");
110+
if (!EncodedPayload)
111+
{
112+
UE_LOG(LogTemp, Warning, TEXT("Missing payload param"));
113+
return false;
114+
}
115+
100116
const FString PayloadJson = USequenceSupport::DecodeBase64ToString(EncodedPayload);
101117

102118
UE_LOG(LogTemp, Log, TEXT("Response Payload Json %s"), *PayloadJson);
103119

104-
if (CurrentOnSuccess.IsValid() && *CurrentOnSuccess)
120+
if (TSharedPtr<TSuccessCallback<FString>> Success = CurrentOnSuccess.Pin())
105121
{
106-
(*CurrentOnSuccess)(PayloadJson);
122+
if (*Success)
123+
{
124+
(*Success)(PayloadJson);
125+
}
107126
}
108127
else
109128
{
110-
UE_LOG(LogTemp, Error, TEXT("CurrentOnSuccess is invalid"));
129+
UE_LOG(LogTemp, Warning, TEXT("Success callback expired"));
111130
}
112131
}
113132

Plugins/SequencePlugin/Source/SequencePlugin/Private/EcosystemWallet/Authentication/LocalhostListener.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ class SEQUENCEPLUGIN_API ULocalhostListener : public UObject
3232
uint32 FallbackPort = 4445;
3333
bool bServerStarted = false;
3434

35-
TSharedPtr<TSuccessCallback<FString>> CurrentOnSuccess;
36-
TSharedPtr<FFailureCallback> CurrentOnFailure;
35+
TWeakPtr<TSuccessCallback<FString>> CurrentOnSuccess;
36+
TWeakPtr<FFailureCallback> CurrentOnFailure;
3737
};

0 commit comments

Comments
 (0)