private bool ProcessOutMarketDataMessage(MarketDataMessage message)
        {
            var secIdKey = IsSupportSubscriptionBySecurity ? message.SecurityId : default(SecurityId);

            lock (_sync)
            {
                switch (message.DataType)
                {
                case MarketDataTypes.News:
                {
                    var key = message.NewsId ?? string.Empty;
                    return(ProcessSubscriptionResult(_newsSubscribers, key, message));
                }

                case MarketDataTypes.CandleTimeFrame:
                case MarketDataTypes.CandleRange:
                case MarketDataTypes.CandlePnF:
                case MarketDataTypes.CandleRenko:
                case MarketDataTypes.CandleTick:
                case MarketDataTypes.CandleVolume:
                {
                    var key = Tuple.Create(message.DataType, secIdKey, message.Arg);
                    return(ProcessSubscriptionResult(_candleSubscribers, key, message));
                }

                default:
                {
                    var key = message.CreateKey(secIdKey);
                    return(ProcessSubscriptionResult(_subscribers, key, message));
                }
                }
            }
        }
Exemple #2
0
        private void ProcessSubscriptionAction(IEnumerator <IMessageAdapter> enumerator, MarketDataMessage message, long originalTransactionId)
        {
            if (enumerator.MoveNext())
            {
                enumerator.Current.SendInMessage(message);
            }
            else
            {
                _subscriptionQueue.RemoveByValue(enumerator);

                var key = _subscriptionKeys.TryGetValue(message.OriginalTransactionId);

                if (key == null)
                {
                    key = message.CreateKey();
                }
                else
                {
                    _subscriptionKeys.Remove(originalTransactionId);
                }

                _subscriptionStates.Remove(key);
                RaiseMarketDataMessage(null, originalTransactionId, new ArgumentException(LocalizedStrings.Str629Params.Put(key.Item1 + " " + key.Item2), nameof(message)), true);
            }
        }
Exemple #3
0
        private void ProcessInMarketDataMessage(MarketDataMessage message)
        {
            var sendIn = false;
            MarketDataMessage sendOutMsg = null;
            SubscriptionInfo  info;

            lock (_sync)
            {
                info = message.DataType == MarketDataTypes.News
                                        ? ProcessSubscription(_newsSubscribers, message.NewsId ?? string.Empty, message, ref sendIn, ref sendOutMsg)
                                        : ProcessSubscription(_subscribers, message.CreateKey(GetSecurityId(message)), message, ref sendIn, ref sendOutMsg);
            }

            if (sendIn)
            {
                if (!message.IsSubscribe && message.OriginalTransactionId == 0)
                {
                    message.OriginalTransactionId = info.Message.TransactionId;
                }

                base.SendInMessage(message);
            }

            if (sendOutMsg != null)
            {
                RaiseNewOutMessage(sendOutMsg);
            }
        }
