From 479b081f745049f9545827ce82411fa3841f64d4 Mon Sep 17 00:00:00 2001 From: yallie Date: Thu, 7 Nov 2024 19:55:54 +0300 Subject: [PATCH] Channel sinks shouldn't produce exceptions. --- .gitignore | 1 + .../Encryption/CryptoClientChannelSink.cs | 38 ++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 3ddab9f4..9c0ce722 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ obj.fx45 /source/TestResults /source/cov-int /source/packages +/source/Zyan.Communication/.vs \ No newline at end of file diff --git a/source/Zyan.Communication/ChannelSinks/Encryption/CryptoClientChannelSink.cs b/source/Zyan.Communication/ChannelSinks/Encryption/CryptoClientChannelSink.cs index a9dcda12..f0838eb5 100644 --- a/source/Zyan.Communication/ChannelSinks/Encryption/CryptoClientChannelSink.cs +++ b/source/Zyan.Communication/ChannelSinks/Encryption/CryptoClientChannelSink.cs @@ -3,6 +3,7 @@ using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Messaging; using System.Security.Cryptography; +using Zyan.Communication.Toolbox.Diagnostics; namespace Zyan.Communication.ChannelSinks.Encryption { @@ -101,11 +102,18 @@ private SymmetricAlgorithm ObtainSharedKey(IMessage msg) // Generate a request for the shared key CreateSharedKeyRequest(requestHeaders); - // Send the request to the next sink to communicate with the CryptoServerChannelSink across the wire - _next.ProcessMessage(msg, requestHeaders, requestStream, out responseHeaders, out responseStream); + // Retry as needed + for (var i = 0; i < _maxAttempts; i++) + { + // Send the request to the next sink to communicate with the CryptoServerChannelSink across the wire + if (NextSinkProcessMessage(msg, requestHeaders, requestStream, out responseHeaders, out responseStream)) + { + // Process server's response + return ProcessSharedKeyResponse(responseHeaders); + } + } - // Process server's response - return ProcessSharedKeyResponse(responseHeaders); + throw new CryptoRemotingException(DEFAULT_EXCEPTION_TEXT); } /// @@ -198,6 +206,26 @@ private Stream EncryptMessage(ITransportHeaders requestHeaders, Stream requestSt return requestStream; } + private bool NextSinkProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, out ITransportHeaders responseHeaders, out Stream responseStream) + { + try + { + // Pass the encrypted message to the next sink + _next.ProcessMessage(msg, requestHeaders, requestStream, out responseHeaders, out responseStream); + return true; + } + catch (Exception ex) + { + Trace.WriteLine("Exception in CryptoClientChannelSink: {0}", ex.ToString()); + + // ProcessMessage shouldn't throw exceptions + responseStream = null; + responseHeaders = null; + return false; + } + } + + /// /// Processes the encrypted message. /// @@ -220,7 +248,7 @@ private bool ProcessEncryptedMessage(IMessage msg, ITransportHeaders requestHead } // Pass the encrypted message to the next sink - _next.ProcessMessage(msg, requestHeaders, requestStream, out responseHeaders, out responseStream); + NextSinkProcessMessage(msg, requestHeaders, requestStream, out responseHeaders, out responseStream); lock (_lockObject) {