Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register callback to notify about new channel token activation #2872

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Stack/Opc.Ua.Core/Stack/Client/UaChannelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ public IServiceMessageContext MessageContext
/// </summary>
public ChannelToken CurrentToken => null;

/// <inheritdoc/>
public event ChannelTokenActivatedEventHandler OnTokenActivated
{
add { }
remove { }
}

/// <summary>
/// Gets or sets the default timeout for requests send via the channel.
/// </summary>
Expand Down Expand Up @@ -685,7 +692,7 @@ private void OnSendRequest(object state)
#endregion
}
#endregion

/// <summary>
/// Processes the request.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions Stack/Opc.Ua.Core/Stack/Https/HttpsTransportChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ public void Dispose()
/// <inheritdoc/>
public ChannelToken CurrentToken => null;

/// <inheritdoc/>
public event ChannelTokenActivatedEventHandler OnTokenActivated
{
add { }
remove { }
}

/// <inheritdoc/>
public int OperationTimeout
{
Expand Down
9 changes: 9 additions & 0 deletions Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public partial class UaSCUaBinaryChannel
/// </summary>
protected ChannelToken RenewedToken => m_renewedToken;

/// <summary>
/// Called when the token changes
/// </summary>
protected internal Action<ChannelToken, ChannelToken> OnTokenActivated { get; set; }

/// <summary>
/// Creates a new token.
/// </summary>
Expand Down Expand Up @@ -66,6 +71,8 @@ protected void ActivateToken(ChannelToken token)
m_currentToken = token;
m_renewedToken = null;

OnTokenActivated?.Invoke(token, m_previousToken);

Utils.LogInfo("ChannelId {0}: Token #{1} activated. CreatedAt={2:HH:mm:ss.fff}. Lifetime={3}.", Id, token.TokenId, token.CreatedAt, token.Lifetime);
}

Expand All @@ -85,6 +92,8 @@ protected void DiscardTokens()
{
m_previousToken = null;
m_currentToken = null;

OnTokenActivated?.Invoke(null, null);
}
#endregion

Expand Down
16 changes: 9 additions & 7 deletions Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@

if (disposing)
{
OnTokenActivated = null;

Utils.SilentDispose(m_handshakeTimer);
m_handshakeTimer = null;
}
Expand Down Expand Up @@ -504,7 +506,7 @@
decoder.Close();
}


// ready to open the channel.
State = TcpChannelState.Opening;

Expand Down Expand Up @@ -583,7 +585,7 @@
{
Utils.LogTrace("ChannelId {0}: ProcessOpenSecureChannelResponse()", ChannelId);

// validate the channel state.
// validate the channel state.
if (State != TcpChannelState.Opening && State != TcpChannelState.Open)
{
ForceReconnect(ServiceResult.Create(StatusCodes.BadTcpMessageTypeInvalid, "Server sent an unexpected OpenSecureChannel response."));
Expand Down Expand Up @@ -645,9 +647,9 @@
if (!(ParseResponse(chunksToProcess) is OpenSecureChannelResponse response))
{
throw ServiceResultException.Create(StatusCodes.BadTypeMismatch, "Server did not return a valid OpenSecureChannelResponse.");
}

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Configuration

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Configuration

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Security.Certificates

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Security.Certificates

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Client.ComplexTypes

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Core

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Core

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Gds

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-PubSub

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Server

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 650 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-Client

The behavior of 'string.Format(string, object)' could vary based on the current user's locale settings. Replace this call in 'UaSCUaBinaryClientChannel.ProcessOpenSecureChannelResponse(uint, ArraySegment<byte>)' with a call to 'string.Format(IFormatProvider, string, params object[])'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

// the client needs to use the creation time assigned when it sent
// the client needs to use the creation time assigned when it sent
// the request and ignores the creation time in the response because
// the server and client clocks may not be synchronized.

Expand Down Expand Up @@ -685,7 +687,7 @@
State = TcpChannelState.Open;
m_reconnecting = false;

// enable reconnects. DO NOT USE!
// enable reconnects. DO NOT USE!
// m_waitBetweenReconnects = TcpMessageLimits.MinTimeBetweenReconnects;
m_waitBetweenReconnects = Timeout.Infinite;

Expand Down Expand Up @@ -1164,7 +1166,7 @@
Socket = null;
}

// set the state.
// set the state.
ChannelStateChanged(TcpChannelState.Closed, reason);
}
}
Expand Down Expand Up @@ -1281,7 +1283,7 @@
}

/// <summary>
/// Creates an object to manage the state of an asynchronous operation.
/// Creates an object to manage the state of an asynchronous operation.
/// </summary>
private WriteOperation BeginOperation(int timeout, AsyncCallback callback, object state)
{
Expand Down Expand Up @@ -1421,7 +1423,7 @@
{
ServiceResult error;

// read request buffer sizes.
// read request buffer sizes.
using (var decoder = new BinaryDecoder(messageChunk, Quotas.MessageContext))
{
ReadAndVerifyMessageTypeAndSize(decoder, TcpMessageType.Error, messageChunk.Count);
Expand Down
14 changes: 14 additions & 0 deletions Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public IMessageSocket Socket
#endregion

#region ITransportChannel Members

/// <summary>
/// Called when the token changes
/// </summary>
public event ChannelTokenActivatedEventHandler OnTokenActivated
{
add => m_OnTokenActivated += value;
remove => m_OnTokenActivated -= value;
}

/// <summary>
/// A masking indicating which features are implemented.
/// </summary>
Expand Down Expand Up @@ -482,6 +492,9 @@ private UaSCUaBinaryClientChannel CreateChannel(ITransportWaitingConnection conn
channel.ReverseSocket = true;
}

// Register the token changed event handler with the internal channel
channel.OnTokenActivated =
(current, previous) => m_OnTokenActivated?.Invoke(this, current, previous);
return channel;
}
#endregion
Expand All @@ -494,6 +507,7 @@ private UaSCUaBinaryClientChannel CreateChannel(ITransportWaitingConnection conn
private ChannelQuotas m_quotas;
private BufferManager m_bufferManager;
private UaSCUaBinaryClientChannel m_channel;
private event ChannelTokenActivatedEventHandler m_OnTokenActivated;
private IMessageSocketFactory m_messageSocketFactory;
#endregion
}
Expand Down
20 changes: 18 additions & 2 deletions Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
namespace Opc.Ua
{
/// <summary>
/// This is an interface to a channel which supports
/// Callback when the token is activated
/// </summary>
/// <param name="channel"></param>
/// <param name="currentToken"></param>
/// <param name="previousToken"></param>
public delegate void ChannelTokenActivatedEventHandler(
ITransportChannel channel,
ChannelToken currentToken,
ChannelToken previousToken);

/// <summary>
/// This is an interface to a channel which supports
/// </summary>
public interface ITransportChannel : IDisposable
{
Expand Down Expand Up @@ -47,6 +58,11 @@ public interface ITransportChannel : IDisposable
/// </summary>
ChannelToken CurrentToken { get; }

/// <summary>
/// Register for token change events
/// </summary>
event ChannelTokenActivatedEventHandler OnTokenActivated;

/// <summary>
/// Gets or sets the default timeout for requests send via the channel.
/// </summary>
Expand Down Expand Up @@ -200,7 +216,7 @@ IAsyncResult BeginOpen(

/// <summary>
/// Completes an asynchronous operation to send a request over the secure channel.
/// Awaitable version
/// Awaitable version
/// </summary>
/// <param name="result">The result returned from the BeginSendRequest call.</param>
/// <param name="ct">The cancellation token.</param>
Expand Down
Loading