Files
UA-.NETStandard/SampleApplications/SDK/Client/SessionReconnectHandler.cs
T

224 lines
7.3 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2016 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Opc.Ua;
namespace Opc.Ua.Client
{
/// <summary>
/// Attempts to reconnect to the server.
/// </summary>
public class SessionReconnectHandler : IDisposable
{
#region IDisposable Members
/// <summary>
/// Frees any unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// An overrideable version of the Dispose.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
lock (m_lock)
{
if (m_reconnectTimer != null)
{
m_reconnectTimer.Dispose();
m_reconnectTimer = null;
}
}
}
}
#endregion
#region Public Methods
/// <summary>
/// Gets the session managed by the handler.
/// </summary>
/// <value>The session.</value>
public Session Session
{
get { return m_session; }
}
/// <summary>
/// Begins the reconnect process.
/// </summary>
public void BeginReconnect(Session session, int reconnectPeriod, EventHandler callback)
{
lock (m_lock)
{
if (m_reconnectTimer != null)
{
throw new ServiceResultException(StatusCodes.BadInvalidState);
}
m_session = session;
m_reconnectFailed = false;
m_reconnectPeriod = reconnectPeriod;
m_callback = callback;
m_reconnectTimer = new System.Threading.Timer(OnReconnect, null, reconnectPeriod, Timeout.Infinite);
}
}
#endregion
#region Private Methods
/// <summary>
/// Called when the reconnect timer expires.
/// </summary>
private void OnReconnect(object state)
{
try
{
// check for exit.
if (m_reconnectTimer == null)
{
return;
}
// dispose of the timer.
lock (m_lock)
{
if (m_reconnectTimer != null)
{
m_reconnectTimer.Dispose();
m_reconnectTimer = null;
}
}
// do the reconnect.
if (DoReconnect())
{
// notify the caller.
m_callback(this, null);
return;
}
}
catch (Exception exception)
{
Utils.Trace(exception, "Unexpected error during reconnect.");
}
// schedule the next reconnect.
lock (m_lock)
{
m_reconnectTimer = new System.Threading.Timer(OnReconnect, null, m_reconnectPeriod, Timeout.Infinite);
}
}
/// <summary>
/// Reconnects to the server.
/// </summary>
private bool DoReconnect()
{
// try a reconnect.
if (!m_reconnectFailed)
{
try
{
m_session.Reconnect();
// monitored items should start updating on their own.
return true;
}
catch (Exception exception)
{
bool recreateNow = false;
// recreate the session if it has been closed or the nonce is wrong.
ServiceResultException sre = exception as ServiceResultException;
if (sre != null)
{
switch (sre.StatusCode)
{
case StatusCodes.BadSessionClosed:
case StatusCodes.BadApplicationSignatureInvalid:
{
recreateNow = true;
break;
}
default:
{
Utils.Trace((int)Utils.TraceMasks.Error, "Unexpected RECONNECT error code. {0}", sre.StatusCode);
break;
}
}
}
m_reconnectFailed = true;
// try a reconnect again after a delay.
if (!recreateNow)
{
Utils.Trace("Reconnect failed. {0}", exception.Message);
return false;
}
}
}
// re-create the session.
try
{
Session session = Session.Recreate(m_session);
m_session.Close();
m_session = session;
return true;
}
catch (Exception exception)
{
Utils.Trace("Unexpected re-creating a Session with the UA Server. {0}", exception.Message);
return false;
}
}
#endregion
#region Private Fields
private object m_lock = new object();
private Session m_session;
private bool m_reconnectFailed;
private int m_reconnectPeriod;
private Timer m_reconnectTimer;
private EventHandler m_callback;
#endregion
}
}