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

                return((E)lastEvents[eventType].Event);
            }
        /// <summary>
        ///     Adds a symbol to the subscription.
        /// </summary>
        /// <remarks>
        ///     Don't call this method inside any listeners and callbacks of NativeSubscription, NativeConnection,
        /// NativeRegionalBook, NativeSnapshotSubscription classes
        /// </remarks>
        /// <param name="symbol">Symbol.</param>
        /// <exception cref="ArgumentException">Invalid <paramref name="symbol"/> parameter.</exception>
        /// <exception cref="InvalidOperationException">You try to add more than one symbol to snapshot subscription.</exception>
        /// <exception cref="DxException">Internal error.</exception>
        public void AddSymbol(string symbol)
        {
            if (string.IsNullOrWhiteSpace(symbol))
            {
                throw new ArgumentException("Invalid symbol parameter");
            }
            if (snapshotPtr != InvalidSnapshot)
            {
                throw new InvalidOperationException("It is allowed to add only one symbol to snapshot subscription");
            }

            if ((eventType == EventType.Candle) ||
                (eventType == EventType.None) && NativeSubscription.IsCandleSymbol(symbol))
            {
                AddSymbol(CandleSymbol.ValueOf(symbol));
                return;
            }


            byte[] sourceBytes = null;
            if (source != string.Empty)
            {
                var ascii = Encoding.ASCII;
                sourceBytes = ascii.GetBytes(source);
            }

            if (eventType == EventType.None)
            {
                eventType = EventType.Order;
                C.CheckOk(C.Instance.dxf_create_order_snapshot(connectionPtr, symbol, sourceBytes, time, out snapshotPtr));
            }
            else
            {
                C.CheckOk(C.Instance.dxf_create_snapshot(connectionPtr, EventTypeUtil.GetEventId(eventType), symbol, sourceBytes, time, out snapshotPtr));
            }

            try
            {
                if (listener is IDxIncOrderSnapshotListener)
                {
                    C.CheckOk(C.Instance.dxf_attach_snapshot_inc_listener(snapshotPtr, incCallback = OnIncOrderSnapshot, IntPtr.Zero));
                }
                else
                {
                    C.CheckOk(C.Instance.dxf_attach_snapshot_listener(snapshotPtr, callback = OnEvent, IntPtr.Zero));
                }
            }
            catch (DxException)
            {
                Dispose();
                throw;
            }
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
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);
                }
            }
        }
Ejemplo n.º 6
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));
            }
Ejemplo n.º 9
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);
                }
            }
        }