Skip to content

Commit 8486786

Browse files
Create PubnubClient class. Add basic functions to Create/Destroy the client.
1 parent fc85ad7 commit 8486786

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Fill out your copyright notice in the Description page of Project Settings.
2+
3+
4+
#include "PubnubClient.h"
5+
#include "PubnubSubsystem.h"
6+
7+
void UPubnubClient::DestroyClient()
8+
{
9+
if(!PubnubSubsystem)
10+
{return;}
11+
12+
PubnubSubsystem->DestroyPubnubClient(this);
13+
}
14+
15+
void UPubnubClient::InitWithConfig(UPubnubSubsystem* InPubnubSubsystem, FPubnubConfig InConfig, int InClientID, FString InDebugName )
16+
{
17+
PubnubSubsystem = InPubnubSubsystem;
18+
ClientID = InClientID;
19+
DebugName = InDebugName;
20+
21+
IsInitialized = true;
22+
}
23+
24+
void UPubnubClient::DeinitializeClient()
25+
{
26+
IsInitialized = false;
27+
PubnubSubsystem = nullptr;
28+
}

Source/PubnubLibrary/Private/PubnubSubsystem.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Entities/PubnubChannelMetadataEntity.h"
1818
#include "Entities/PubnubUserMetadataEntity.h"
1919
#include "Entities/PubnubSubscription.h"
20+
#include "PubnubClient.h"
2021

2122
DEFINE_LOG_CATEGORY(PubnubLog)
2223

@@ -39,6 +40,39 @@ void UPubnubSubsystem::Deinitialize()
3940
Super::Deinitialize();
4041
}
4142

43+
UPubnubClient* UPubnubSubsystem::CreatePubnubClient(FPubnubConfig Config, FString DebugName)
44+
{
45+
UPubnubClient* PubnubClient = NewObject<UPubnubClient>(this);
46+
47+
int NewID = NextClientID++;
48+
49+
PubnubClient->InitWithConfig(this, Config, NewID, DebugName);
50+
51+
PubnubClients.Add(NewID, PubnubClient);
52+
53+
return PubnubClient;
54+
}
55+
56+
UPubnubClient* UPubnubSubsystem::GetPubnubClient(int ClientID)
57+
{
58+
return PubnubClients.FindRef(ClientID);
59+
}
60+
61+
bool UPubnubSubsystem::DestroyPubnubClient(UPubnubClient* ClientToDestroy)
62+
{
63+
if(!ClientToDestroy)
64+
{return false;}
65+
66+
if(PubnubClients.Find(ClientToDestroy->GetClientID()))
67+
{
68+
PubnubClients.Remove(ClientToDestroy->GetClientID());
69+
ClientToDestroy->DeinitializeClient();
70+
return true;
71+
}
72+
73+
return false;
74+
}
75+
4276
void UPubnubSubsystem::InitPubnub()
4377
{
4478
InitPubnubWithConfig(UPubnubUtilities::PubnubConfigFromPluginSettings(PubnubPluginSettings));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Fill out your copyright notice in the Description page of Project Settings.
2+
3+
#pragma once
4+
5+
#include "CoreMinimal.h"
6+
#include "UObject/Object.h"
7+
#include "PubnubStructLibrary.h"
8+
#include "PubnubClient.generated.h"
9+
10+
class UPubnubSubsystem;
11+
12+
/**
13+
*
14+
*/
15+
UCLASS()
16+
class PUBNUBLIBRARY_API UPubnubClient : public UObject
17+
{
18+
GENERATED_BODY()
19+
20+
friend class UPubnubSubsystem;
21+
22+
public:
23+
24+
25+
UFUNCTION(BlueprintCallable, Category="PubnubClient")
26+
int GetClientID() {return ClientID;};
27+
28+
UFUNCTION(BlueprintCallable, Category="PubnubClient")
29+
void DestroyClient();
30+
31+
private:
32+
33+
void InitWithConfig(UPubnubSubsystem* InPubnubSubsystem, FPubnubConfig InConfig, int InClientID, FString InDebugName = "");
34+
void DeinitializeClient();
35+
36+
UPROPERTY()
37+
TObjectPtr<UPubnubSubsystem> PubnubSubsystem = nullptr;
38+
int ClientID = -1;
39+
bool IsInitialized = false;
40+
FString DebugName = "";
41+
};

Source/PubnubLibrary/Public/PubnubSubsystem.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class UPubnubChannelGroupEntity;
2626
class UPubnubChannelMetadataEntity;
2727
class UPubnubUserMetadataEntity;
2828
class UPubnubSubscriptionSet;
29+
class UPubnubClient;
2930

3031
struct CCoreSubscriptionData
3132
{
@@ -148,6 +149,20 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
148149
/**Listener to react for subscription status changed , equivalent that accepts lambdas*/
149150
FOnSubscriptionStatusChangedNative OnSubscriptionStatusChangedNative;
150151

152+
#pragma region PUBNUB CLIENT
153+
154+
UFUNCTION(BlueprintCallable, Category = "Pubnub|Client")
155+
UPubnubClient* CreatePubnubClient(FPubnubConfig Config, FString DebugName = "");
156+
157+
UFUNCTION(BlueprintCallable, Category = "Pubnub|Client")
158+
UPubnubClient* GetPubnubClient(int ClientID);
159+
160+
UFUNCTION(BlueprintCallable, Category = "Pubnub|Client")
161+
bool DestroyPubnubClient(UPubnubClient* ClientToDestroy);
162+
163+
164+
#pragma endregion
165+
151166
#pragma region BLUEPRINT EXPOSED
152167

153168

@@ -1813,6 +1828,11 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
18131828
#pragma endregion
18141829

18151830
private:
1831+
1832+
UPROPERTY()
1833+
TMap<int, TObjectPtr<UPubnubClient>> PubnubClients;
1834+
int NextClientID = 0;
1835+
18161836

18171837
//Thread for all PubNub operations, this thread will queue all PubNub calls and trigger them one by one
18181838
FPubnubFunctionThread* QuickActionThread = nullptr;

0 commit comments

Comments
 (0)