Exemplo n.º 1
0
 /// <summary>
 /// Releases all references to the server.
 /// </summary>
 protected virtual void ReleaseServer()
 {
     lock (m_lock)
     {
         ComUtils.ReleaseServer(m_unknown);
         m_unknown = null;
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// An overrideable version of the Dispose.
 /// </summary>
 protected virtual void Dispose(bool disposing)
 {
     if (m_server != null)
     {
         ComUtils.ReleaseServer(m_server);
         m_server = null;
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// An overrideable version of the Dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            object enumerator = System.Threading.Interlocked.Exchange(ref m_enumerator, null);

            if (enumerator != null)
            {
                ComUtils.ReleaseServer(enumerator);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Must be called in the finally block after making a COM call.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        protected void EndComCall(string methodName)
        {
            Utils.Trace(Utils.TraceMasks.ExternalSystem, "{0} completed.", methodName);

            lock (m_lock)
            {
                m_outstandingCalls--;

                if (m_disposed && m_outstandingCalls <= 0)
                {
                    ComUtils.ReleaseServer(m_unknown);
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Releases the active server.
 /// </summary>
 public void Disconnect()
 {
     try
     {
         if (m_server != null)
         {
             ComUtils.ReleaseServer(m_server);
             m_server = null;
         }
     }
     catch (Exception e)
     {
         throw ServiceResultException.Create(StatusCodes.BadCommunicationError, e, "Could not release OPCEnum server.");
     }
 }
Exemplo n.º 6
0
        public List <Guid> GetClsIds()
        {
            var categories = new[]
            {
                new Guid("63D5F432-CFE4-11d1-B2C8-0060083BA1FB"), // DA 2.xxx
                new Guid("CC603642-66D7-48f1-B69A-B625E73652D7"), // DA 3.0
            };
            IOPCEnumGUID enumerator = null;

            _server.EnumClassesOfCategories(2, categories, 0, null, out enumerator);
            var guids = new List <Guid>();

            try
            {
                var buffer   = new Uri[10];
                var guidSize = Marshal.SizeOf <Guid>();
                int fetched  = 0;
                do
                {
                    var ptr = Marshal.AllocCoTaskMem(guidSize * buffer.Length);
                    try
                    {
                        enumerator.Next(buffer.Length, ptr, out fetched);
                        if (fetched > 0)
                        {
                            var p = ptr;
                            for (int i = 0; i < fetched; i++, p += guidSize)
                            {
                                var v = Marshal.PtrToStructure <Guid>(p);
                                Marshal.DestroyStructure <Guid>(p);
                                guids.Add(v);
                            }
                        }
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(ptr);
                    }
                }while (fetched > 0);
            }
            catch { }
            finally
            {
                ComUtils.ReleaseServer(enumerator);
            }
            return(guids);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Returns a list of servers that support the specified specification.
        /// </summary>
        public Uri[] GetAvailableServers(params Specification[] specifications)
        {
            // enumerate servers on specified machine.
            try
            {
                // convert the interface version to a guid.
                Guid[] catids = new Guid[specifications.Length];

                for (int ii = 0; ii < catids.Length; ii++)
                {
                    catids[ii] = new Guid(specifications[ii].Id);
                }

                // get list of servers in the specified specification.
                IOPCEnumGUID enumerator = null;

                m_server.EnumClassesOfCategories(
                    catids.Length,
                    catids,
                    0,
                    null,
                    out enumerator);

                // read clsids.
                List <Guid> clsids = ReadClasses(enumerator);

                // release enumerator object.
                ComUtils.ReleaseServer(enumerator);
                enumerator = null;

                // fetch class descriptions.
                Uri[] uris = new Uri[clsids.Count];

                for (int ii = 0; ii < uris.Length; ii++)
                {
                    uris[ii] = CreateUri(clsids[ii]);
                }

                return(uris);
            }
            catch (Exception e)
            {
                throw ServiceResultException.Create(StatusCodes.BadCommunicationError, e, "Could not enumerate COM servers.");
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// An overrideable version of the Dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            UnregisterInterface(typeof(OpcRcw.Ae.IOPCEventSink).GUID);

            // Remove monitored items added for this subscription
            if (m_AreaVector.Count > 0)
            {
                m_server.RemoveMonitoredItems(m_AreaVector);
            }

            if (m_server != null)
            {
                m_server.SubscriptionListRemove(this);
            }

            if (m_server != null)
            {
                ComUtils.ReleaseServer(m_server);
                m_server = null;
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Connects to OPCEnum on the specified machine.
        /// </summary>
        public void Connect(string host, UserIdentity identity)
        {
            // disconnect from current server.
            Disconnect();

            // create in the instance.
            object unknown = null;

            try
            {
                unknown = ComUtils.CreateInstance(OPCEnumCLSID, host, identity);
            }
            catch (Exception e)
            {
                throw ServiceResultException.Create(StatusCodes.BadCommunicationError, e, "Could not connect to OPCEnum server.");
            }

            m_server = unknown as IOPCServerList2;

            if (m_server == null)
            {
                ComUtils.ReleaseServer(unknown);

                StringBuilder error = new StringBuilder();

                error.Append("Server does not support IOPCServerList2. ");
                error.Append("The OPC proxy/stubs may not be installed properly or the client or server machine. ");
                error.Append("The also could be a problem with DCOM security configuration.");

                throw ServiceResultException.Create(StatusCodes.BadCommunicationError, error.ToString());
            }

            m_host = host;

            if (String.IsNullOrEmpty(m_host))
            {
                m_host = "localhost";
            }
        }