Exemple #4
0
        private void ProcessInMarketDataMessage(MarketDataMessage message)
        {
            var sendIn = false;
            MarketDataMessage sendOutMsg = null;
            SubscriptionInfo  info;
            var secIdKey = IsSupportSubscriptionBySecurity ? message.SecurityId : default(SecurityId);

            lock (_sync)
            {
                var isSubscribe = message.IsSubscribe;

                switch (message.DataType)
                {
                case MarketDataTypes.News:
                {
                    var key = message.NewsId ?? string.Empty;
                    info = ProcessSubscription(_newsSubscribers, key, message, isSubscribe, ref sendIn, ref sendOutMsg);
                    break;
                }

                case MarketDataTypes.CandleTimeFrame:
                case MarketDataTypes.CandleRange:
                case MarketDataTypes.CandlePnF:
                case MarketDataTypes.CandleRenko:
                case MarketDataTypes.CandleTick:
                case MarketDataTypes.CandleVolume:
                {
                    var key = Tuple.Create(message.DataType, secIdKey, message.Arg);
                    info = ProcessSubscription(_candleSubscribers, key, message, isSubscribe, ref sendIn, ref sendOutMsg);
                    break;
                }

                default:
                {
                    var key = message.CreateKey(secIdKey);
                    info = ProcessSubscription(_subscribers, key, message, isSubscribe, ref sendIn, ref sendOutMsg);
                    break;
                }
                }
            }

            if (sendIn)
            {
                if (!message.IsSubscribe && message.OriginalTransactionId == 0)
                {
                    message.OriginalTransactionId = info.Message.TransactionId;
                }

                base.SendInMessage(message);
            }

            if (sendOutMsg != null)
            {
                RaiseNewOutMessage(sendOutMsg);
            }
        }
        private void ProcessInMarketDataMessage(MarketDataMessage message)
        {
            if (_passThroughtIds.Contains(message.TransactionId))
            {
                base.SendInMessage(message);
                return;
            }

            var sendIn                   = false;
            var isOnlyHistory            = false;
            MarketDataMessage sendOutMsg = null;
            SubscriptionInfo <MarketDataMessage> info;

            lock (_sync)
            {
                info = message.DataType == MarketDataTypes.News
                                        ? ProcessSubscription(_newsSubscribers, message.NewsId ?? string.Empty, message, ref sendIn, ref isOnlyHistory, ref sendOutMsg)
                                        : ProcessSubscription(_subscribers, message.CreateKey(GetSecurityId(message)), message, ref sendIn, ref isOnlyHistory, ref sendOutMsg);
            }

            if (sendIn)
            {
                if (!message.IsSubscribe && message.OriginalTransactionId == 0)
                {
                    message.OriginalTransactionId = info.Message.TransactionId;
                }
                else
                {
                    message.IsHistory = isOnlyHistory;

                    if (isOnlyHistory)
                    {
                        _onlyHistorySubscriptions.Add(message.TransactionId);
                    }
                }

                base.SendInMessage(message);
            }

            if (sendOutMsg != null)
            {
                RaiseNewOutMessage(sendOutMsg);
            }
        }
        private void ProcessInMarketDataMessage(MarketDataMessage message)
        {
            MarketDataMessage CreateSendOut(bool isSubscribe, long transId)
            {
                return(new MarketDataMessage
                {
                    //DataType = message.DataType,
                    IsSubscribe = isSubscribe,
                    //SecurityId = message.SecurityId,
                    //Arg = message.Arg,
                    OriginalTransactionId = transId,
                });
            }

            MarketDataMessage CreateNonExist(long transId)
            {
                if (!NonExistSubscriptionAsError)
                {
                    this.AddInfoLog(LocalizedStrings.SubscriptionNonExist);
                }

                return(new MarketDataMessage
                {
                    OriginalTransactionId = transId,
                    Error = NonExistSubscriptionAsError ? new InvalidOperationException(LocalizedStrings.SubscriptionNonExist) : null,
                });
            }

            if (message.DataType.IsSecurityRequired())
            {
                ProcessInSubscriptionMessage(message, message.CreateKey(GetSecurityId(message.SecurityId)), _mdSubscribers, _mdSubscribersById, CreateSendOut, CreateNonExist);
            }
            else
            {
                ProcessInSubscriptionMessage(message, GetNewsBoardKey(message), _newsBoardSubscribers, _mdSubscribersById, CreateSendOut, CreateNonExist);
            }
        }
