Skip to content

Commit dfcbf44

Browse files
Finish init/deinit PubnubClient.
1 parent ff3c6fe commit dfcbf44

File tree

2 files changed

+123
-9
lines changed

2 files changed

+123
-9
lines changed

Source/PubnubLibrary/Private/PubnubClient.cpp

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "PubnubClient.h"
55
#include "PubNub.h"
6+
#include "PubnubInternalMacros.h"
67
#include "PubnubSubsystem.h"
78
#include "Threads/PubnubFunctionThread.h"
89
#include "FunctionLibraries/PubnubUtilities.h"
@@ -76,15 +77,57 @@ void UPubnubClient::InitWithConfig(UPubnubSubsystem* InPubnubSubsystem, FPubnubC
7677

7778
void UPubnubClient::DeinitializeClient()
7879
{
80+
if(!IsInitialized)
81+
{return;}
82+
83+
if(PubnubCallsThread)
84+
{
85+
PubnubCallsThread->Stop();
86+
delete PubnubCallsThread;
87+
PubnubCallsThread = nullptr;
88+
}
89+
90+
//Unsubscribe from all channels and groups so this user will not be visible for others anymore
91+
UnsubscribeFromAll_priv();
92+
7993
IsInitialized = false;
8094
PubnubSubsystem = nullptr;
95+
96+
if(ctx_pub && ctx_ee)
97+
{
98+
//We set this to prevent crash from C-Core when it's trying to clean up provider made in UE
99+
pubnub_set_crypto_module(ctx_pub, nullptr);
100+
pubnub_set_crypto_module(ctx_ee, nullptr);
101+
102+
//Clean up Crypto bridge if it was created
103+
if(CryptoBridge)
104+
{
105+
CryptoBridge->CleanUpCryptoBridge();
106+
}
107+
108+
//Clean up C-core pubnub contexts
109+
pubnub_free(ctx_pub);
110+
pubnub_free(ctx_ee);
111+
ctx_pub = nullptr;
112+
ctx_ee = nullptr;
113+
}
114+
115+
IsUserIDSet = false;
116+
delete[] AuthTokenBuffer;
117+
AuthTokenBuffer = nullptr;
118+
AuthTokenLength = 0;
119+
120+
SubscriptionResultDelegates.Empty();
121+
122+
//Notify that Deinitialization is finished
123+
OnClientDeinitialized.Broadcast(ClientID);
81124
}
82125

83126
void UPubnubClient::SavePubnubConfig(const FPubnubConfig& InConfig)
84127
{
85128
PubnubConfig = InConfig;
86129

87-
// Safely copy all keys using the utility function
130+
//Safely copy all keys using the utility function
88131
UPubnubUtilities::SafeCopyFStringToCharBuffer(PublishKey, PublishKeySize + 1, InConfig.PublishKey, TEXT("PublishKey"));
89132
UPubnubUtilities::SafeCopyFStringToCharBuffer(SubscribeKey, PublishKeySize + 1, InConfig.SubscribeKey, TEXT("SubscribeKey"));
90133
UPubnubUtilities::SafeCopyFStringToCharBuffer(SecretKey, SecretKeySize + 1, InConfig.SecretKey, TEXT("SecretKey"));
@@ -202,3 +245,47 @@ void UPubnubClient::InitPubnub_priv(const FPubnubConfig& Config)
202245
SetSecretKey();
203246
}
204247
}
248+
249+
void UPubnubClient::UnsubscribeFromAll_priv(FPubnubOnSubscribeOperationResponseNative OnUnsubscribeFromAllResponse)
250+
{
251+
if(ChannelSubscriptions.IsEmpty() && ChannelGroupSubscriptions.IsEmpty())
252+
{
253+
UPubnubUtilities::CallPubnubDelegate(OnUnsubscribeFromAllResponse, FPubnubOperationResult({200, false, ""}));
254+
return;
255+
}
256+
257+
PUBNUB_ENSURE_USER_ID_IS_SET(OnUnsubscribeFromAllResponse);
258+
259+
//All subscription related operations are non blocking, so we lock ActionThread manually,
260+
//make it wait with calling other function until we have subscription result
261+
PubnubCallsThread->LockForSubscribeOperation();
262+
263+
pubnub_unsubscribe_all(ctx_ee);
264+
265+
//Clean up all channel subscriptions
266+
for(auto& Pair : ChannelSubscriptions)
267+
{
268+
if(Pair.Value)
269+
{
270+
if(Pair.Value->Subscription)
271+
{
272+
pubnub_subscription_free(&Pair.Value->Subscription);
273+
}
274+
delete Pair.Value;
275+
}
276+
}
277+
for(auto& Pair : ChannelGroupSubscriptions)
278+
{
279+
if(Pair.Value)
280+
{
281+
if(Pair.Value->Subscription)
282+
{
283+
pubnub_subscription_free(&Pair.Value->Subscription);
284+
}
285+
delete Pair.Value;
286+
}
287+
}
288+
289+
ChannelSubscriptions.Empty();
290+
ChannelGroupSubscriptions.Empty();
291+
}

