Skip to content

Commit

Permalink
Refactored error update emit for realtime channel
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 3, 2023
1 parent 9857a59 commit 0a889a2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
10 changes: 4 additions & 6 deletions src/IO.Ably.Shared/Realtime/ChannelMessageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ public Task<bool> MessageReceived(ProtocolMessage protocolMessage, RealtimeState
channel.Params = new ReadOnlyChannelParams(protocolMessage.Params);
}

if (channel.State == ChannelState.Attached)
// RTL12
if (channel.State == ChannelState.Attached && !protocolMessage.HasFlag(ProtocolMessage.Flag.Resumed))
{
// RTL12
if (!protocolMessage.HasFlag(ProtocolMessage.Flag.Resumed))
{
channel.EmitUpdate(protocolMessage.Error, false, protocolMessage);
}
channel.Presence.ChannelAttached(protocolMessage, false);
channel.EmitErrorUpdate(protocolMessage.Error, false, protocolMessage);
}
else
{
Expand Down
12 changes: 6 additions & 6 deletions src/IO.Ably.Shared/Realtime/Presence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,23 +671,23 @@ private void EnterMembersFromInternalPresenceMap()
{
if (!success)
{
EmitChannelUpdateErrorEvent(item.ClientId, _channel.Name, info.Message);
EmitErrorUpdate(item.ClientId, _channel.Name, info.Message);
}
});
}
catch (AblyException e)
{
EmitChannelUpdateErrorEvent(item.ClientId, _channel.Name, e.ErrorInfo.Message);
EmitErrorUpdate(item.ClientId, _channel.Name, e.ErrorInfo.Message);
}
}

// (RTP17e)
void EmitChannelUpdateErrorEvent(string clientId, string channelName, string errorMessage)
void EmitErrorUpdate(string clientId, string channelName, string errorMessage)
{
var errorString =
$"Cannot automatically re-enter {clientId} on channel {channelName} ({errorMessage})";
Logger.Error(errorString);
_channel.EmitUpdate(new ErrorInfo(errorString, 91004), true);
_channel.EmitErrorUpdate(new ErrorInfo(errorString, 91004), true);
}
}

Expand Down Expand Up @@ -744,10 +744,10 @@ internal void ChannelSuspended(ErrorInfo error)
FailQueuedMessages(error);
}

internal void ChannelAttached(ProtocolMessage attachedMessage, bool isNewAttach = false)
internal void ChannelAttached(ProtocolMessage attachedMessage, bool isAttachWithoutMessageLoss = true)
{
// RTP17f
if (isNewAttach)
if (isAttachWithoutMessageLoss)
{
EnterMembersFromInternalPresenceMap();
}
Expand Down
13 changes: 5 additions & 8 deletions src/IO.Ably.Shared/Realtime/RealtimeChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ private void HandleStateChange(ChannelState state, ErrorInfo error, ProtocolMess
case ChannelState.Attached:
_retryCount = 0;
AttachResume = true;
Presence.ChannelAttached(protocolMessage, previousState != ChannelState.Attached);
Presence.ChannelAttached(protocolMessage);
break;
case ChannelState.Detached:
/* RTL13a check for unexpected detach */
Expand Down Expand Up @@ -823,15 +823,12 @@ private void SendMessage(ProtocolMessage protocolMessage, Action<bool, ErrorInfo
ConnectionManager.Send(protocolMessage, callback, Options);
}

internal void EmitUpdate(ErrorInfo errorInfo, bool resumed, ProtocolMessage message = null)
internal void EmitErrorUpdate(ErrorInfo errorInfo, bool resumed, ProtocolMessage message = null)
{
if (State == ChannelState.Attached)
Emit(ChannelEvent.Update, new ChannelStateChange(ChannelEvent.Update, State, State, errorInfo, resumed)
{
Emit(ChannelEvent.Update, new ChannelStateChange(ChannelEvent.Update, State, State, errorInfo, resumed)
{
ProtocolMessage = message,
});
}
ProtocolMessage = message,
});
}

internal bool ShouldReAttach(ChannelOptions options)
Expand Down

0 comments on commit 0a889a2

Please sign in to comment.