public void DetachSubscriptionTest() { //create default endpoint DXEndpoint.Create(); //try to detach null subscription Assert.Catch(typeof(ArgumentNullException), () => { DXFeed.GetInstance().DetachSubscription <IDxOrder>(null); }); //try to detach already detached subscription var s = DXFeed.GetInstance().CreateSubscription <IDxOrder>(); DXFeed.GetInstance().DetachSubscription(s); DXFeed.GetInstance().DetachSubscription(s); //try to detach another not attached subscription DXFeedSubscription <IDxOrder> other = new DXFeedSubscription <IDxOrder>(DXEndpoint.GetInstance() as DXEndpoint); DXFeed.GetInstance().DetachSubscription(other); //thread-safety case DXEndpoint.Create(); Parallel.For(ParallelFrom, ParallelTo, i => { Assert.DoesNotThrow(() => { var newSubscription = new DXFeedSubscription <IDxOrder>(DXEndpoint.GetInstance() as DXEndpoint); DXFeed.GetInstance().AttachSubscription(newSubscription); DXFeed.GetInstance().DetachSubscription(newSubscription); }); }); }
/// <summary> /// Creates new snapshot subscription for a single event type that is attached to this feed. /// This method creates new <see cref="IDXFeedSubscription{E}"/>. /// </summary> /// <typeparam name="E">The type of events.</typeparam> /// <param name="time">Unix time in the past - number of milliseconds from 1.1.1970.</param> /// <param name="source">The source of the event.</param> /// <returns>New DXFeedSubscription for a single event type.</returns> internal IDXFeedSubscription <E> CreateSnapshotSubscription <E>(long time, IndexedEventSource source) where E : IDxIndexedEvent { IDXFeedSubscription <E> subscription = new DXFeedSubscription <E>(endpoint, time, source) as IDXFeedSubscription <E>; subscription.Attach(this); return(subscription); }
/// <summary> /// Tries to get <see cref="DXFeedSubscription{E}"/> instance from current /// <see cref="DXFeed"/>. This method uses reflection to get access to private /// hashset of attached subscriptions. This methods returns first finded subscription /// of type E that contains <paramref name="symbol"/>. /// </summary> /// <typeparam name="E">Event type.</typeparam> /// <typeparam name="symbol">Event type.</typeparam> /// <returns> /// First finded subscription of type E that contains <paramref name="symbol"/>. /// </returns> /// <exception cref="InvalidOperationException"> /// Cannot access to private fileds or such subscription is not exist. /// </exception> internal static DXFeedSubscription <E> GetSubscriptionFromFeed <E>(string symbol) where E : class, IDxEventType { FieldInfo attachedSubscriptionsInfo = typeof(DXFeed).GetField("attachedSubscriptions", BindingFlags.NonPublic | BindingFlags.Instance); if (attachedSubscriptionsInfo == null) { throw new InvalidOperationException("attachedSubscriptions field not found!"); } HashSet <object> attachedSubscriptionsSet = attachedSubscriptionsInfo.GetValue(DXFeed.GetInstance()) as HashSet <object>; if (attachedSubscriptionsSet == null) { throw new InvalidOperationException("Cannot get the set of attached subscriptions!"); } FieldInfo attachLockInfo = typeof(DXFeed).GetField("attachLock", BindingFlags.NonPublic | BindingFlags.Instance);; if (attachLockInfo == null) { throw new InvalidOperationException("attachLock field not found!"); } object attachLocker = attachLockInfo.GetValue(DXFeed.GetInstance()); if (attachLocker == null) { throw new InvalidOperationException("Cannot get the locker of attached subscriptions!"); } HashSet <object> subscriptions = null; lock (attachLocker) { subscriptions = new HashSet <object>(attachedSubscriptionsSet); } if (subscriptions.Count == 0) { throw new InvalidOperationException("There is no attached subscriptions to feed!"); } DXFeedSubscription <E> subscription = null; foreach (var s in subscriptions) { subscription = s as DXFeedSubscription <E>; if (subscription != null && subscription.GetSymbols().Contains(symbol)) { break; } } if (subscription == null) { throw new InvalidOperationException(string.Format("The {0} subscription is not found in this feed!", typeof(E))); } return(subscription); }
/// <summary> /// Creates new subscription for multiple event types that is attached to this feed. /// For a single event type use <see cref="CreateSubscription{E}"/>. /// This method creates new <see cref="IDXFeedSubscription{E}"/> and invokes /// <see cref="AttachSubscription{E}(IDXFeedSubscription{E})"/>. /// </summary> /// <typeparam name="E">The type of events.</typeparam> /// <param name="eventTypes">The classes of event types.</param> /// <returns>The new <see cref="IDXFeedSubscription{E}"/>.</returns> /// <exception cref="InvalidOperationException">Endpoint was been closed.</exception> public IDXFeedSubscription <E> CreateSubscription <E>(params Type[] eventTypes) where E : IDxEventType { if (endpoint.State == DXEndpointState.Closed) { throw new InvalidOperationException("Endpoint was been closed."); } IDXFeedSubscription <E> subscription = new DXFeedSubscription <E>(endpoint, eventTypes) as IDXFeedSubscription <E>; subscription.Attach(this); return(subscription); }