示例#1
0
        //======================================================================
        // Constructor

        /// <summary>
        /// Initializes a new instance of a subscription.
        /// </summary>
        internal Subscription(
            Server server,
            OpcXml.Da10.Service proxy,
            SubscriptionState state,
            int filters)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }
            if (proxy == null)
            {
                throw new ArgumentNullException("proxy");
            }

            m_server  = server;
            m_proxy   = proxy;
            m_filters = filters;
            m_state   = (state != null)?(SubscriptionState)state.Clone():new SubscriptionState();

            if (m_state.Name == null || m_state.Name == "")
            {
                lock (typeof(Subscription))
                {
                    m_state.Name = String.Format("Subscription{0,3:000}", ++m_counter);
                }
            }
        }
示例#2
0
        public ISubscription CreateSubscription(SubscriptionState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            lock (this)
            {
                if (m_server == null)
                {
                    throw new NotConnectedException();
                }

                SubscriptionState subscriptionState = (SubscriptionState)state.Clone();
                Guid     riid               = typeof(IOPCItemMgt).GUID;
                object   ppUnk              = null;
                int      phServerGroup      = 0;
                int      pRevisedUpdateRate = 0;
                GCHandle gCHandle           = GCHandle.Alloc(subscriptionState.Deadband, GCHandleType.Pinned);
                try
                {
                    ((IOPCServer)m_server).AddGroup((subscriptionState.Name != null) ? subscriptionState.Name : "", subscriptionState.Active ? 1 : 0, subscriptionState.UpdateRate, 0, IntPtr.Zero, gCHandle.AddrOfPinnedObject(), OpcCom.Interop.GetLocale(subscriptionState.Locale), out phServerGroup, out pRevisedUpdateRate, ref riid, out ppUnk);
                }
                catch (Exception e)
                {
                    throw OpcCom.Interop.CreateException("IOPCServer.AddGroup", e);
                }
                finally
                {
                    if (gCHandle.IsAllocated)
                    {
                        gCHandle.Free();
                    }
                }

                try
                {
                    int pdwRevisedKeepAliveTime = 0;
                    ((IOPCGroupStateMgt2)ppUnk).SetKeepAlive(subscriptionState.KeepAlive, out pdwRevisedKeepAliveTime);
                    subscriptionState.KeepAlive = pdwRevisedKeepAliveTime;
                }
                catch
                {
                    subscriptionState.KeepAlive = 0;
                }

                subscriptionState.ServerHandle = phServerGroup;
                if (pRevisedUpdateRate > subscriptionState.UpdateRate)
                {
                    subscriptionState.UpdateRate = pRevisedUpdateRate;
                }

                Subscription subscription = CreateSubscription(ppUnk, subscriptionState, m_filters);
                m_subscriptions[phServerGroup] = subscription;
                return(subscription);
            }
        }
示例#3
0
        //======================================================================
        // CreateSubscription

        /// <summary>
        /// Creates a new subscription.
        /// </summary>
        /// <param name="state">The initial state of the subscription.</param>
        /// <returns>The new subscription object.</returns>
        public ISubscription CreateSubscription(SubscriptionState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            lock (this)
            {
                if (m_server == null)
                {
                    throw new NotConnectedException();
                }

                // copy the subscription state.
                SubscriptionState result = (Opc.Da.SubscriptionState)state.Clone();

                // initialize arguments.
                Guid   iid   = typeof(IOPCItemMgt).GUID;
                object group = null;

                int serverHandle      = 0;
                int revisedUpdateRate = 0;

                GCHandle hDeadband = GCHandle.Alloc(result.Deadband, GCHandleType.Pinned);

                // invoke COM method.
                try
                {
                    ((IOPCServer)m_server).AddGroup(
                        (result.Name != null)?result.Name:"",
                        (result.Active)?1:0,
                        result.UpdateRate,
                        0,
                        IntPtr.Zero,
                        hDeadband.AddrOfPinnedObject(),
                        OpcCom.Interop.GetLocale(result.Locale),
                        out serverHandle,
                        out revisedUpdateRate,
                        ref iid,
                        out group);
                }
                catch (Exception e)
                {
                    throw OpcCom.Interop.CreateException("IOPCServer.AddGroup", e);
                }
                finally
                {
                    if (hDeadband.IsAllocated)
                    {
                        hDeadband.Free();
                    }
                }

                // set the keep alive rate if requested.
                try
                {
                    int keepAlive = 0;
                    ((IOPCGroupStateMgt2)group).SetKeepAlive(result.KeepAlive, out keepAlive);
                    result.KeepAlive = keepAlive;
                }
                catch
                {
                    result.KeepAlive = 0;
                }

                // save server handle.
                result.ServerHandle = serverHandle;

                // set the revised update rate.
                if (revisedUpdateRate > result.UpdateRate)
                {
                    result.UpdateRate = revisedUpdateRate;
                }

                // create the subscription object.
                OpcCom.Da.Subscription subscription = CreateSubscription(group, result, m_filters);

                // index by server handle.
                m_subscriptions[serverHandle] = subscription;

                // return subscription.
                return(subscription);
            }
        }