From 8c357db7d3559161729934287b75b64546299edb Mon Sep 17 00:00:00 2001 From: Kamil Gronek Date: Wed, 2 Oct 2024 13:18:36 +0200 Subject: [PATCH 1/5] Add missing fixes from old PC. --- .../PubnubLibrary/Private/PubnubSubsystem.cpp | 177 ++++++++++++------ .../Private/Threads/PubnubLoopingThread.cpp | 4 + .../Public/Config/PubnubSettings.h | 2 +- Source/PubnubLibrary/Public/PubnubSubsystem.h | 38 ++-- Source/PubnubLibrary/PubnubLibrary.Build.cs | 3 +- 5 files changed, 144 insertions(+), 80 deletions(-) diff --git a/Source/PubnubLibrary/Private/PubnubSubsystem.cpp b/Source/PubnubLibrary/Private/PubnubSubsystem.cpp index 3e67f97..550003b 100644 --- a/Source/PubnubLibrary/Private/PubnubSubsystem.cpp +++ b/Source/PubnubLibrary/Private/PubnubSubsystem.cpp @@ -1,9 +1,10 @@ // Copyright 2024 PubNub Inc. All Rights Reserved. #include "PubnubSubsystem.h" -#include #include "Json.h" + #include "Config/PubnubSettings.h" +#include "FunctionLibraries/PubnubUtilities.h" #include "Threads/PubnubFunctionThread.h" #include "Threads/PubnubLoopingThread.h" @@ -24,7 +25,9 @@ void UPubnubSubsystem::Initialize(FSubsystemCollectionBase& Collection) void UPubnubSubsystem::Deinitialize() { DeinitPubnub(); - Super::Deinitialize(); + + //Give some time for C-Core to clean up correctly + FPlatformProcess::Sleep(0.5); if(QuickActionThread) { @@ -34,6 +37,9 @@ void UPubnubSubsystem::Deinitialize() { LongpollThread->Stop(); } + + Super::Deinitialize(); + } void UPubnubSubsystem::InitPubnub() @@ -293,14 +299,14 @@ void UPubnubSubsystem::RevokeToken(FString Token) }); } -void UPubnubSubsystem::ParseToken(FString Token) +void UPubnubSubsystem::ParseToken(FString Token, FOnPubnubResponse OnParseTokenResponse) { if(!CheckQuickActionThreadValidity()) {return;} - QuickActionThread->AddFunctionToQueue( [this, Token] + QuickActionThread->AddFunctionToQueue( [this, Token, OnParseTokenResponse] { - ParseToken_priv(Token); + ParseToken_priv(Token, OnParseTokenResponse); }); } @@ -337,14 +343,14 @@ void UPubnubSubsystem::FetchHistory(FString ChannelName, FOnPubnubResponse OnFet }); } -void UPubnubSubsystem::MessageCounts(FString ChannelName, FString TimeStamp, FOnPubnubResponse OnMessageCountsResponse) +void UPubnubSubsystem::MessageCounts(FString ChannelName, FString Timetoken, FOnPubnubIntResponse OnMessageCountsResponse) { if(!CheckQuickActionThreadValidity()) {return;} - QuickActionThread->AddFunctionToQueue( [this, ChannelName, TimeStamp, OnMessageCountsResponse] + QuickActionThread->AddFunctionToQueue( [this, ChannelName, Timetoken, OnMessageCountsResponse] { - MessageCounts_priv(ChannelName, TimeStamp, OnMessageCountsResponse); + MessageCounts_priv(ChannelName, Timetoken, OnMessageCountsResponse); }); } @@ -515,14 +521,14 @@ void UPubnubSubsystem::RemoveChannelMembers(FString ChannelMetadataID, FString I }); } -void UPubnubSubsystem::AddMessageAction(FString ChannelName, FString MessageTimeToken, EPubnubActionType ActionType, FString Value) +void UPubnubSubsystem::AddMessageAction(FString ChannelName, FString MessageTimetoken, EPubnubActionType ActionType, FString Value, FOnPubnubResponse AddActionResponse) { if(!CheckQuickActionThreadValidity()) {return;} - QuickActionThread->AddFunctionToQueue( [this, ChannelName, MessageTimeToken, ActionType, Value] + QuickActionThread->AddFunctionToQueue( [this, ChannelName, MessageTimetoken, ActionType, Value, AddActionResponse] { - AddMessageAction_priv(ChannelName, MessageTimeToken, ActionType, Value); + AddMessageAction_priv(ChannelName, MessageTimetoken, ActionType, Value, AddActionResponse); }); } @@ -536,7 +542,7 @@ void UPubnubSubsystem::HistoryWithMessageActions(FString ChannelName, FString St HistoryWithMessageActions_priv(ChannelName, Start, End, SizeLimit, OnHistoryWithMessageActionsResponse); }); } - +/* DISABLED void UPubnubSubsystem::HistoryWithMessageActionsContinue(FOnPubnubResponse OnHistoryWithMAContinueResponse) { if(!CheckQuickActionThreadValidity()) @@ -547,6 +553,7 @@ void UPubnubSubsystem::HistoryWithMessageActionsContinue(FOnPubnubResponse OnHis HistoryWithMessageActionsContinue_priv(OnHistoryWithMAContinueResponse); }); } +*/ void UPubnubSubsystem::GetMessageActions(FString ChannelName, FString Start, FString End, int SizeLimit, FOnPubnubResponse OnGetMessageActionsResponse) { @@ -559,6 +566,18 @@ void UPubnubSubsystem::GetMessageActions(FString ChannelName, FString Start, FSt }); } +void UPubnubSubsystem::RemoveMessageAction(FString ChannelName, FString MessageTimetoken, FString ActionTimetoken) +{ + if(!CheckQuickActionThreadValidity()) + {return;} + + QuickActionThread->AddFunctionToQueue( [this, ChannelName, MessageTimetoken, ActionTimetoken] + { + RemoveMessageAction_priv(ChannelName, MessageTimetoken, ActionTimetoken); + }); +} + +/* DISABLED void UPubnubSubsystem::GetMessageActionsContinue(FOnPubnubResponse OnGetMessageActionsContinueResponse) { if(!CheckQuickActionThreadValidity()) @@ -569,6 +588,7 @@ void UPubnubSubsystem::GetMessageActionsContinue(FOnPubnubResponse OnGetMessageA GetMessageActionsContinue_priv(OnGetMessageActionsContinueResponse); }); } +*/ FString UPubnubSubsystem::GrantTokenStructureToJsonString(FPubnubGrantTokenStructure TokenStructure, bool &success) { @@ -667,12 +687,8 @@ FString UPubnubSubsystem::GrantTokenStructureToJsonString(FPubnubGrantTokenStruc PermissionsJsonObject->SetStringField("authorized_uuid", TokenStructure.AuthorizedUUID); PermissionsJsonObject->SetObjectField("permissions", TokenStructureJsonObject); - //Convert created Json to string - FString JsonString; - TSharedRef< TJsonWriter<> > JsonWriter = TJsonWriterFactory<>::Create(&JsonString); - FJsonSerializer::Serialize(PermissionsJsonObject.ToSharedRef(), JsonWriter); - - return JsonString; + //Convert created Json object to string + return UPubnubUtilities::JsonObjectToString(PermissionsJsonObject); } void UPubnubSubsystem::SystemPublish(FString ChannelOpt) @@ -715,6 +731,10 @@ void UPubnubSubsystem::StartPubnubSubscribeLoop() {return;} } + //Check once again, as subsystem could be deinitialized during await + if(!IsInitialized) + {return;} + //At this stage we received messages, so read them and get channel from where they were sent const char* MessageChar = pubnub_get(ctx_sub); const char* ChannelChar = pubnub_get_channel(ctx_sub); @@ -748,17 +768,11 @@ FString UPubnubSubsystem::StringArrayToCommaSeparated(TArray StringArra if(CommaSeparatedString.IsEmpty()) { CommaSeparatedString.Append(StringElement); - CommaSeparatedString.Append(","); - CommaSeparatedString.Append(StringElement); - CommaSeparatedString.Append("-pnpres"); } else { CommaSeparatedString.Append(","); CommaSeparatedString.Append(StringElement); - CommaSeparatedString.Append(","); - CommaSeparatedString.Append(StringElement); - CommaSeparatedString.Append("-pnpres"); } } return CommaSeparatedString; @@ -860,7 +874,6 @@ void UPubnubSubsystem::PubnubPublishError() //Broadcast bound delegate with JsonResponse OnPubnubError.Broadcast(FinalErrorMessage, EPubnubErrorType::PET_Error);; }); - } void UPubnubSubsystem::LoadPluginSettings() @@ -959,7 +972,7 @@ void UPubnubSubsystem::InitPubnub_priv() pubnub_init(ctx_pub, PublishKey, SubscribeKey); pubnub_init(ctx_sub, PublishKey, SubscribeKey); - if(PubnubSettings->SetSecretKetAutomatically) + if(PubnubSettings->SetSecretKeyAutomatically) { SetSecretKey(); } @@ -975,6 +988,8 @@ void UPubnubSubsystem::DeinitPubnub_priv() //Unsubscribe from all channels so this user will not be visible for others anymore UnsubscribeFromAll(); + IsInitialized = false; + if(ctx_pub) { pubnub_free(ctx_pub); @@ -986,7 +1001,6 @@ void UPubnubSubsystem::DeinitPubnub_priv() ctx_sub = nullptr; } - IsInitialized = false; } void UPubnubSubsystem::SetUserID_priv(FString UserID) @@ -1340,7 +1354,7 @@ void UPubnubSubsystem::GrantToken_priv(FString PermissionObject, FOnPubnubRespon if(CheckIsFieldEmpty(PermissionObject, "PermissionObject", "GrantToken")) {return;} - + pubnub_grant_token(ctx_pub, TCHAR_TO_ANSI(*PermissionObject)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); @@ -1385,7 +1399,7 @@ void UPubnubSubsystem::RevokeToken_priv(FString Token) } } -void UPubnubSubsystem::ParseToken_priv(FString Token) +void UPubnubSubsystem::ParseToken_priv(FString Token, FOnPubnubResponse OnParseTokenResponse) { if(!CheckIsPubnubInitialized() || !CheckIsUserIDSet()) {return;} @@ -1400,6 +1414,22 @@ void UPubnubSubsystem::ParseToken_priv(FString Token) { PubnubResponseError(PubnubResponse, "Failed to Parse Token."); } + + pubnub_chamebl_t grant_token_resp = pubnub_get_grant_token(ctx_pub); + if(!grant_token_resp.ptr) + { + PubnubError("Failed to get Parse Token - pointer to token is invalid."); + return; + } + + FString JsonResponse(grant_token_resp.ptr); + + //Delegate needs to be executed back on Game Thread + AsyncTask(ENamedThreads::GameThread, [this, OnParseTokenResponse, JsonResponse]() + { + //Broadcast bound delegate with JsonResponse + OnParseTokenResponse.ExecuteIfBound(JsonResponse); + }); } void UPubnubSubsystem::SetAuthToken_priv(FString Token) @@ -1481,7 +1511,7 @@ void UPubnubSubsystem::FetchHistory_priv(FString ChannelName, FOnPubnubResponse }); } -void UPubnubSubsystem::MessageCounts_priv(FString ChannelName, FString TimeStamp, FOnPubnubResponse OnMessageCountsResponse) +void UPubnubSubsystem::MessageCounts_priv(FString ChannelName, FString Timetoken, FOnPubnubIntResponse OnMessageCountsResponse) { if(!CheckIsPubnubInitialized() || !CheckIsUserIDSet()) {return;} @@ -1489,28 +1519,16 @@ void UPubnubSubsystem::MessageCounts_priv(FString ChannelName, FString TimeStamp if(CheckIsFieldEmpty(ChannelName, "ChannelName", "MessageCounts")) {return;} - //FString UnixTimeStamp = FString::FromInt(TimeStamp.ToUnixTimestamp()); + pubnub_message_counts(ctx_pub, TCHAR_TO_ANSI(*ChannelName), TCHAR_TO_ANSI(*Timetoken)); - //UE_LOG(LogTemp, Warning, TEXT("unix timestamp: %s"), *UnixTimeStamp); - - pubnub_message_counts(ctx_pub, TCHAR_TO_ANSI(*ChannelName), TCHAR_TO_ANSI(*TimeStamp)); + int MessageCountsNumber = 0; + pubnub_get_message_counts(ctx_pub, TCHAR_TO_ANSI(*ChannelName), &MessageCountsNumber); - pubnub_res PubnubResponse = pubnub_await(ctx_pub); - if(PubnubResponse != PNR_OK) - { - PubnubResponseError(PubnubResponse, "Failed to get message counts."); - } - - int MessageCountsReturn; - pubnub_get_message_counts(ctx_pub, TCHAR_TO_ANSI(*ChannelName), &MessageCountsReturn); - - FString JsonResponse(FString::FromInt(MessageCountsReturn)); - //Delegate needs to be executed back on Game Thread - AsyncTask(ENamedThreads::GameThread, [this, OnMessageCountsResponse, JsonResponse]() + AsyncTask(ENamedThreads::GameThread, [this, OnMessageCountsResponse, MessageCountsNumber]() { //Broadcast bound delegate with JsonResponse - OnMessageCountsResponse.ExecuteIfBound(JsonResponse); + OnMessageCountsResponse.ExecuteIfBound(MessageCountsNumber); }); } @@ -1714,7 +1732,8 @@ void UPubnubSubsystem::RemoveMemberships_priv(FString UUIDMetadataID, FString In } } -void UPubnubSubsystem::GetChannelMembers_priv(FString ChannelMetadataID, FString Include, int Limit, FString Start, FString End, EPubnubTribool Count, FOnPubnubResponse OnGetMembersResponse) +void UPubnubSubsystem::GetChannelMembers_priv(FString ChannelMetadataID, FString Include, int Limit, FString Start, + FString End, EPubnubTribool Count, FOnPubnubResponse OnGetMembersResponse) { if(!CheckIsPubnubInitialized() || !CheckIsUserIDSet()) {return;} @@ -1723,9 +1742,8 @@ void UPubnubSubsystem::GetChannelMembers_priv(FString ChannelMetadataID, FString {return;} pubnub_tribool InCount = (pubnub_tribool)(uint8)Count; - pubnub_get_members(ctx_pub,TCHAR_TO_ANSI(*ChannelMetadataID), TCHAR_TO_ANSI(*Include), Limit, TCHAR_TO_ANSI(*Start), TCHAR_TO_ANSI(*End), InCount); - + FString JsonResponse = GetLastResponse(ctx_pub); //Delegate needs to be executed back on Game Thread @@ -1787,16 +1805,16 @@ void UPubnubSubsystem::RemoveChannelMembers_priv(FString ChannelMetadataID, FStr } } -void UPubnubSubsystem::AddMessageAction_priv(FString ChannelName, FString MessageTimeToken, EPubnubActionType ActionType, FString Value) +void UPubnubSubsystem::AddMessageAction_priv(FString ChannelName, FString MessageTimetoken, EPubnubActionType ActionType, FString Value, FOnPubnubResponse AddActionResponse) { if(!CheckIsPubnubInitialized() || !CheckIsUserIDSet()) {return;} - if(CheckIsFieldEmpty(ChannelName, "ChannelName", "AddMessageAction") || CheckIsFieldEmpty(MessageTimeToken, "MessageTimeToken", "AddMessageAction")) + if(CheckIsFieldEmpty(ChannelName, "ChannelName", "AddMessageAction") || CheckIsFieldEmpty(MessageTimetoken, "MessageTimetoken", "AddMessageAction")) {return;} pubnub_action_type PubnubActionType = (pubnub_action_type)(uint8)ActionType; - pubnub_add_message_action(ctx_pub, TCHAR_TO_ANSI(*ChannelName), TCHAR_TO_ANSI(*MessageTimeToken), PubnubActionType, TCHAR_TO_ANSI(*Value)); + pubnub_add_message_action(ctx_pub, TCHAR_TO_ANSI(*ChannelName), TCHAR_TO_ANSI(*MessageTimetoken), PubnubActionType, TCHAR_TO_ANSI(*Value)); pubnub_res PubnubResponse = pubnub_await(ctx_pub); if(PubnubResponse != PNR_OK) @@ -1804,12 +1822,63 @@ void UPubnubSubsystem::AddMessageAction_priv(FString ChannelName, FString Messag PubnubResponseError(PubnubResponse, "Failed to Add Message Action."); } pubnub_chamebl_t AddMessageActionResponse = pubnub_get_message_action_timetoken(ctx_pub); + if(!AddMessageActionResponse.ptr) { return; } FString JsonResponse(AddMessageActionResponse.ptr); - UE_LOG(PubnubLog, Warning, TEXT("AddMessageAction response: %s"), *JsonResponse); + + //Delegate needs to be executed back on Game Thread + AsyncTask(ENamedThreads::GameThread, [this, AddActionResponse, JsonResponse]() + { + //Broadcast bound delegate with JsonResponse + AddActionResponse.ExecuteIfBound(JsonResponse); + }); +} + +void UPubnubSubsystem::RemoveMessageAction_priv(FString ChannelName, FString MessageTimetoken, FString ActionTimetoken) +{ + if(!CheckIsPubnubInitialized() || !CheckIsUserIDSet()) + {return;} + + if(CheckIsFieldEmpty(ChannelName, "ChannelName", "RemoveMessageAction") || CheckIsFieldEmpty(MessageTimetoken, "MessageTimetoken", "RemoveMessageAction") + || CheckIsFieldEmpty(ActionTimetoken, "ActionTimetoken", "RemoveMessageAction")) + {return;} + + auto MessageTimetokenConverter = StringCast(*MessageTimetoken); + auto ActionTimetokenConverter = StringCast(*ActionTimetoken); + + // Allocate memory for message_timetoken_char and copy the content + char* message_timetoken_char = new char[MessageTimetoken.Len() + 1]; + std::strcpy(message_timetoken_char, MessageTimetokenConverter.Get()); + + pubnub_chamebl_t message_timetoken_chamebl; + message_timetoken_chamebl.ptr = message_timetoken_char; + message_timetoken_chamebl.size = MessageTimetoken.Len(); + + // Allocate memory for action_timetoken_char and copy the content + char* action_timetoken_char = new char[ActionTimetoken.Len() + 1]; + std::strcpy(action_timetoken_char, ActionTimetokenConverter.Get()); + + pubnub_chamebl_t action_timetoken_chamebl; + action_timetoken_chamebl.ptr = action_timetoken_char; + action_timetoken_chamebl.size = ActionTimetoken.Len(); + + pubnub_remove_message_action(ctx_pub, TCHAR_TO_ANSI(*ChannelName), message_timetoken_chamebl, action_timetoken_chamebl); + + pubnub_res PubnubResponse = pubnub_await(ctx_pub); + + FString JsonResponse = GetLastResponse(ctx_pub); + + if(PubnubResponse != PNR_OK) + { + PubnubResponseError(PubnubResponse, "Failed to Remove Message Action."); + } + + // Clean up allocated memory + delete[] message_timetoken_char; + delete[] action_timetoken_char; } void UPubnubSubsystem::HistoryWithMessageActions_priv(FString ChannelName, FString Start, FString End, int SizeLimit, FOnPubnubResponse OnHistoryWithMessageActionsResponse) diff --git a/Source/PubnubLibrary/Private/Threads/PubnubLoopingThread.cpp b/Source/PubnubLibrary/Private/Threads/PubnubLoopingThread.cpp index 8a2ce17..313f3b9 100644 --- a/Source/PubnubLibrary/Private/Threads/PubnubLoopingThread.cpp +++ b/Source/PubnubLibrary/Private/Threads/PubnubLoopingThread.cpp @@ -36,6 +36,10 @@ uint32 FPubnubLoopingThread::Run() //Run all functions from queue for(int i = 0; i < PubnubAsyncLoopingFunctions.Num(); i++) { + if(bShutdown) + { + return 0; + } PubnubAsyncLoopingFunctions[i](); } } diff --git a/Source/PubnubLibrary/Public/Config/PubnubSettings.h b/Source/PubnubLibrary/Public/Config/PubnubSettings.h index 5b336be..cc120b6 100644 --- a/Source/PubnubLibrary/Public/Config/PubnubSettings.h +++ b/Source/PubnubLibrary/Public/Config/PubnubSettings.h @@ -26,5 +26,5 @@ class PUBNUBLIBRARY_API UPubnubSettings : public UDeveloperSettings //Should Secret Key be used automatically (secret key gives host permissions to the user). If set to false, SetSecretKey needs to be called manually to give host permissions. UPROPERTY(Config, EditAnywhere, Category = "Init") - bool SetSecretKetAutomatically = false; + bool SetSecretKeyAutomatically = false; }; \ No newline at end of file diff --git a/Source/PubnubLibrary/Public/PubnubSubsystem.h b/Source/PubnubLibrary/Public/PubnubSubsystem.h index d8d6066..af5a970 100644 --- a/Source/PubnubLibrary/Public/PubnubSubsystem.h +++ b/Source/PubnubLibrary/Public/PubnubSubsystem.h @@ -21,6 +21,7 @@ class UPubnubChatSystem; DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnMessageReceived, FString, MessageJson, FString, Channel); DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnPubnubError, FString, ErrorMessage, EPubnubErrorType, ErrorType); DECLARE_DYNAMIC_DELEGATE_OneParam(FOnPubnubResponse, FString, JsonResponse); +DECLARE_DYNAMIC_DELEGATE_OneParam(FOnPubnubIntResponse, int, IntValue); UCLASS() @@ -121,7 +122,7 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem void RevokeToken(FString Token); UFUNCTION(BlueprintCallable, Category = "Pubnub|AccessManager") - void ParseToken(FString Token); + void ParseToken(FString Token, FOnPubnubResponse OnParseTokenResponse); UFUNCTION(BlueprintCallable, Category = "Pubnub|AccessManager") void SetAuthToken(FString Token); @@ -133,7 +134,7 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem void FetchHistory(FString ChannelName, FOnPubnubResponse OnFetchHistoryResponse, FPubnubFetchHistorySettings FetchHistorySettings = FPubnubFetchHistorySettings()); UFUNCTION(BlueprintCallable, Category = "Pubnub|MessagePersistence") - void MessageCounts(FString ChannelName, FString TimeStamp, FOnPubnubResponse OnMessageCountsResponse); + void MessageCounts(FString ChannelName, FString Timetoken, FOnPubnubIntResponse OnMessageCountsResponse); UFUNCTION(BlueprintCallable, Category = "Pubnub|AppContext") void GetAllUUIDMetadata(FString Include, int Limit, FString Start, FString End, EPubnubTribool Count, FOnPubnubResponse OnGetAllUUIDMetadataResponse); @@ -181,19 +182,22 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem void RemoveChannelMembers(FString ChannelMetadataID, FString Include, FString RemoveObj); UFUNCTION(BlueprintCallable, Category = "Pubnub|MessageActions") - void AddMessageAction(FString ChannelName, FString MessageTimeToken, EPubnubActionType ActionType, FString Value); + void AddMessageAction(FString ChannelName, FString MessageTimetoken, EPubnubActionType ActionType, FString Value, FOnPubnubResponse AddActionResponse); UFUNCTION(BlueprintCallable, Category = "Pubnub|MessageActions") void HistoryWithMessageActions(FString ChannelName, FString Start, FString End, int SizeLimit, FOnPubnubResponse OnHistoryWithMessageActionsResponse); - UFUNCTION(BlueprintCallable, Category = "Pubnub|MessageActions") - void HistoryWithMessageActionsContinue(FOnPubnubResponse OnHistoryWithMAContinueResponse); + //UFUNCTION(BlueprintCallable, Category = "Pubnub|MessageActions") + //void HistoryWithMessageActionsContinue(FOnPubnubResponse OnHistoryWithMAContinueResponse); UFUNCTION(BlueprintCallable, Category = "Pubnub|MessageActions") void GetMessageActions(FString ChannelName, FString Start, FString End, int SizeLimit, FOnPubnubResponse OnGetMessageActionsResponse); UFUNCTION(BlueprintCallable, Category = "Pubnub|MessageActions") - void GetMessageActionsContinue(FOnPubnubResponse OnGetMessageActionsContinueResponse); + void RemoveMessageAction(FString ChannelName, FString MessageTimetoken, FString ActionTimetoken); + + //UFUNCTION(BlueprintCallable, Category = "Pubnub|MessageActions") + //void GetMessageActionsContinue(FOnPubnubResponse OnGetMessageActionsContinueResponse); #pragma endregion @@ -303,11 +307,11 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem void Heartbeat_priv(FString ChannelName, FString ChannelGroup); void GrantToken_priv(FString PermissionObject, FOnPubnubResponse OnGrantTokenResponse); void RevokeToken_priv(FString Token); - void ParseToken_priv(FString Token); + void ParseToken_priv(FString Token, FOnPubnubResponse OnParseTokenResponse); void SetAuthToken_priv(FString Token); void History_priv(FString ChannelName, FOnPubnubResponse OnHistoryResponse, FPubnubHistorySettings HistorySettings = FPubnubHistorySettings()); void FetchHistory_priv(FString ChannelName, FOnPubnubResponse OnFetchHistoryResponse, FPubnubFetchHistorySettings FetchHistorySettings = FPubnubFetchHistorySettings()); - void MessageCounts_priv(FString ChannelName, FString TimeStamp, FOnPubnubResponse OnMessageCountsResponse); + void MessageCounts_priv(FString ChannelName, FString Timetoken, FOnPubnubIntResponse OnMessageCountsResponse); void GetAllUUIDMetadata_priv(FString Include, int Limit, FString Start, FString End, EPubnubTribool Count, FOnPubnubResponse OnGetAllUUIDMetadataResponse); void SetUUIDMetadata_priv(FString UUIDMetadataID, FString Include, FString UUIDMetadataObj); void GetUUIDMetadata_priv(FString Include, FString UUIDMetadataID, FOnPubnubResponse OnGetUUIDMetadataResponse); @@ -323,7 +327,8 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem void AddChannelMembers_priv(FString ChannelMetadataID, FString Include, FString AddObj); void SetChannelMembers_priv(FString ChannelMetadataID, FString Include, FString SetObj); void RemoveChannelMembers_priv(FString ChannelMetadataID, FString Include, FString RemoveObj); - void AddMessageAction_priv(FString ChannelName, FString MessageTimeToken, EPubnubActionType ActionType, FString Value); + void AddMessageAction_priv(FString ChannelName, FString MessageTimetoken, EPubnubActionType ActionType, FString Value, FOnPubnubResponse AddActionResponse); + void RemoveMessageAction_priv(FString ChannelName, FString MessageTimetoken, FString ActionTimetoken); void HistoryWithMessageActions_priv(FString ChannelName, FString Start, FString End, int SizeLimit, FOnPubnubResponse OnHistoryWithMessageActionsResponse); void HistoryWithMessageActionsContinue_priv(FOnPubnubResponse OnHistoryWithMAContinueResponse); void GetMessageActions_priv(FString ChannelName, FString Start, FString End, int SizeLimit, FOnPubnubResponse OnGetMessageActionsResponse); @@ -344,20 +349,5 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem TSharedPtr AddChannelPermissionsToJson(TArray Channels, TArray ChannelPermissions); TSharedPtr AddChannelGroupPermissionsToJson(TArray ChannelGroups, TArray ChannelGroupPermissions); TSharedPtr AddUUIDPermissionsToJson(TArray UUIDs, TArray UUIDPermissions); - - static void CallbackFunctionInClass(int number, const char* response) - { - UE_LOG(LogTemp, Warning, TEXT("Callback from class, number: %d"), number); - }; - - void DynamicCallbackFunctionInClass(int number, const char* response) - { - UE_LOG(LogTemp, Warning, TEXT("Dynamic Callback from class, number: %d"), number); - }; - }; -static void CallbackFunction(int number, const char* response) -{ - UE_LOG(LogTemp, Warning, TEXT("Callback number: %d"), number); -}; \ No newline at end of file diff --git a/Source/PubnubLibrary/PubnubLibrary.Build.cs b/Source/PubnubLibrary/PubnubLibrary.Build.cs index f9cca70..8930183 100644 --- a/Source/PubnubLibrary/PubnubLibrary.Build.cs +++ b/Source/PubnubLibrary/PubnubLibrary.Build.cs @@ -14,7 +14,8 @@ public PubnubLibrary(ReadOnlyTargetRules Target) : base(Target) new string[] { "Core", - "PubNubModule" + "PubNubModule", + "Projects" } ); From b879125651e28cd87b00b978cda7a237ce68e47e Mon Sep 17 00:00:00 2001 From: Kamil Gronek Date: Thu, 3 Oct 2024 09:55:22 +0200 Subject: [PATCH 2/5] Fix some ways to get crash. --- .../PubnubLibrary/Private/PubnubSubsystem.cpp | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/Source/PubnubLibrary/Private/PubnubSubsystem.cpp b/Source/PubnubLibrary/Private/PubnubSubsystem.cpp index 550003b..9c05321 100644 --- a/Source/PubnubLibrary/Private/PubnubSubsystem.cpp +++ b/Source/PubnubLibrary/Private/PubnubSubsystem.cpp @@ -49,24 +49,26 @@ void UPubnubSubsystem::InitPubnub() PubnubError("Pubnub is already initialized", EPubnubErrorType::PET_Warning); return; } - - //Create new threads - separate for subscribe and all other operations - QuickActionThread = new FPubnubFunctionThread; - LongpollThread = new FPubnubLoopingThread; - - if(!CheckQuickActionThreadValidity()) - {return;} - QuickActionThread->AddFunctionToQueue( [this] + InitPubnub_priv(); + + //If initialized correctly, create required thread. + if(IsInitialized) { - InitPubnub_priv(); - }); + //Create new threads - separate for subscribe and all other operations + QuickActionThread = new FPubnubFunctionThread; + LongpollThread = new FPubnubLoopingThread; + } + } void UPubnubSubsystem::DeinitPubnub() { - if(!CheckQuickActionThreadValidity()) - {return;} + if(!QuickActionThread) + { + DeinitPubnub_priv(); + return; + } QuickActionThread->AddFunctionToQueue( [this] { @@ -828,6 +830,11 @@ void UPubnubSubsystem::PubnubError(FString ErrorMessage, EPubnubErrorType ErrorT UE_LOG(PubnubLog, Warning, TEXT("%s"), *ErrorMessage); } + if(!OnPubnubError.IsBound()) + { + return; + } + //Errors has to be broadcasted on GameThread, otherwise engine will crash if someone uses them for example with widgets AsyncTask(ENamedThreads::GameThread, [this, ErrorMessage, ErrorType]() { @@ -959,11 +966,13 @@ void UPubnubSubsystem::InitPubnub_priv() if(std::strlen(PublishKey) == 0 ) { PubnubError("Publish key is empty, can't initialize Pubnub"); + return; } if(std::strlen(SubscribeKey) == 0 ) { PubnubError("Subscribe key is empty, can't initialize Pubnub"); + return; } ctx_pub = pubnub_alloc(); @@ -1179,8 +1188,11 @@ void UPubnubSubsystem::UnsubscribeFromAll_priv() } LongpollThread->ClearLoopingFunctions(); - - SystemPublish(ChannelForSystemPublish); + + if(!ChannelForSystemPublish.IsEmpty()) + { + SystemPublish(ChannelForSystemPublish); + } } void UPubnubSubsystem::AddChannelToGroup_priv(FString ChannelName, FString ChannelGroup) From cca051a9f680ddd6e57c0aef68a9d78afb1cbfa7 Mon Sep 17 00:00:00 2001 From: Kamil Gronek Date: Thu, 3 Oct 2024 12:40:38 +0200 Subject: [PATCH 3/5] Add new version --- PubnubLibrary.uplugin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PubnubLibrary.uplugin b/PubnubLibrary.uplugin index 967d604..a38181e 100644 --- a/PubnubLibrary.uplugin +++ b/PubnubLibrary.uplugin @@ -1,7 +1,7 @@ { "FileVersion": 3, - "Version": 7, - "VersionName": "0.1.6", + "Version": 78 + "VersionName": "0.1.7", "FriendlyName": "Pubnub Unreal SDK", "Description": "Quickly add interactive features to your game that scale without building your backend infrastructure.", "Category": "Code", From 3fe3727989eda8a9538926d3efa94b0ffd24c002 Mon Sep 17 00:00:00 2001 From: Kamil Gronek Date: Thu, 3 Oct 2024 13:02:18 +0200 Subject: [PATCH 4/5] Fix typo in version number --- PubnubLibrary.uplugin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PubnubLibrary.uplugin b/PubnubLibrary.uplugin index a38181e..5e6b502 100644 --- a/PubnubLibrary.uplugin +++ b/PubnubLibrary.uplugin @@ -1,6 +1,6 @@ { "FileVersion": 3, - "Version": 78 + "Version": 8 "VersionName": "0.1.7", "FriendlyName": "Pubnub Unreal SDK", "Description": "Quickly add interactive features to your game that scale without building your backend infrastructure.", From 161b51bac3cc5ed13f111ce7031b78b16297f0c1 Mon Sep 17 00:00:00 2001 From: Kamil Gronek Date: Thu, 3 Oct 2024 13:16:29 +0200 Subject: [PATCH 5/5] Missing comma --- PubnubLibrary.uplugin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PubnubLibrary.uplugin b/PubnubLibrary.uplugin index 5e6b502..1998905 100644 --- a/PubnubLibrary.uplugin +++ b/PubnubLibrary.uplugin @@ -1,6 +1,6 @@ { "FileVersion": 3, - "Version": 8 + "Version": 8, "VersionName": "0.1.7", "FriendlyName": "Pubnub Unreal SDK", "Description": "Quickly add interactive features to your game that scale without building your backend infrastructure.",