/// <summary>
        /// Completes an asynchronous request cancel
        /// </summary>
        private void OnCancel(object stateInfo)
        {
            try
            {
                lock (this)
                {
                    // check if subscription object has been disposed.
                    if (m_disposed)
                    {
                        return;
                    }
                }

                // unmarshal arguments.
                Opc.Da.Request             request  = null;
                CancelCompleteEventHandler callback = null;

                if (typeof(object[]).IsInstanceOfType(stateInfo))
                {
                    request  = (Opc.Da.Request)((object[])stateInfo)[0];
                    callback = (CancelCompleteEventHandler)((object[])stateInfo)[1];
                }

                // invoke callback.
                callback(request.Handle);
            }
            catch (Exception e)
            {
                string message = e.Message;
            }
        }
        /// <summary>
        /// Completes an asynchronous read request.
        /// </summary>
        private void OnRead(object stateInfo)
        {
            try
            {
                // unmarshal arguments.
                Opc.Da.Request request = null;
                Item[]         items   = null;

                if (typeof(object[]).IsInstanceOfType(stateInfo))
                {
                    request = (Opc.Da.Request)((object[])stateInfo)[0];
                    items   = (Item[])((object[])stateInfo)[1];
                }

                // check if request is still valid and read values.
                object            clientHandle = null;
                ItemValueResult[] results      = null;
                Delegate          callback     = null;

                lock (this)
                {
                    // check if subscription object has been disposed.
                    if (m_disposed)
                    {
                        return;
                    }

                    clientHandle = m_state.ClientHandle;
                    callback     = (Delegate)m_requests[request];

                    if (callback != null)
                    {
                        m_requests.Remove(request);
                        results = Read(items);
                    }
                }

                // invoke read complete callback.
                if (typeof(ReadCompleteEventHandler).IsInstanceOfType(callback))
                {
                    ((ReadCompleteEventHandler)callback)(request.Handle, results);
                }

                // invoke refresh complete callback.
                else if (typeof(DataChangedEventHandler).IsInstanceOfType(callback))
                {
                    ((DataChangedEventHandler)callback)(clientHandle, request.Handle, results);
                }
            }
            catch (Exception e)
            {
                string message = e.Message;
            }
        }
        /// <summary>
        /// Completes an asynchronous write request.
        /// </summary>
        private void OnWrite(object stateInfo)
        {
            try
            {
                do
                {
                    object                    requestHandle = null;
                    IdentifiedResult[]        results       = null;
                    WriteCompleteEventHandler callback      = null;

                    lock (this)
                    {
                        // check if subscription object has been disposed.
                        if (m_disposed)
                        {
                            return;
                        }

                        // check if write queue has emptied.
                        if (m_writeQueue.Count == 0)
                        {
                            break;
                        }

                        // get operation parameters.
                        object[] parameters = (object[])m_writeQueue.Dequeue();

                        Opc.Da.Request request = (Opc.Da.Request)parameters[0];
                        ItemValue[]    items   = (ItemValue[])parameters[1];

                        requestHandle = request.Handle;
                        callback      = (WriteCompleteEventHandler)m_requests[request];

                        // do write request if not cancelled.
                        if (callback != null)
                        {
                            m_requests.Remove(request);
                            results = Write(items);
                        }
                    }

                    // invoke callback.
                    if (callback != null)
                    {
                        callback(requestHandle, results);
                    }
                }while (true);
            }
            catch (Exception e)
            {
                string message = e.Message;
            }
        }
        /// <remarks/>
        public void Refresh(
            object requestHandle,
            out IRequest request)
        {
            lock (this)
            {
                request = null;

                // return error is group is not active.
                if (!m_state.Active)
                {
                    throw new ResultIDException(ResultID.E_FAIL);
                }

                // determine list available active items.
                ArrayList activeItems = new ArrayList();

                foreach (SubscriptionItem subscriptionItem in m_items.Values)
                {
                    if (subscriptionItem.Active)
                    {
                        Item item = new Item(new ItemIdentifier(subscriptionItem.ItemID));

                        item.ServerHandle    = subscriptionItem.ServerHandle;
                        item.MaxAge          = 0;
                        item.MaxAgeSpecified = true;

                        activeItems.Add(item);
                    }
                }

                // no active items.
                if (activeItems.Count == 0)
                {
                    throw new ResultIDException(ResultID.E_FAIL);
                }

                // create request.
                request = new Opc.Da.Request(this, requestHandle);
                m_requests.Add(request, m_dataChanged);

                // queue update.
                ThreadPool.QueueUserWorkItem(new WaitCallback(OnRead), new object[] { request, activeItems.ToArray(typeof(Item)) });
            }
        }
        /// <summary>
        /// Begins an asynchronous write operation for a set of items.
        /// </summary>
        /// <param name="items">The set of item values to write (must include the server handle).</param>
        /// <param name="requestHandle">An identifier for the request assigned by the caller.</param>
        /// <param name="callback">A delegate used to receive notifications when the request completes.</param>
        /// <param name="request">An object that contains the state of the request (used to cancel the request).</param>
        /// <returns>A set of results containing any errors encountered when the server validated the items.</returns>
        public IdentifiedResult[] Write(
            ItemValue[]               items,
            object requestHandle,
            WriteCompleteEventHandler callback,
            out IRequest request)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            lock (this)
            {
                request = null;

                // handle trivial case.
                if (items.Length == 0)
                {
                    return(new IdentifiedResult[0]);
                }

                // validate the items.
                ArrayList validItems = new ArrayList();

                IdentifiedResult[] results = new IdentifiedResult[items.Length];

                for (int ii = 0; ii < items.Length; ii++)
                {
                    // initialize result with item.
                    results[ii] = new IdentifiedResult(items[ii]);

                    // check for invalid handle.
                    if (items[ii].ServerHandle == null)
                    {
                        results[ii].ResultID = ResultID.Da.E_INVALIDHANDLE;
                        continue;
                    }

                    // lookup subscription item.
                    SubscriptionItem item = (SubscriptionItem)m_items[items[ii].ServerHandle];

                    if (item == null)
                    {
                        results[ii].ResultID = ResultID.Da.E_INVALIDHANDLE;
                        continue;
                    }

                    // at least one valid item exists.
                    validItems.Add(items[ii]);
                }

                if (validItems.Count > 0)
                {
                    request = new Opc.Da.Request(this, requestHandle);

                    m_requests.Add(request, callback);
                    m_writeQueue.Enqueue(new object[] { request, validItems.ToArray(typeof(ItemValue)) });

                    ThreadPool.QueueUserWorkItem(new WaitCallback(OnWrite));
                }

                // apply result filters.
                ApplyFilters(m_filters, results);

                // return results.
                return(results);
            }
        }
