Beispiel #1
0
        private static ulong GetMessageId(TopicLookup topics, string key)
        {
            Topic topic;

            if (topics.TryGetValue(key, out topic))
            {
                return(GetMessageId(topic));
            }
            return(0);
        }
Beispiel #2
0
        public MessageBus(IStringMinifier stringMinifier,
                          ITraceManager traceManager,
                          IPerformanceCounterManager performanceCounterManager,
                          IConfigurationManager configurationManager,
                          int maxTopicsWithNoSubscriptions)
        {
            if (stringMinifier == null)
            {
                throw new ArgumentNullException("stringMinifier");
            }

            if (traceManager == null)
            {
                throw new ArgumentNullException("traceManager");
            }

            if (performanceCounterManager == null)
            {
                throw new ArgumentNullException("performanceCounterManager");
            }

            if (configurationManager == null)
            {
                throw new ArgumentNullException("configurationManager");
            }

            if (configurationManager.DefaultMessageBufferSize < 0)
            {
                throw new ArgumentOutOfRangeException(Resources.Error_BufferSizeOutOfRange);
            }

            _stringMinifier = stringMinifier;
            _traceManager   = traceManager;
            Counters        = performanceCounterManager;
            _trace          = _traceManager["SignalR." + typeof(MessageBus).Name];
            _maxTopicsWithNoSubscriptions = maxTopicsWithNoSubscriptions;

            _gcTimer = new Timer(_ => GarbageCollectTopics(), state: null, dueTime: _gcInterval, period: _gcInterval);

            _broker = new MessageBroker(Counters)
            {
                Trace = _trace
            };

            // The default message store size
            _messageStoreSize = (uint)configurationManager.DefaultMessageBufferSize;

            _topicTtl            = configurationManager.TopicTtl();
            _createTopic         = CreateTopic;
            _addEvent            = AddEvent;
            _removeEvent         = RemoveEvent;
            _disposeSubscription = o => DisposeSubscription(o);

            Topics = new TopicLookup();
        }
Beispiel #3
0
        private List <Cursor> GetCursorsFromEventKeys(IList <string> eventKeys, TopicLookup topics)
        {
            var list = new List <Cursor>(eventKeys.Count);

            foreach (var eventKey in eventKeys)
            {
                var cursor = new Cursor(eventKey, GetMessageId(topics, eventKey), _stringMinifier.Minify(eventKey));
                list.Add(cursor);
            }

            return(list);
        }
Beispiel #4
0
        public DefaultSubscription(string identity,
                                   IList <string> eventKeys,
                                   TopicLookup topics,
                                   string cursor,
                                   Func <MessageResult, object, Task <bool> > callback,
                                   int maxMessages,
                                   IStringMinifier stringMinifier,
                                   IPerformanceCounterManager counters,
                                   object state) :
            base(identity, eventKeys, callback, maxMessages, counters, state)
        {
            _stringMinifier = stringMinifier;

            if (String.IsNullOrEmpty(cursor))
            {
                _cursors = GetCursorsFromEventKeys(EventKeys, topics);
            }
            else
            {
                // Ensure delegate continues to use the C# Compiler static delegate caching optimization.
                _cursors = Cursor.GetCursors(cursor, _defaultCursorPrefix, (k, s) => UnminifyCursor(k, s), stringMinifier) ?? GetCursorsFromEventKeys(EventKeys, topics);
            }

            _cursorTopics = new List <Topic>();

            if (!String.IsNullOrEmpty(cursor))
            {
                // Update all of the cursors so we're within the range
                for (int i = _cursors.Count - 1; i >= 0; i--)
                {
                    Cursor c = _cursors[i];
                    Topic  topic;
                    if (!EventKeys.Contains(c.Key))
                    {
                        _cursors.Remove(c);
                    }
                    else if (!topics.TryGetValue(_cursors[i].Key, out topic) || _cursors[i].Id > topic.Store.GetMessageCount())
                    {
                        UpdateCursor(c.Key, 0);
                    }
                }
            }

            // Add dummy entries so they can be filled in
            for (int i = 0; i < _cursors.Count; i++)
            {
                _cursorTopics.Add(null);
            }
        }