Exemple #7
0
        private void ProcessMarketDataMessage(IMessageAdapter adapter, MarketDataMessage message)
        {
            var key = _subscriptionKeys.TryGetValue(message.OriginalTransactionId) ?? message.CreateKey();

            var enumerator = _subscriptionQueue.TryGetValue(key);
            var state      = _subscriptionStates.TryGetValue2(key);
            var error      = message.Error;
            var isOk       = !message.IsNotSupported && error == null;

            var isSubscribe = message.IsSubscribe;

            switch (state)
            {
            case SubscriptionStates.Subscribed:
                break;

            case SubscriptionStates.Subscribing:
                isSubscribe = true;
                if (isOk)
                {
                    SetSubscribed(adapter, key);
                }
                else if (error != null)
                {
                    _subscriptions.Remove(key);
                    _subscriptionStates.Remove(key);
                }
                break;

            //case SubscriptionStates.Unsubscribing:
            //	isSubscribe = false;
            //	_subscriptions.Remove(key);
            //	_subscriptionStates.Remove(key);
            //	break;
            case null:
                if (isOk)
                {
                    if (message.IsSubscribe)
                    {
                        SetSubscribed(adapter, key);
                        break;
                    }
                }

                _subscriptions.Remove(key);
                _subscriptionStates.Remove(key);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (message.IsNotSupported)
            {
                if (enumerator != null)
                {
                    ProcessSubscriptionAction(enumerator, message, message.OriginalTransactionId);
                }
                else
                {
                    if (error == null)
                    {
                        error = new InvalidOperationException(LocalizedStrings.Str633Params.Put(message.SecurityId, message.DataType));
                    }
                }
            }

            _subscriptionQueue.Remove(key);
            _subscriptionKeys.Remove(message.OriginalTransactionId);

            RaiseMarketDataMessage(adapter, message.OriginalTransactionId, error, isSubscribe);
        }
        private void ProcessInMarketDataMessage(MarketDataMessage message)
        {
            var sendIn = false;
            MarketDataMessage sendOutMsg = null;
            RefPair <MarketDataMessage, int> pair;
            var secIdKey = InnerAdapter.IsSupportSubscriptionBySecurity ? message.SecurityId : default(SecurityId);

            lock (_sync)
            {
                var isSubscribe = message.IsSubscribe;

                switch (message.DataType)
                {
                case MarketDataTypes.News:
                {
                    var subscriber = message.NewsId ?? string.Empty;

                    pair = _newsSubscribers.TryGetValue(subscriber) ?? RefTuple.Create((MarketDataMessage)message.Clone(), 0);
                    var subscribersCount = pair.Second;

                    if (isSubscribe)
                    {
                        subscribersCount++;
                        sendIn = subscribersCount == 1;
                    }
                    else
                    {
                        if (subscribersCount > 0)
                        {
                            subscribersCount--;
                            sendIn = subscribersCount == 0;
                        }
                        else
                        {
                            sendOutMsg = NonExist(message);
                        }
                    }

                    if (sendOutMsg == null)
                    {
                        if (!sendIn)
                        {
                            sendOutMsg = new MarketDataMessage
                            {
                                DataType              = message.DataType,
                                IsSubscribe           = isSubscribe,
                                OriginalTransactionId = message.TransactionId,
                            };
                        }

                        if (subscribersCount > 0)
                        {
                            pair.Second = subscribersCount;
                            _newsSubscribers[subscriber] = pair;
                        }
                        else
                        {
                            _newsSubscribers.Remove(subscriber);
                        }
                    }

                    break;
                }

                case MarketDataTypes.CandleTimeFrame:
                case MarketDataTypes.CandleRange:
                case MarketDataTypes.CandlePnF:
                case MarketDataTypes.CandleRenko:
                case MarketDataTypes.CandleTick:
                case MarketDataTypes.CandleVolume:
                {
                    var key = Tuple.Create(message.DataType, secIdKey, message.Arg);

                    pair = _candleSubscribers.TryGetValue(key) ?? RefTuple.Create((MarketDataMessage)message.Clone(), 0);
                    var subscribersCount = pair.Second;

                    if (isSubscribe)
                    {
                        subscribersCount++;
                        sendIn = subscribersCount == 1;
                    }
                    else
                    {
                        if (subscribersCount > 0)
                        {
                            subscribersCount--;
                            sendIn = subscribersCount == 0;
                        }
                        else
                        {
                            sendOutMsg = NonExist(message);
                        }
                    }

                    if (sendOutMsg == null)
                    {
                        if (!sendIn)
                        {
                            sendOutMsg = new MarketDataMessage
                            {
                                DataType              = message.DataType,
                                IsSubscribe           = isSubscribe,
                                SecurityId            = message.SecurityId,
                                Arg                   = message.Arg,
                                OriginalTransactionId = message.TransactionId,
                            };
                        }

                        if (subscribersCount > 0)
                        {
                            pair.Second             = subscribersCount;
                            _candleSubscribers[key] = pair;
                        }
                        else
                        {
                            _candleSubscribers.Remove(key);
                        }
                    }

                    break;
                }

                default:
                {
                    var key = message.CreateKey(secIdKey);

                    pair = _subscribers.TryGetValue(key) ?? RefTuple.Create((MarketDataMessage)message.Clone(), 0);
                    var subscribersCount = pair.Second;

                    if (isSubscribe)
                    {
                        subscribersCount++;
                        sendIn = subscribersCount == 1;
                    }
                    else
                    {
                        if (subscribersCount > 0)
                        {
                            subscribersCount--;
                            sendIn = subscribersCount == 0;
                        }
                        else
                        {
                            sendOutMsg = NonExist(message);
                        }
                    }

                    if (sendOutMsg == null)
                    {
                        if (!sendIn)
                        {
                            sendOutMsg = new MarketDataMessage
                            {
                                DataType              = message.DataType,
                                IsSubscribe           = isSubscribe,
                                SecurityId            = message.SecurityId,
                                OriginalTransactionId = message.TransactionId,
                            };
                        }

                        if (subscribersCount > 0)
                        {
                            pair.Second       = subscribersCount;
                            _subscribers[key] = pair;
                        }
                        else
                        {
                            _subscribers.Remove(key);
                        }
                    }

                    break;
                }
                }
            }

            if (sendIn)
            {
                if (!message.IsSubscribe && message.OriginalTransactionId == 0)
                {
                    message.OriginalTransactionId = pair.First.TransactionId;
                }

                base.SendInMessage(message);
            }

            if (sendOutMsg != null)
            {
                RaiseNewOutMessage(sendOutMsg);
            }
        }