예제 #6
0
        /// <summary>
        /// Completes an asynchronous read request.
        /// </summary>
        private void OnRead(object stateInfo)
        {
            try
            {
                // unmarshal arguments.
                Opc.Da.Request request = null;
                Item[]         items   = null;

                if (typeof(object[]).IsInstanceOfType(stateInfo))
                {
                    request = (Opc.Da.Request)((object[])stateInfo)[0];
                    items   = (Item[])((object[])stateInfo)[1];
                }

                // check if request is still valid and read values.
                object            clientHandle = null;
                ItemValueResult[] results      = null;
                Delegate          callback     = null;

                lock (this)
                {
                    // check if subscription object has been disposed.
                    if (m_disposed)
                    {
                        return;
                    }

                    clientHandle = m_state.ClientHandle;
                    callback     = (Delegate)m_requests[request];

                    if (callback != null)
                    {
                        m_requests.Remove(request);
                        results = Read(items);
                    }
                }

                // invoke read complete callback.
                if (typeof(ReadCompleteEventHandler).IsInstanceOfType(callback))
                {
                    ((ReadCompleteEventHandler)callback)(request.Handle, results);
                }

                // invoke refresh complete callback.
                else if (typeof(DataChangedEventHandler).IsInstanceOfType(callback))
                {
                    ((DataChangedEventHandler)callback)(clientHandle, request.Handle, results);
                }
                else
                {
                    new CAS.Lib.RTLib.Processes.EventLogMonitor
                    (
                        "Internal error cannot find request callback delegate",
                        System.Diagnostics.EventLogEntryType.Error,
                        (int)CAS.Lib.RTLib.Processes.Error.CAS_OpcSvr_Da_NETServer_Subscription,
                        1098
                    ).WriteEntry();
                }
            }
            catch (Exception e)
            {
                new CAS.Lib.RTLib.Processes.EventLogMonitor
                (
                    e.Message
#if DEBUG
                    + " " + e.StackTrace
#endif
                    , System.Diagnostics.EventLogEntryType.Error,
                    (int)CAS.Lib.RTLib.Processes.Error.CAS_OpcSvr_Da_NETServer_Subscription,
                    1107
                ).WriteEntry();
            }
        }