Skip to content

Commit

Permalink
Channel sinks shouldn't produce exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
yallie committed Nov 7, 2024
1 parent eb9d7f8 commit 479b081
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ obj.fx45
/source/TestResults
/source/cov-int
/source/packages
/source/Zyan.Communication/.vs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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;
}
}


/// <summary>
/// Processes the encrypted message.
/// </summary>
Expand All @@ -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)
{
Expand Down

0 comments on commit 479b081

Please sign in to comment.