Skip to content

Commit

Permalink
Try your best not to fail event dispatch even if the client had power…
Browse files Browse the repository at this point in the history
… failure, close #72.
  • Loading branch information
yallie committed Nov 21, 2024
1 parent 4c9f9d6 commit abf3ce5
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions CoreRemoting/RemotingSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public sealed class RemotingSession : IDisposable
/// Event: Fired before the session is disposed to do some clean up.
/// </summary>
public event Action BeforeDispose;

#endregion

#region Construction
Expand Down Expand Up @@ -70,7 +70,7 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se
_delegateProxyCache = new ConcurrentDictionary<Guid, IDelegateProxy>();
_rawMessageTransport = rawMessageTransport ?? throw new ArgumentNullException(nameof(rawMessageTransport));
_clientPublicKeyBlob = clientPublicKey;

_rawMessageTransport.ReceiveMessage += OnReceiveMessage;
_rawMessageTransport.ErrorOccured += OnErrorOccured;

Expand Down Expand Up @@ -122,6 +122,7 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se
_remoteDelegateInvocationEventAggregator.RemoteDelegateInvocationNeeded +=
(_, uniqueCallKey, handlerKey, arguments) =>
{
// handle graceful client disconnection
if (_isDisposing)
return null;

Expand All @@ -147,9 +148,19 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se
keyPair: _keyPair,
messageType: "invoke");

// Invoke remote delegate on client
_rawMessageTransport?.SendMessage(
_server.Serializer.Serialize(remoteDelegateInvocationWebsocketMessage));
try
{
// Invoke remote delegate on client
_rawMessageTransport?.SendMessage(
_server.Serializer.Serialize(remoteDelegateInvocationWebsocketMessage));
}
catch (Exception ex)
{
// handle unexpected client disconnection
OnErrorOccured("Failed to dispatch the remote event. " +
$"Session: {SessionId}, Unique call key: {uniqueCallKey}, " +
$"Handler key: {handlerKey}", ex);
}

return null;
};
Expand All @@ -165,7 +176,7 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se
private void OnErrorOccured(string errorMessage, Exception ex)
{
var exception = new RemotingException(errorMessage, innerEx: ex);

((RemotingServer)_server).OnError(exception);
}

Expand Down Expand Up @@ -708,7 +719,7 @@ private bool MapLinqExpressionArgument(Type argumentType, object argument, out o

return true;
}

#endregion

#region Close session
Expand Down

0 comments on commit abf3ce5

Please sign in to comment.