#region Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved
//-----------------------------------------------------------------------------
// Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved
// Web: https://www.technosoftware.com
//
// The source code in this file is covered under a dual-license scenario:
// - Owner of a purchased license: SCLA 1.0
// - GPL V3: everybody else
//
// SCLA license terms accompanied with this source code.
// See SCLA 1.0: https://technosoftware.com/license/Source_Code_License_Agreement.pdf
//
// GNU General Public License as published by the Free Software Foundation;
// version 3 of the License are accompanied with this source code.
// See https://technosoftware.com/license/GPLv3License.txt
//
// This source code is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
//-----------------------------------------------------------------------------
#endregion Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved
#region Using Directives
using System;
using Technosoftware.OpcRcw.Comn;
#endregion
namespace Technosoftware.DaAeHdaClient.Com
{
///
/// Adds and removes a connection point to a server.
///
internal class ConnectionPoint : IDisposable
{
///
/// The COM server that supports connection points.
///
private IConnectionPoint server_;
///
/// The id assigned to the connection by the COM server.
///
private int cookie_;
///
/// The number of times Advise() has been called without a matching Unadvise().
///
private int refs_;
///
/// Initializes the object by finding the specified connection point.
///
public ConnectionPoint(object server, Guid iid)
{
((IConnectionPointContainer)server).FindConnectionPoint(ref iid, out server_);
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
if (server_ != null)
{
while (Unadvise() > 0)
{
}
try
{
Utilities.Interop.ReleaseServer(server_);
}
catch
{
// Ignore. COM Server probably no longer connected
}
server_ = null;
}
}
///
/// The cookie returned in the advise call.
///
public int Cookie => cookie_;
//=====================================================================
// IConnectionPoint
///
/// Establishes a connection, if necessary and increments the reference count.
///
public int Advise(object callback)
{
if (refs_++ == 0) server_.Advise(callback, out cookie_);
return refs_;
}
///
/// Decrements the reference count and closes the connection if no more references.
///
public int Unadvise()
{
if (--refs_ == 0) server_.Unadvise(cookie_);
return refs_;
}
}
}