public E GetEvent <E>() where E : class, IDxEventType
            {
                if (!HasEvent <E>())
                {
                    return(null);
                }
                EventType eventType = EventTypeUtil.GetEventsType(typeof(E));

                return((E)lastEvents[eventType].Event);
            }
Esempio n. 2
0
        /// <summary>
        ///     Creates detached subscription for a single event type.
        /// </summary>
        /// <param name="endpoint">The <see cref="DXEndpoint"/> instance.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="endpoint"/> is null.</exception>
        /// <exception cref="ArgumentException">If type E is not event class.</exception>
        /// <exception cref="DxException">Internal error.</exception>
        public DXFeedSubscription(DXEndpoint endpoint) : this()
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }

            endpoint.OnClosing += Endpoint_OnClosing;

            subscriptionInstance = endpoint.Connection.CreateSubscription(
                EventTypeUtil.GetEventsType(typeof(E)),
                new DXFeedEventHandler <E>(eventListeners, eventListenerLocker));
        }
Esempio n. 3
0
        /// <summary>
        ///     Creates detached snapshot subscription for a single event type.
        /// </summary>
        /// <param name="endpoint">The <see cref="DXEndpoint"/> instance.</param>
        /// <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>
        /// <exception cref="ArgumentNullException">If <paramref name="endpoint"/> is null.</exception>
        /// <exception cref="ArgumentException">If type E is not event class.</exception>
        /// <exception cref="DxException">Internal error.</exception>
        internal DXFeedSubscription(DXEndpoint endpoint, long time, IndexedEventSource source) : this()
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }

            endpoint.OnClosing += Endpoint_OnClosing;

            subscriptionInstance = endpoint.Connection.CreateSnapshotSubscription(
                EventTypeUtil.GetEventsType(typeof(E)),
                time,
                new DXFeedEventHandler <E>(eventListeners, eventListenerLocker));
            if (source != IndexedEventSource.DEFAULT)
            {
                subscriptionInstance.SetSource(source.Name);
            }
        }
