Skip to content

Commit b8e8725

Browse files
Fix Auth Token - manage scope for provided token. Remove PublishMessage error - it should be handled by result callback.
1 parent 76358b8 commit b8e8725

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

Source/PubnubLibrary/Private/PubnubSubsystem.cpp

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void UPubnubSubsystem::DeinitPubnub()
8585
ChannelSubscriptions.Empty();
8686
ChannelGroupSubscriptions.Empty();
8787
IsUserIDSet = false;
88+
delete[] AuthTokenBuffer;
8889
}
8990

9091
void UPubnubSubsystem::SetUserID(FString UserID)
@@ -523,12 +524,16 @@ void UPubnubSubsystem::SetAuthToken(FString Token)
523524
if(!CheckIsUserIDSet())
524525
{return;}
525526

526-
if(CheckIsFieldEmpty(Token, "Token", "SetAuthToken"))
527-
{return;}
528-
529-
FUTF8StringHolder TokenHolder(Token);
527+
//Auth token has to be kept alive for the lifetime of the sdk, so we copy it into AuthTokenBuffer
528+
FTCHARToUTF8 Converter(*Token);
529+
AuthTokenLength = Converter.Length();
530+
delete[] AuthTokenBuffer;
531+
AuthTokenBuffer = new char[AuthTokenLength + 1];
532+
FMemory::Memcpy(AuthTokenBuffer, Converter.Get(), AuthTokenLength);
533+
AuthTokenBuffer[AuthTokenLength] = '\0';
534+
530535
//This is just a setter, so no need to call it on a separate thread
531-
pubnub_set_auth_token(ctx_pub, TokenHolder.Get());
536+
pubnub_set_auth_token(ctx_pub, AuthTokenBuffer);
532537
}
533538

534539
void UPubnubSubsystem::FetchHistory(FString Channel, FOnFetchHistoryResponse OnFetchHistoryResponse, FPubnubFetchHistorySettings FetchHistorySettings)
@@ -1404,29 +1409,6 @@ void UPubnubSubsystem::PubnubResponseError(pubnub_res PubnubResponse, FString Er
14041409
});
14051410
}
14061411

1407-
void UPubnubSubsystem::PubnubPublishError()
1408-
{
1409-
FString FinalErrorMessage;
1410-
if(ctx_pub == nullptr)
1411-
{
1412-
FinalErrorMessage = "Can't publish message. Publish context is invalid";
1413-
}
1414-
1415-
FString PublishError(pubnub_last_publish_result(ctx_pub));
1416-
FinalErrorMessage = FString::Printf(TEXT("Publish message failed. Error: %s."), *PublishError);
1417-
1418-
//Log and broadcast error
1419-
UE_LOG(PubnubLog, Error, TEXT("%s"), *FinalErrorMessage);
1420-
1421-
//Errors has to be broadcasted on GameThread, otherwise engine will crash if someone uses them for example with widgets
1422-
AsyncTask(ENamedThreads::GameThread, [this, FinalErrorMessage]()
1423-
{
1424-
//Broadcast bound delegate with JsonResponse
1425-
OnPubnubError.Broadcast(FinalErrorMessage, EPubnubErrorType::PET_Error);
1426-
OnPubnubErrorNative.Broadcast(FinalErrorMessage, EPubnubErrorType::PET_Error);
1427-
});
1428-
}
1429-
14301412
void UPubnubSubsystem::LoadPluginSettings()
14311413
{
14321414
//Save all settings
@@ -1629,10 +1611,6 @@ void UPubnubSubsystem::PublishMessage_priv(FString Channel, FString Message, FOn
16291611
PublishedMessage.MessageType = EPubnubMessageType::PMT_Published;
16301612
PublishedMessage.CustomMessageType = PublishSettings.CustomMessageType;
16311613
}
1632-
else
1633-
{
1634-
PubnubPublishError();
1635-
}
16361614

16371615
//Delegate needs to be executed back on Game Thread
16381616
AsyncTask(ENamedThreads::GameThread, [this, OnPublishMessageResponse, PublishResult, PublishedMessage]()
@@ -1683,10 +1661,6 @@ void UPubnubSubsystem::Signal_priv(FString Channel, FString Message, FOnSignalRe
16831661
SignalMessage.MessageType = EPubnubMessageType::PMT_Signal;
16841662
SignalMessage.CustomMessageType = SignalSettings.CustomMessageType;
16851663
}
1686-
else
1687-
{
1688-
PubnubPublishError();
1689-
}
16901664

16911665
//Delegate needs to be executed back on Game Thread
16921666
AsyncTask(ENamedThreads::GameThread, [this, OnSignalResponse, PublishResult, SignalMessage]()

Source/PubnubLibrary/Public/PubnubSubsystem.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ DECLARE_LOG_CATEGORY_EXTERN(PubnubLog, Log, All);
1414
constexpr int PUBNUB_MAX_LIMIT = 100;
1515

1616
class FJsonObject;
17-
1817
class UPubnubSettings;
1918
class FPubnubFunctionThread;
2019
class UPubnubChatSystem;
2120

22-
2321
struct CCoreSubscriptionData
2422
{
2523
pubnub_subscribe_message_callback_t Callback;
@@ -1771,6 +1769,10 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
17711769
pubnub_t *ctx_pub = nullptr;
17721770
//Pubnub context for the event engine - subscribe operations
17731771
pubnub_t *ctx_ee = nullptr;
1772+
1773+
//Auth token has to be kept alive for the lifetime of the sdk, so this is the container for it
1774+
char* AuthTokenBuffer = nullptr;
1775+
size_t AuthTokenLength = 0;
17741776

17751777
TMap<FString, CCoreSubscriptionData> ChannelSubscriptions;
17761778
TMap<FString, CCoreSubscriptionData> ChannelGroupSubscriptions;

0 commit comments

Comments
 (0)