public ISubscription CreateSubscription(SubscriptionState state) { if (state == null) { throw new ArgumentNullException("state"); } lock (this) { if (base.m_server == null) { throw new NotConnectedException(); } SubscriptionState state2 = (SubscriptionState)state.Clone(); Guid gUID = typeof(IOPCItemMgt).GUID; object ppUnk = null; int phServerGroup = 0; int pRevisedUpdateRate = 0; GCHandle handle = GCHandle.Alloc(state2.Deadband, GCHandleType.Pinned); try { ((IOPCServer)base.m_server).AddGroup((state2.Name != null) ? state2.Name : "", state2.Active ? 1 : 0, state2.UpdateRate, 0, IntPtr.Zero, handle.AddrOfPinnedObject(), OpcCom.Interop.GetLocale(state2.Locale), out phServerGroup, out pRevisedUpdateRate, ref gUID, out ppUnk); } catch (Exception exception) { throw OpcCom.Interop.CreateException("IOPCServer.AddGroup", exception); } finally { if (handle.IsAllocated) { handle.Free(); } } try { int pdwRevisedKeepAliveTime = 0; ((IOPCGroupStateMgt2)ppUnk).SetKeepAlive(state2.KeepAlive, out pdwRevisedKeepAliveTime); state2.KeepAlive = pdwRevisedKeepAliveTime; } catch { state2.KeepAlive = 0; } state2.ServerHandle = phServerGroup; if (pRevisedUpdateRate > state2.UpdateRate) { state2.UpdateRate = pRevisedUpdateRate; } OpcCom.Da.Subscription subscription = this.CreateSubscription(ppUnk, state2, this.m_filters); this.m_subscriptions[phServerGroup] = subscription; return(subscription); } }
//====================================================================== // 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); } }