Пример #1
0
        /// <summary>
        /// Returns the current state of the subscription.
        /// </summary>
        /// <returns>The current state of the subscription.</returns>
        public GroupState GetState()
        {
            lock (this)
            {
                GroupState state = new GroupState();

                state.ClientHandle = m_clientHandle;

                try
                {
                    string name         = null;
                    int    active       = 0;
                    int    updateRate   = 0;
                    float  deadband     = 0;
                    int    timebias     = 0;
                    int    localeID     = 0;
                    int    clientHandle = 0;
                    int    serverHandle = 0;

                    ((IOPCGroupStateMgt)m_group).GetState(
                        out updateRate,
                        out active,
                        out name,
                        out timebias,
                        out deadband,
                        out localeID,
                        out clientHandle,
                        out serverHandle);

                    state.Name         = name;
                    state.ServerHandle = serverHandle;
                    state.Active       = active != 0;
                    state.UpdateRate   = updateRate;
                    state.Deadband     = deadband;
                    state.Locale       = Interop.GetLocale(localeID);

                    // cache the name separately.
                    m_name = state.Name;
                }
                catch (Exception e)
                {
                    throw Interop.CreateException("IOPCGroupStateMgt.GetState", e);
                }

                return(state);
            }
        }
Пример #2
0
        /// <summary>
        /// Changes the state of a group.
        /// </summary>
        /// <param name="masks">A bit mask that indicates which elements of the group state are changing.</param>
        /// <param name="state">The new group state.</param>
        /// <returns>The actual group state after applying the changes.</returns>
        public GroupState SetState(int masks, GroupState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            lock (this)
            {
                // update the group name.
                if ((masks & (int)StateMask.Name) != 0 && state.Name != m_name)
                {
                    try
                    {
                        ((IOPCGroupStateMgt)m_group).SetName(state.Name);
                        m_name = state.Name;
                    }
                    catch (Exception e)
                    {
                        throw Interop.CreateException("IOPCGroupStateMgt.SetName", e);
                    }
                }

                // update the client handle.
                if ((masks & (int)StateMask.ClientHandle) != 0)
                {
                    m_clientHandle = state.ClientHandle;

                    // update the callback object.
                    m_callback.SetFilters(m_clientHandle, m_filters);
                }

                // update the group state.
                int active   = (state.Active) ? 1 : 0;
                int localeID = ((masks & (int)StateMask.Locale) != 0) ? Interop.GetLocale(state.Locale) : 0;

                GCHandle hActive     = GCHandle.Alloc(active, GCHandleType.Pinned);
                GCHandle hLocale     = GCHandle.Alloc(localeID, GCHandleType.Pinned);
                GCHandle hUpdateRate = GCHandle.Alloc(state.UpdateRate, GCHandleType.Pinned);
                GCHandle hDeadband   = GCHandle.Alloc(state.Deadband, GCHandleType.Pinned);

                int updateRate = 0;

                try
                {
                    ((IOPCGroupStateMgt)m_group).SetState(
                        ((masks & (int)StateMask.UpdateRate) != 0) ? hUpdateRate.AddrOfPinnedObject() : IntPtr.Zero,
                        out updateRate,
                        ((masks & (int)StateMask.Active) != 0) ? hActive.AddrOfPinnedObject() : IntPtr.Zero,
                        IntPtr.Zero,
                        ((masks & (int)StateMask.Deadband) != 0) ? hDeadband.AddrOfPinnedObject() : IntPtr.Zero,
                        ((masks & (int)StateMask.Locale) != 0) ? hLocale.AddrOfPinnedObject() : IntPtr.Zero,
                        IntPtr.Zero);
                }
                catch (Exception e)
                {
                    throw Interop.CreateException("IOPCGroupStateMgt.SetState", e);
                }
                finally
                {
                    if (hActive.IsAllocated)
                    {
                        hActive.Free();
                    }
                    if (hLocale.IsAllocated)
                    {
                        hLocale.Free();
                    }
                    if (hUpdateRate.IsAllocated)
                    {
                        hUpdateRate.Free();
                    }
                    if (hDeadband.IsAllocated)
                    {
                        hDeadband.Free();
                    }
                }

                // return the current state.
                return(GetState());
            }
        }