From 0a8386e9b4ae7c91264a10ea4dbede8703bd0fbf Mon Sep 17 00:00:00 2001 From: Marc Schier Date: Fri, 29 Nov 2024 11:15:07 +0100 Subject: [PATCH 1/3] Add OnTokenActivated event for token change notifications Added a new event `OnTokenActivated` to the `UaChannelBase` class in the `Opc.Ua` namespace to notify when a channel token is activated. Implemented this event in the `HttpsTransportChannel` class within the `Opc.Ua.Bindings` namespace. Introduced a protected internal `OnTokenActivated` event handler in the `UaSCBinaryChannel.Symmetric` class, invoked during token changes in the `ActivateToken` and `DiscardTokens` methods. Cleaned up the event handler in the `Dispose` method of the `UaSCBinaryClientChannel` class. Registered the event handler with the internal channel in the `UaSCBinaryTransportChannel` class. Added a new delegate `ChannelTokenActivatedEventHandler` in the `ITransportChannel` interface and updated the interface to include the `OnTokenActivated` event. Made minor formatting and comment adjustments for improved readability and consistency. --- Stack/Opc.Ua.Core/Stack/Client/UaChannelBase.cs | 9 ++++++++- .../Stack/Https/HttpsTransportChannel.cs | 7 +++++++ .../Stack/Tcp/UaSCBinaryChannel.Symmetric.cs | 9 +++++++++ .../Stack/Tcp/UaSCBinaryClientChannel.cs | 16 +++++++++------- .../Stack/Tcp/UaSCBinaryTransportChannel.cs | 13 +++++++++++++ .../Stack/Transport/ITransportChannel.cs | 16 ++++++++++++++-- 6 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Stack/Opc.Ua.Core/Stack/Client/UaChannelBase.cs b/Stack/Opc.Ua.Core/Stack/Client/UaChannelBase.cs index 1442a4738f..4d7e888c14 100644 --- a/Stack/Opc.Ua.Core/Stack/Client/UaChannelBase.cs +++ b/Stack/Opc.Ua.Core/Stack/Client/UaChannelBase.cs @@ -198,6 +198,13 @@ public IServiceMessageContext MessageContext /// public ChannelToken CurrentToken => null; + /// + public event ChannelTokenActivatedEventHandler OnTokenActivated + { + add { } + remove { } + } + /// /// Gets or sets the default timeout for requests send via the channel. /// @@ -685,7 +692,7 @@ private void OnSendRequest(object state) #endregion } #endregion - + /// /// Processes the request. /// diff --git a/Stack/Opc.Ua.Core/Stack/Https/HttpsTransportChannel.cs b/Stack/Opc.Ua.Core/Stack/Https/HttpsTransportChannel.cs index 387f224532..1d82412788 100644 --- a/Stack/Opc.Ua.Core/Stack/Https/HttpsTransportChannel.cs +++ b/Stack/Opc.Ua.Core/Stack/Https/HttpsTransportChannel.cs @@ -108,6 +108,13 @@ public void Dispose() /// public ChannelToken CurrentToken => null; + /// + public event ChannelTokenActivatedEventHandler OnTokenActivated + { + add { } + remove { } + } + /// public int OperationTimeout { diff --git a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs index 8d91e253af..acd6bdf423 100644 --- a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs +++ b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs @@ -36,6 +36,11 @@ public partial class UaSCUaBinaryChannel /// protected ChannelToken RenewedToken => m_renewedToken; + /// + /// Called when the token changes + /// + protected internal ChannelTokenActivatedEventHandler OnTokenActivated { get; set; } + /// /// Creates a new token. /// @@ -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); } @@ -85,6 +92,8 @@ protected void DiscardTokens() { m_previousToken = null; m_currentToken = null; + + OnTokenActivated?.Invoke(null, null); } #endregion diff --git a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs index 9c6915cece..43c9bca243 100644 --- a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs +++ b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryClientChannel.cs @@ -104,6 +104,8 @@ protected override void Dispose(bool disposing) if (disposing) { + OnTokenActivated = null; + Utils.SilentDispose(m_handshakeTimer); m_handshakeTimer = null; } @@ -504,7 +506,7 @@ private bool ProcessAcknowledgeMessage(ArraySegment messageChunk) decoder.Close(); } - + // ready to open the channel. State = TcpChannelState.Opening; @@ -583,7 +585,7 @@ private bool ProcessOpenSecureChannelResponse(uint messageType, ArraySegment - /// Creates an object to manage the state of an asynchronous operation. + /// Creates an object to manage the state of an asynchronous operation. /// private WriteOperation BeginOperation(int timeout, AsyncCallback callback, object state) { @@ -1421,7 +1423,7 @@ protected bool ProcessErrorMessage(uint messageType, ArraySegment messageC { ServiceResult error; - // read request buffer sizes. + // read request buffer sizes. using (var decoder = new BinaryDecoder(messageChunk, Quotas.MessageContext)) { ReadAndVerifyMessageTypeAndSize(decoder, TcpMessageType.Error, messageChunk.Count); diff --git a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs index b7a76f64f1..534abc00d5 100644 --- a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs +++ b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs @@ -70,6 +70,16 @@ public IMessageSocket Socket #endregion #region ITransportChannel Members + + /// + /// Called when the token changes + /// + public event ChannelTokenActivatedEventHandler OnTokenActivated + { + add => m_OnTokenActivated += value; + remove => m_OnTokenActivated -= value; + } + /// /// A masking indicating which features are implemented. /// @@ -482,6 +492,8 @@ private UaSCUaBinaryClientChannel CreateChannel(ITransportWaitingConnection conn channel.ReverseSocket = true; } + // Register the token changed event handler with the internal channel + channel.OnTokenActivated = m_OnTokenActivated; return channel; } #endregion @@ -494,6 +506,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 } diff --git a/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs b/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs index cc02a3503a..c5f9a65ffe 100644 --- a/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs +++ b/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs @@ -18,7 +18,14 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. namespace Opc.Ua { /// - /// This is an interface to a channel which supports + /// Callback when the token is activated + /// + /// + /// + public delegate void ChannelTokenActivatedEventHandler(ChannelToken currentToken, ChannelToken previousToken); + + /// + /// This is an interface to a channel which supports /// public interface ITransportChannel : IDisposable { @@ -47,6 +54,11 @@ public interface ITransportChannel : IDisposable /// ChannelToken CurrentToken { get; } + /// + /// Register for token change events + /// + event ChannelTokenActivatedEventHandler OnTokenActivated; + /// /// Gets or sets the default timeout for requests send via the channel. /// @@ -200,7 +212,7 @@ IAsyncResult BeginOpen( /// /// Completes an asynchronous operation to send a request over the secure channel. - /// Awaitable version + /// Awaitable version /// /// The result returned from the BeginSendRequest call. /// The cancellation token. From 3a9bedbf68e3b320960a764170f51b1edcf8b4a7 Mon Sep 17 00:00:00 2001 From: Marc Schier Date: Mon, 2 Dec 2024 08:22:10 +0100 Subject: [PATCH 2/3] Include channel in callback --- Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs | 2 +- Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs | 3 ++- Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs index acd6bdf423..7dc0bda84b 100644 --- a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs +++ b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryChannel.Symmetric.cs @@ -39,7 +39,7 @@ public partial class UaSCUaBinaryChannel /// /// Called when the token changes /// - protected internal ChannelTokenActivatedEventHandler OnTokenActivated { get; set; } + protected internal Action OnTokenActivated { get; set; } /// /// Creates a new token. diff --git a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs index 534abc00d5..a56152a94b 100644 --- a/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs +++ b/Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs @@ -493,7 +493,8 @@ private UaSCUaBinaryClientChannel CreateChannel(ITransportWaitingConnection conn } // Register the token changed event handler with the internal channel - channel.OnTokenActivated = m_OnTokenActivated; + channel.OnTokenActivated = + (current, previous) => m_OnTokenActivated?.Invoke(this, current, previous); return channel; } #endregion diff --git a/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs b/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs index c5f9a65ffe..5880861f78 100644 --- a/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs +++ b/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs @@ -20,9 +20,13 @@ namespace Opc.Ua /// /// Callback when the token is activated /// + /// /// /// - public delegate void ChannelTokenActivatedEventHandler(ChannelToken currentToken, ChannelToken previousToken); + public delegate void ChannelTokenActivatedEventHandler( + ITransportChannel source, + ChannelToken currentToken, + ChannelToken previousToken); /// /// This is an interface to a channel which supports From c0ee6da6a82ccfbd4769c6b8e22cb91264d5b6d9 Mon Sep 17 00:00:00 2001 From: Marc Schier Date: Mon, 2 Dec 2024 08:39:34 +0100 Subject: [PATCH 3/3] Rename parameter --- Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs b/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs index 5880861f78..ad7ae429f9 100644 --- a/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs +++ b/Stack/Opc.Ua.Core/Stack/Transport/ITransportChannel.cs @@ -20,11 +20,11 @@ namespace Opc.Ua /// /// Callback when the token is activated /// - /// + /// /// /// public delegate void ChannelTokenActivatedEventHandler( - ITransportChannel source, + ITransportChannel channel, ChannelToken currentToken, ChannelToken previousToken);