Skip to content

Commit 66584e6

Browse files
Add result handling for all subscription related operations.
1 parent af0bad4 commit 66584e6

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

Source/PubnubLibrary/Private/PubnubSubsystem.cpp

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ void UPubnubSubsystem::SubscribeToChannel_priv(FString Channel, FOnSubscribeOper
14191419

14201420
if(ChannelSubscriptions.Contains(Channel))
14211421
{
1422-
PubnubError("[SubscribeToChannel]: Already subscribed to chis channel. Aborting operation.", EPubnubErrorType::PET_Warning);
1422+
PubnubError("[SubscribeToChannel]: Already subscribed to this channel. Aborting operation.", EPubnubErrorType::PET_Warning);
14231423
UPubnubUtilities::CallPubnubDelegateWithInvalidArgumentResult(OnSubscribeToChannelResponse, "[SubscribeToChannel]: Already subscribed to chis channel. Aborting operation.");
14241424
return;
14251425
}
@@ -1475,7 +1475,7 @@ void UPubnubSubsystem::SubscribeToGroup_priv(FString ChannelGroup, FOnSubscribeO
14751475

14761476
if(ChannelGroupSubscriptions.Contains(ChannelGroup))
14771477
{
1478-
PubnubError("[SubscribeToGroup]: Already subscribed to chis channel. Aborting operation.", EPubnubErrorType::PET_Warning);
1478+
PubnubError("[SubscribeToGroup]: Already subscribed to this channel group. Aborting operation.", EPubnubErrorType::PET_Warning);
14791479
UPubnubUtilities::CallPubnubDelegateWithInvalidArgumentResult(OnSubscribeToGroupResponse, "[SubscribeToGroup]: Already subscribed to chis channel group. Aborting operation.");
14801480
return;
14811481
}
@@ -1489,16 +1489,16 @@ void UPubnubSubsystem::SubscribeToGroup_priv(FString ChannelGroup, FOnSubscribeO
14891489

14901490
if(nullptr == Subscription)
14911491
{
1492-
PubnubError("Failed to subscribe to group. Pubnub_subscription_alloc didn't create subscription");
1492+
PubnubError("[SubscribeToGroup]: Failed to subscribe to channel group. Pubnub_subscription_alloc didn't create subscription.");
1493+
UPubnubUtilities::CallPubnubDelegateWithInvalidArgumentResult(OnSubscribeToGroupResponse, "[SubscribeToGroup]: Failed to subscribe to channel group. Pubnub_subscription_alloc didn't create subscription.");
1494+
QuickActionThread->UnlockAfterSubscriptionOperationFinished();
14931495
return;
14941496
}
14951497

14961498
//Create callback that will be triggered by the c-core event engine
14971499
pubnub_subscribe_message_callback_t Callback = +[](const pubnub_t* pb, struct pubnub_v2_message message, void* user_data)
14981500
{
14991501
UPubnubSubsystem* ThisSubsystem = static_cast<UPubnubSubsystem*>(user_data);
1500-
if(!ThisSubsystem)
1501-
{return;}
15021502
FPubnubMessageData MessageData = UEMessageFromPubnub(message);
15031503
AsyncTask(ENamedThreads::GameThread, [MessageData, ThisSubsystem]()
15041504
{
@@ -1513,7 +1513,9 @@ void UPubnubSubsystem::SubscribeToGroup_priv(FString ChannelGroup, FOnSubscribeO
15131513
//Add subscription listener and subscribe with subscription
15141514
if(!UPubnubUtilities::EEAddListenerAndSubscribe(Subscription, Callback, this))
15151515
{
1516-
PubnubError("Failed to subscribe to group.");
1516+
PubnubError("[SubscribeToGroup]: Failed to subscribe to channel group.");
1517+
UPubnubUtilities::CallPubnubDelegateWithInvalidArgumentResult(OnSubscribeToGroupResponse, "[SubscribeToGroup]: Failed to subscribe to channel group.");
1518+
QuickActionThread->UnlockAfterSubscriptionOperationFinished();
15171519
return;
15181520
}
15191521

@@ -1524,19 +1526,27 @@ void UPubnubSubsystem::SubscribeToGroup_priv(FString ChannelGroup, FOnSubscribeO
15241526

15251527
void UPubnubSubsystem::UnsubscribeFromChannel_priv(FString Channel, FOnSubscribeOperationResponseNative OnUnsubscribeFromChannelResponse)
15261528
{
1527-
PUBNUB_RETURN_IF_USER_ID_NOT_SET();
1529+
PUBNUB_ENSURE_USER_ID_IS_SET(OnUnsubscribeFromChannelResponse);
1530+
PUBNUB_ENSURE_FIELD_NOT_EMPTY(Channel, OnUnsubscribeFromChannelResponse);
15281531

15291532
CCoreSubscriptionData* SubscriptionData = ChannelSubscriptions.Find(Channel);
15301533
if(!SubscriptionData)
15311534
{
1532-
PubnubError("Failed to unsubscribe from channel. There is no such subscription");
1535+
PubnubError("[UnsubscribeFromChannel]: There is no such subscription. Aborting operation.", EPubnubErrorType::PET_Warning);
1536+
UPubnubUtilities::CallPubnubDelegateWithInvalidArgumentResult(OnUnsubscribeFromChannelResponse, "[UnsubscribeFromChannel]: There is no such subscription. Aborting operation.");
15331537
return;
15341538
}
15351539

1540+
//All subscription related operations are non blocking, so we lock ActionThread manually,
1541+
//make it wait with calling other function until we have subscription result
1542+
QuickActionThread->LockForSubscribeOperation();
1543+
15361544
//Remove subscription listener and unsubscribe with subscription
15371545
if(!UPubnubUtilities::EERemoveListenerAndUnsubscribe(&SubscriptionData->Subscription, SubscriptionData->Callback, this))
15381546
{
1539-
PubnubError("Failed to unsubscribe.");
1547+
PubnubError("[UnsubscribeFromChannel]: Failed to unsubscribe.", EPubnubErrorType::PET_Warning);
1548+
UPubnubUtilities::CallPubnubDelegateWithInvalidArgumentResult(OnUnsubscribeFromChannelResponse, "[UnsubscribeFromChannel]: Failed to unsubscribe.");
1549+
QuickActionThread->UnlockAfterSubscriptionOperationFinished();
15401550
return;
15411551
}
15421552

@@ -1548,19 +1558,27 @@ void UPubnubSubsystem::UnsubscribeFromChannel_priv(FString Channel, FOnSubscribe
15481558

15491559
void UPubnubSubsystem::UnsubscribeFromGroup_priv(FString ChannelGroup, FOnSubscribeOperationResponseNative OnUnsubscribeFromGroupResponse)
15501560
{
1551-
PUBNUB_RETURN_IF_USER_ID_NOT_SET();
1561+
PUBNUB_ENSURE_USER_ID_IS_SET(OnUnsubscribeFromGroupResponse);
1562+
PUBNUB_ENSURE_FIELD_NOT_EMPTY(ChannelGroup, OnUnsubscribeFromGroupResponse);
15521563

15531564
CCoreSubscriptionData* SubscriptionData = ChannelGroupSubscriptions.Find(ChannelGroup);
15541565
if(!SubscriptionData)
15551566
{
1556-
PubnubError("Failed to unsubscribe from channel. There is no such subscription");
1567+
PubnubError("[UnsubscribeFromGroup]: There is no such subscription. Aborting operation.", EPubnubErrorType::PET_Warning);
1568+
UPubnubUtilities::CallPubnubDelegateWithInvalidArgumentResult(OnUnsubscribeFromGroupResponse, "[UnsubscribeFromGroup]: There is no such subscription. Aborting operation.");
15571569
return;
15581570
}
15591571

1572+
//All subscription related operations are non blocking, so we lock ActionThread manually,
1573+
//make it wait with calling other function until we have subscription result
1574+
QuickActionThread->LockForSubscribeOperation();
1575+
15601576
//Remove subscription listener and unsubscribe with subscription
15611577
if(!UPubnubUtilities::EERemoveListenerAndUnsubscribe(&SubscriptionData->Subscription, SubscriptionData->Callback, this))
15621578
{
1563-
PubnubError("Failed to unsubscribe.");
1579+
PubnubError("[UnsubscribeFromGroup]: Failed to unsubscribe.", EPubnubErrorType::PET_Warning);
1580+
UPubnubUtilities::CallPubnubDelegateWithInvalidArgumentResult(OnUnsubscribeFromGroupResponse, "[UnsubscribeFromGroup]: Failed to unsubscribe.");
1581+
QuickActionThread->UnlockAfterSubscriptionOperationFinished();
15641582
return;
15651583
}
15661584

@@ -1573,9 +1591,16 @@ void UPubnubSubsystem::UnsubscribeFromGroup_priv(FString ChannelGroup, FOnSubscr
15731591
void UPubnubSubsystem::UnsubscribeFromAll_priv(FOnSubscribeOperationResponseNative OnUnsubscribeFromAllResponse)
15741592
{
15751593
if(ChannelSubscriptions.IsEmpty() && ChannelGroupSubscriptions.IsEmpty())
1576-
{return;}
1577-
1578-
PUBNUB_RETURN_IF_USER_ID_NOT_SET();
1594+
{
1595+
UPubnubUtilities::CallPubnubDelegate(OnUnsubscribeFromAllResponse, FPubnubOperationResult({200, false, ""}));
1596+
return;
1597+
}
1598+
1599+
PUBNUB_ENSURE_USER_ID_IS_SET(OnUnsubscribeFromAllResponse);
1600+
1601+
//All subscription related operations are non blocking, so we lock ActionThread manually,
1602+
//make it wait with calling other function until we have subscription result
1603+
QuickActionThread->LockForSubscribeOperation();
15791604

15801605
pubnub_unsubscribe_all(ctx_ee);
15811606

0 commit comments

Comments
 (0)