Source/PubnubLibrary/Public/PubnubClient.h

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
#include "PubnubClient.generated.h"
1010

1111
class UPubnubSubsystem;
12+
class UPubnubCryptoBridge;
1213
class FPubnubFunctionThread;
1314
struct CCoreSubscriptionCallback;
1415

15-
1616
struct pubnub_;
1717
typedef struct pubnub_ pubnub_t;
1818
enum pubnub_res;
1919

2020

21+
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPubnubOnClientDeinitialized, int, ClientID);
2122
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FPubnubOnSubscriptionStatusChanged, EPubnubSubscriptionStatus, Status, FPubnubSubscriptionStatusData, StatusData);
2223
DECLARE_MULTICAST_DELEGATE_TwoParams(FPubnubOnSubscriptionStatusChangedNative, EPubnubSubscriptionStatus Status, const FPubnubSubscriptionStatusData& StatusData);
2324

@@ -39,16 +40,20 @@ class PUBNUBLIBRARY_API UPubnubClient : public UObject
3940

4041
public:
4142

42-
/**Listener to react for subscription status changed */
43+
/**Listener to react for subscription status changed*/
4344
UPROPERTY(BlueprintAssignable, Category = "Pubnub|Delegates")
4445
FPubnubOnSubscriptionStatusChanged OnSubscriptionStatusChanged;
4546

4647
/**Listener to react for subscription status changed , equivalent that accepts lambdas*/
4748
FPubnubOnSubscriptionStatusChangedNative OnSubscriptionStatusChangedNative;
49+
50+
/**Delegate that is called when PubnubClient is deinitialized*/
51+
UPROPERTY(BlueprintAssignable, Category = "Pubnub|Delegates")
52+
FPubnubOnClientDeinitialized OnClientDeinitialized;
4853

4954

50-
UFUNCTION(BlueprintCallable, Category="PubnubClient")
51-
int GetClientID() {return ClientID;};
55+
UFUNCTION(BlueprintPure, BlueprintCallable, Category="PubnubClient")
56+
int GetClientID() const {return ClientID;};
5257

5358
UFUNCTION(BlueprintCallable, Category="PubnubClient")
5459
void DestroyClient();
@@ -133,8 +138,25 @@ class PUBNUBLIBRARY_API UPubnubClient : public UObject
133138
int ClientID = -1;
134139
FString DebugName = "";
135140
bool IsInitialized = false;
141+
bool IsUserIDSet = false;
136142

137-
#pragma endregion
143+
#pragma endregion
144+
145+
#pragma region PUBNUB CRYPTO
146+
147+
//CryptoBridge class that holds provided CryptoModule and inserts it into C-Core system - it keeps all required references alive
148+
UPROPERTY()
149+
TObjectPtr<UPubnubCryptoBridge> CryptoBridge;
150+
151+
#pragma endregion
152+
153+
#pragma region PUBNUB AUTH
154+
155+
//Auth token has to be kept alive for the lifetime of the sdk, so this is the container for it
156+
char* AuthTokenBuffer = nullptr;
157+
size_t AuthTokenLength = 0;
158+
159+
#pragma endregion
138160

139161
#pragma region PUBNUB CONFIG
140162

@@ -154,9 +176,7 @@ class PUBNUBLIBRARY_API UPubnubClient : public UObject
154176

155177
#pragma endregion
156178

157-
//TODO:: Move these functions to the logger
158-
void PubnubError(FString ErrorMessage, EPubnubErrorType ErrorType = EPubnubErrorType::PET_Error);
159-
void PubnubResponseError(pubnub_res PubnubResponse, FString ErrorMessage);
179+
#pragma region PUBNUB SUBSCRIPTION
160180

161181
//Storage for global subscriptions (not from Entities)
162182
TMap<FString, CCoreSubscriptionCallback*> ChannelSubscriptions;
@@ -167,6 +187,13 @@ class PUBNUBLIBRARY_API UPubnubClient : public UObject
167187

168188
void OnCCoreSubscriptionStatusReceived(int StatusEnum, const void* StatusData);
169189

190+
#pragma endregion
191+
192+
//TODO:: Move these functions to the logger
193+
void PubnubError(FString ErrorMessage, EPubnubErrorType ErrorType = EPubnubErrorType::PET_Error);
194+
void PubnubResponseError(pubnub_res PubnubResponse, FString ErrorMessage);
170195

171196
void InitPubnub_priv(const FPubnubConfig& Config);
197+
void UnsubscribeFromAll_priv(FPubnubOnSubscribeOperationResponseNative OnUnsubscribeFromAllResponse = nullptr);
172198
};
199+

0 commit comments

Comments
 (0)