Clean
This commit is contained in:
@@ -22,14 +22,12 @@
|
||||
|
||||
#region Using Directives
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Collections;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Technosoftware.OpcRcw.Comn;
|
||||
using Technosoftware.DaAeHdaClient.Ae;
|
||||
using Technosoftware.DaAeHdaClient.Da;
|
||||
using Technosoftware.DaAeHdaClient.Hda;
|
||||
using Technosoftware.OpcRcw.Comn;
|
||||
#endregion
|
||||
|
||||
namespace Technosoftware.DaAeHdaClient.Com
|
||||
@@ -38,192 +36,192 @@ namespace Technosoftware.DaAeHdaClient.Com
|
||||
/// A unique identifier for the result of an operation of an item.
|
||||
/// </summary>
|
||||
public class ServerEnumerator : IOpcDiscovery
|
||||
{
|
||||
//======================================================================
|
||||
// IDisposable
|
||||
{
|
||||
//======================================================================
|
||||
// IDisposable
|
||||
|
||||
/// <summary>
|
||||
/// Frees all unmanaged resources
|
||||
/// </summary>
|
||||
public void Dispose() {}
|
||||
/// <summary>
|
||||
/// Frees all unmanaged resources
|
||||
/// </summary>
|
||||
public void Dispose() { }
|
||||
|
||||
//======================================================================
|
||||
// IDiscovery
|
||||
//======================================================================
|
||||
// IDiscovery
|
||||
|
||||
/// <summary>
|
||||
/// Enumerates hosts that may be accessed for server discovery.
|
||||
/// </summary>
|
||||
public string[] EnumerateHosts()
|
||||
{
|
||||
return Interop.EnumComputers();
|
||||
}
|
||||
/// <summary>
|
||||
/// Enumerates hosts that may be accessed for server discovery.
|
||||
/// </summary>
|
||||
public string[] EnumerateHosts()
|
||||
{
|
||||
return Interop.EnumComputers();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of servers that support the specified interface specification.
|
||||
/// </summary>
|
||||
public OpcServer[] GetAvailableServers(OpcSpecification specification)
|
||||
{
|
||||
return GetAvailableServers(specification, null, null);
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a list of servers that support the specified interface specification.
|
||||
/// </summary>
|
||||
public OpcServer[] GetAvailableServers(OpcSpecification specification)
|
||||
{
|
||||
return GetAvailableServers(specification, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of servers that support the specified specification on the specified host.
|
||||
/// </summary>
|
||||
public OpcServer[] GetAvailableServers(OpcSpecification specification, string host, OpcConnectData connectData)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
var credentials = (connectData != null)?connectData.GetCredential(null, null):null;
|
||||
/// <summary>
|
||||
/// Returns a list of servers that support the specified specification on the specified host.
|
||||
/// </summary>
|
||||
public OpcServer[] GetAvailableServers(OpcSpecification specification, string host, OpcConnectData connectData)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
var credentials = (connectData != null) ? connectData.GetCredential(null, null) : null;
|
||||
|
||||
// connect to the server.
|
||||
m_server = (IOPCServerList2)Interop.CreateInstance(CLSID, host, credentials, connectData?.UseConnectSecurity ?? false);
|
||||
m_host = host;
|
||||
// connect to the server.
|
||||
m_server = (IOPCServerList2)Interop.CreateInstance(CLSID, host, credentials, connectData?.UseConnectSecurity ?? false);
|
||||
m_host = host;
|
||||
|
||||
try
|
||||
{
|
||||
var servers = new ArrayList();
|
||||
|
||||
// convert the interface version to a guid.
|
||||
var catid = new Guid(specification.Id);
|
||||
|
||||
// get list of servers in the specified specification.
|
||||
IOPCEnumGUID enumerator = null;
|
||||
try
|
||||
{
|
||||
var servers = new ArrayList();
|
||||
|
||||
m_server.EnumClassesOfCategories(
|
||||
1,
|
||||
new Guid[] { catid },
|
||||
0,
|
||||
null,
|
||||
out enumerator);
|
||||
// convert the interface version to a guid.
|
||||
var catid = new Guid(specification.Id);
|
||||
|
||||
// read clsids.
|
||||
var clsids = ReadClasses(enumerator);
|
||||
// get list of servers in the specified specification.
|
||||
IOPCEnumGUID enumerator = null;
|
||||
|
||||
m_server.EnumClassesOfCategories(
|
||||
1,
|
||||
new Guid[] { catid },
|
||||
0,
|
||||
null,
|
||||
out enumerator);
|
||||
|
||||
// read clsids.
|
||||
var clsids = ReadClasses(enumerator);
|
||||
|
||||
// release enumerator object.
|
||||
Interop.ReleaseServer(enumerator);
|
||||
enumerator = null;
|
||||
enumerator = null;
|
||||
|
||||
// fetch class descriptions.
|
||||
foreach (var clsid in clsids)
|
||||
{
|
||||
var factory = new Factory();
|
||||
// fetch class descriptions.
|
||||
foreach (var clsid in clsids)
|
||||
{
|
||||
var factory = new Factory();
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = CreateUrl(specification, clsid);
|
||||
|
||||
OpcServer server = null;
|
||||
OpcServer server = null;
|
||||
|
||||
if (specification == OpcSpecification.OPC_DA_30)
|
||||
{
|
||||
server = new TsCDaServer(factory, url);
|
||||
}
|
||||
if (specification == OpcSpecification.OPC_DA_30)
|
||||
{
|
||||
server = new TsCDaServer(factory, url);
|
||||
}
|
||||
|
||||
else if (specification == OpcSpecification.OPC_DA_20)
|
||||
{
|
||||
server = new TsCDaServer(factory, url);
|
||||
}
|
||||
|
||||
else if (specification == OpcSpecification.OPC_AE_10)
|
||||
{
|
||||
server = new TsCAeServer(factory, url);
|
||||
}
|
||||
else if (specification == OpcSpecification.OPC_DA_20)
|
||||
{
|
||||
server = new TsCDaServer(factory, url);
|
||||
}
|
||||
|
||||
else if (specification == OpcSpecification.OPC_HDA_10)
|
||||
{
|
||||
server = new TsCHdaServer(factory, url);
|
||||
}
|
||||
|
||||
servers.Add(server);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignore bad clsids.
|
||||
}
|
||||
}
|
||||
else if (specification == OpcSpecification.OPC_AE_10)
|
||||
{
|
||||
server = new TsCAeServer(factory, url);
|
||||
}
|
||||
|
||||
return (OpcServer[])servers.ToArray(typeof(OpcServer));
|
||||
}
|
||||
finally
|
||||
{
|
||||
// free the server.
|
||||
Interop.ReleaseServer(m_server);
|
||||
m_server = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up the CLSID for the specified prog id on a remote host.
|
||||
/// </summary>
|
||||
public Guid CLSIDFromProgID(string progID, string host, OpcConnectData connectData)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
var credentials = (connectData != null)?connectData.GetCredential(null, null):null;
|
||||
else if (specification == OpcSpecification.OPC_HDA_10)
|
||||
{
|
||||
server = new TsCHdaServer(factory, url);
|
||||
}
|
||||
|
||||
servers.Add(server);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignore bad clsids.
|
||||
}
|
||||
}
|
||||
|
||||
return (OpcServer[])servers.ToArray(typeof(OpcServer));
|
||||
}
|
||||
finally
|
||||
{
|
||||
// free the server.
|
||||
Interop.ReleaseServer(m_server);
|
||||
m_server = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up the CLSID for the specified prog id on a remote host.
|
||||
/// </summary>
|
||||
public Guid CLSIDFromProgID(string progID, string host, OpcConnectData connectData)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
var credentials = (connectData != null) ? connectData.GetCredential(null, null) : null;
|
||||
|
||||
// connect to the server.
|
||||
m_server = (IOPCServerList2)Interop.CreateInstance(CLSID, host, credentials, connectData?.UseConnectSecurity ?? false);
|
||||
m_host = host;
|
||||
m_host = host;
|
||||
|
||||
// lookup prog id.
|
||||
Guid clsid;
|
||||
// lookup prog id.
|
||||
Guid clsid;
|
||||
|
||||
try
|
||||
{
|
||||
m_server.CLSIDFromProgID(progID, out clsid);
|
||||
}
|
||||
catch
|
||||
{
|
||||
clsid = Guid.Empty;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Interop.ReleaseServer(m_server);
|
||||
m_server = null;
|
||||
}
|
||||
try
|
||||
{
|
||||
m_server.CLSIDFromProgID(progID, out clsid);
|
||||
}
|
||||
catch
|
||||
{
|
||||
clsid = Guid.Empty;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Interop.ReleaseServer(m_server);
|
||||
m_server = null;
|
||||
}
|
||||
|
||||
// return empty guid if prog id not found.
|
||||
return clsid;
|
||||
}
|
||||
}
|
||||
// return empty guid if prog id not found.
|
||||
return clsid;
|
||||
}
|
||||
}
|
||||
|
||||
//======================================================================
|
||||
// Private Members
|
||||
//======================================================================
|
||||
// Private Members
|
||||
|
||||
/// <summary>
|
||||
/// The server enumerator COM server.
|
||||
/// </summary>
|
||||
private IOPCServerList2 m_server = null;
|
||||
/// <summary>
|
||||
/// The server enumerator COM server.
|
||||
/// </summary>
|
||||
private IOPCServerList2 m_server = null;
|
||||
|
||||
/// <summary>
|
||||
/// The host where the servers are being enumerated.
|
||||
/// </summary>
|
||||
private string m_host = null;
|
||||
/// <summary>
|
||||
/// The host where the servers are being enumerated.
|
||||
/// </summary>
|
||||
private string m_host = null;
|
||||
|
||||
/// <summary>
|
||||
/// The ProgID for the OPC Server Enumerator.
|
||||
/// </summary>
|
||||
private const string ProgID = "OPC.ServerList.1";
|
||||
|
||||
/// <summary>
|
||||
/// The CLSID for the OPC Server Enumerator.
|
||||
/// </summary>
|
||||
private static readonly Guid CLSID = new Guid("13486D51-4821-11D2-A494-3CB306C10000");
|
||||
/// <summary>
|
||||
/// The ProgID for the OPC Server Enumerator.
|
||||
/// </summary>
|
||||
private const string ProgID = "OPC.ServerList.1";
|
||||
|
||||
//======================================================================
|
||||
// Private Methods
|
||||
/// <summary>
|
||||
/// The CLSID for the OPC Server Enumerator.
|
||||
/// </summary>
|
||||
private static readonly Guid CLSID = new Guid("13486D51-4821-11D2-A494-3CB306C10000");
|
||||
|
||||
/// <summary>
|
||||
/// Reads the guids from the enumerator.
|
||||
/// </summary>
|
||||
private Guid[] ReadClasses(IOPCEnumGUID enumerator)
|
||||
{
|
||||
var guids = new ArrayList();
|
||||
//======================================================================
|
||||
// Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// Reads the guids from the enumerator.
|
||||
/// </summary>
|
||||
private Guid[] ReadClasses(IOPCEnumGUID enumerator)
|
||||
{
|
||||
var guids = new ArrayList();
|
||||
var count = 10;
|
||||
|
||||
// create buffer.
|
||||
var buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))*count);
|
||||
var buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * count);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -257,64 +255,64 @@ namespace Technosoftware.DaAeHdaClient.Com
|
||||
{
|
||||
Marshal.FreeCoTaskMem(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the server details from the enumerator.
|
||||
/// </summary>
|
||||
OpcUrl CreateUrl(OpcSpecification specification, Guid clsid)
|
||||
{
|
||||
{
|
||||
// initialize the server url.
|
||||
var url = new OpcUrl();
|
||||
|
||||
url.HostName = m_host;
|
||||
url.Port = 0;
|
||||
url.Path = null;
|
||||
|
||||
if (specification == OpcSpecification.OPC_DA_30) { url.Scheme = OpcUrlScheme.DA; }
|
||||
else if (specification == OpcSpecification.OPC_DA_20) { url.Scheme = OpcUrlScheme.DA; }
|
||||
else if (specification == OpcSpecification.OPC_DA_10) { url.Scheme = OpcUrlScheme.DA; }
|
||||
else if (specification == OpcSpecification.OPC_AE_10) { url.Scheme = OpcUrlScheme.AE; }
|
||||
else if (specification == OpcSpecification.OPC_HDA_10) { url.Scheme = OpcUrlScheme.HDA; }
|
||||
url.HostName = m_host;
|
||||
url.Port = 0;
|
||||
url.Path = null;
|
||||
|
||||
try
|
||||
{
|
||||
// fetch class details from the enumerator.
|
||||
string progID = null;
|
||||
string description = null;
|
||||
string verIndProgID = null;
|
||||
if (specification == OpcSpecification.OPC_DA_30) { url.Scheme = OpcUrlScheme.DA; }
|
||||
else if (specification == OpcSpecification.OPC_DA_20) { url.Scheme = OpcUrlScheme.DA; }
|
||||
else if (specification == OpcSpecification.OPC_DA_10) { url.Scheme = OpcUrlScheme.DA; }
|
||||
else if (specification == OpcSpecification.OPC_AE_10) { url.Scheme = OpcUrlScheme.AE; }
|
||||
else if (specification == OpcSpecification.OPC_HDA_10) { url.Scheme = OpcUrlScheme.HDA; }
|
||||
|
||||
m_server.GetClassDetails(
|
||||
ref clsid,
|
||||
out progID,
|
||||
out description,
|
||||
out verIndProgID);
|
||||
|
||||
// create the server URL path.
|
||||
if (verIndProgID != null)
|
||||
{
|
||||
url.Path = string.Format("{0}/{1}", verIndProgID, "{" + clsid.ToString() + "}");
|
||||
}
|
||||
else if (progID != null)
|
||||
{
|
||||
url.Path = string.Format("{0}/{1}", progID, "{" + clsid.ToString() + "}");
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// bad value in registry.
|
||||
}
|
||||
finally
|
||||
{
|
||||
// default to the clsid if the prog is not known.
|
||||
if (url.Path == null)
|
||||
{
|
||||
url.Path = string.Format("{0}", "{" + clsid.ToString() + "}");
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
// fetch class details from the enumerator.
|
||||
string progID = null;
|
||||
string description = null;
|
||||
string verIndProgID = null;
|
||||
|
||||
// return the server url.
|
||||
return url;
|
||||
}
|
||||
}
|
||||
m_server.GetClassDetails(
|
||||
ref clsid,
|
||||
out progID,
|
||||
out description,
|
||||
out verIndProgID);
|
||||
|
||||
// create the server URL path.
|
||||
if (verIndProgID != null)
|
||||
{
|
||||
url.Path = string.Format("{0}/{1}", verIndProgID, "{" + clsid.ToString() + "}");
|
||||
}
|
||||
else if (progID != null)
|
||||
{
|
||||
url.Path = string.Format("{0}/{1}", progID, "{" + clsid.ToString() + "}");
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// bad value in registry.
|
||||
}
|
||||
finally
|
||||
{
|
||||
// default to the clsid if the prog is not known.
|
||||
if (url.Path == null)
|
||||
{
|
||||
url.Path = string.Format("{0}", "{" + clsid.ToString() + "}");
|
||||
}
|
||||
}
|
||||
|
||||
// return the server url.
|
||||
return url;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user