Esempio n. 4
0
        public void PlayEvents(string symbol, params IPlayedEvent[] playEventsList)
        {
            if (playEventsList == null)
            {
                throw new ArgumentNullException("playEventsList");
            }
            FieldInfo nativeSubscription = typeof(DXFeedSubscription <E>).GetField("subscriptionInstance", BindingFlags.NonPublic | BindingFlags.Instance);

            if (nativeSubscription == null)
            {
                throw new InvalidOperationException("subscriptionInstance field not found!");
            }
            MethodInfo onEvent = typeof(NativeSubscription).GetMethod("OnEvent", BindingFlags.NonPublic | BindingFlags.Instance);

            if (onEvent == null)
            {
                throw new InvalidOperationException("OnEvent method not found!");
            }

            char[] symbolChars    = symbol.ToCharArray();
            IntPtr symbolCharsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(symbolChars, 0);

            foreach (var playedEvent in playEventsList)
            {
                DxTestEventParams nativeEventParams = new DxTestEventParams(playedEvent.Params.Flags, playedEvent.Params.TimeIntField, playedEvent.Params.SnapshotKey);
                IntPtr            dataPtr           = Marshal.AllocHGlobal(Marshal.SizeOf(playedEvent.Data));
                IntPtr            paramsPtr         = Marshal.AllocHGlobal(Marshal.SizeOf(nativeEventParams));
                const int         dataCount         = 1;
                try
                {
                    Marshal.StructureToPtr(playedEvent.Data, dataPtr, false);
                    Marshal.StructureToPtr(nativeEventParams, paramsPtr, false);
                    onEvent.Invoke(nativeSubscription.GetValue(subscription), new object[] {
                        EventTypeUtil.GetEventsType(playedEvent.GetType()), symbolCharsPtr, dataPtr, dataCount, paramsPtr, IntPtr.Zero
                    });
                }
                finally
                {
                    Marshal.FreeHGlobal(dataPtr);
                    Marshal.FreeHGlobal(paramsPtr);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        ///     Creates detached subscription for the given list of event types.
        /// </summary>
        /// <param name="endpoint">The <see cref="DXEndpoint"/> instance.</param>
        /// <param name="eventTypes">The list of event types.</param>
        /// <exception cref="ArgumentNullException">
        ///     If <paramref name="endpoint"/> or <paramref name="eventTypes"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     If <paramref name="eventTypes"/> are empty or any type of
        ///     <paramref name="eventTypes"/> is not event class.
        /// </exception>
        /// <exception cref="DxException">Internal error.</exception>
        public DXFeedSubscription(DXEndpoint endpoint, params Type[] eventTypes) : this(eventTypes)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }

            foreach (Type t in eventTypes)
            {
                if (!typeof(E).IsAssignableFrom(t))
                {
                    throw new ArgumentException(string.Format("The type {0} is not {1}", t, typeof(E)));
                }
            }

            endpoint.OnClosing += Endpoint_OnClosing;

            subscriptionInstance = endpoint.Connection.CreateSubscription(
                EventTypeUtil.GetEventsType(eventTypes),
                new DXFeedEventHandler <E>(eventListeners, eventListenerLocker));
        }
            public void AddEvent <E>(E eventData) where E : class, IDxEventType
            {
                EventType eventType = EventTypeUtil.GetEventsType(typeof(E));

                lastEvents.GetOrAdd(eventType, new EventStorage <IDxEventType>()).Event = eventData;
            }
            public bool HasEvent <E>() where E : class, IDxEventType
            {
                EventType eventType = EventTypeUtil.GetEventsType(typeof(E));

                return(lastEvents.ContainsKey(eventType));
            }
Esempio n. 8
0
        public void PlaySnapshot(string symbol, params IPlayedEvent[] playEventsList)
        {
            if (playEventsList == null)
            {
                throw new ArgumentNullException("playEventsList");
            }
            FieldInfo nativeSubscription = typeof(DXFeedSubscription <E>).GetField("subscriptionInstance", BindingFlags.NonPublic | BindingFlags.Instance);

            if (nativeSubscription == null)
            {
                throw new InvalidOperationException("subscriptionInstance field not found!");
            }
            MethodInfo onEvent = typeof(NativeSnapshotSubscription).GetMethod("OnEvent", BindingFlags.NonPublic | BindingFlags.Instance);

            if (onEvent == null)
            {
                throw new InvalidOperationException("OnEvent method not found!");
            }

            char[] symbolChars = symbol.ToCharArray();
            IntPtr recordsPtr  = IntPtr.Zero;
            IntPtr snapshotPtr = IntPtr.Zero;

            try
            {
                IntPtr nextRecordPtr = IntPtr.Zero;
                foreach (var playedEvent in playEventsList)
                {
                    if (!(playedEvent is E))
                    {
                        throw new ArgumentException("The one of played events is not " + typeof(E));
                    }
                    var dataSize = Marshal.SizeOf(playedEvent.Data);
                    if (recordsPtr == IntPtr.Zero)
                    {
                        recordsPtr    = Marshal.AllocHGlobal(dataSize * playEventsList.Length);
                        nextRecordPtr = recordsPtr;
                    }
                    Marshal.StructureToPtr(playedEvent.Data, nextRecordPtr, false);
                    nextRecordPtr += dataSize;
                }

                DxTestSnapshotData snapshot;
                snapshot.event_type    = EventTypeUtil.GetEventsType(typeof(E));
                snapshot.symbol        = Marshal.UnsafeAddrOfPinnedArrayElement(symbolChars, 0);
                snapshot.records_count = playEventsList.Length;
                snapshot.records       = recordsPtr;
                snapshotPtr            = Marshal.AllocHGlobal(Marshal.SizeOf(snapshot));
                Marshal.StructureToPtr(snapshot, snapshotPtr, false);

                onEvent.Invoke(nativeSubscription.GetValue(subscription), new object[] {
                    snapshotPtr, IntPtr.Zero
                });
            }
            finally
            {
                if (recordsPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(recordsPtr);
                }
                if (snapshotPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(snapshotPtr);
                }
            }
        }