Example #1
0
        public Buffer SetState(QpcTimeStamp qpcTimeStamp, BufferState state, string reason)
        {
            BufferState entryState = State;

            State     = state;
            TimeStamp = qpcTimeStamp;

            switch (state)
            {
            case BufferState.SendPosted: CopyHeaderToByteArray(); SendPostedTimeStamp = qpcTimeStamp; break;

            case BufferState.Received: UpdateHeaderFromByteArray(); break;

            default: break;
            }

            if (StateEmitter.IsEnabled)
            {
                StateEmitter.Emit("{0} State changed to {1} [from: {2}, reason: {3}]", BufferName, state, entryState, reason ?? "NoReasonGiven");
            }

            INotifyable notifyOnStateChange = NotifyOnSetState;

            if (notifyOnStateChange != null)
            {
                notifyOnStateChange.Notify();
            }

            return(this);
        }
Example #2
0
        private void InternalNotify(string token, object sender,
                                    NotificationEventArgs e, bool post)
        {
            // Get weak subscribers
            List <WeakReference> weakSubscribers;

            lock (_sharedLock)
            {
                if (!_subscriptions.TryGetValue(token, out weakSubscribers))
                {
                    return;
                }

                // Make a copy while locked
                weakSubscribers = weakSubscribers.ToList();
            }

            // Get compatible living subscribers
            var subscribers = from w in weakSubscribers
                              let s = w.Target as INotifyable
                                      where w != null && w.IsAlive && s != null
                                      select s;

            // Invoke each callback associated with token
            foreach (var subscriber in subscribers)
            {
                INotifyable subscriber1 = subscriber;
                SafeNotify(() => subscriber1.Notify(token, sender, e));
            }

            lock (_sharedLock)
            {
                if (_subscriptions.ContainsKey(token))
                {
                    // Remove subscribers who are no longer alive
                    var deadSubscribers = weakSubscribers
                                          .Where(w => w == null || !w.IsAlive);
                    foreach (var s in deadSubscribers)
                    {
                        _subscriptions[token].Remove(s);
                    }

                    // Remove dictionary entry if no subscibers left
                    if (_subscriptions[token].Count == 0)
                    {
                        _subscriptions.Remove(token);
                    }
                }
            }
        }