public IDisposable TrackActions(AsyncQueue <DynamicJsonValue> notificationsQueue, IWebsocketWriter webSocketWriter)
        {
            var watcher = new ConnectedWatcher
            {
                NotificationsQueue = notificationsQueue,
                Writer             = webSocketWriter
            };

            lock (_watchersLock)
            {
                Watchers.TryAdd(watcher);

                if (Watchers.Count == 1)
                {
                    StartBackgroundWorkers();
                }

                if (watcher.Writer is NotificationCenterWebSocketWriter)
                {
                    if (_state.NumberOfClients == 0)
                    {
                        var copy = _state;
                        // we use interlocked here to make sure that other threads
                        // are immediately exposed to this
                        Interlocked.Exchange(ref _state, new State
                        {
                            NumberOfClients = 1
                        });
                        copy.NewWebSocket.TrySetResult(null);
                    }
                    else
                    {
                        Interlocked.Increment(ref _state.NumberOfClients);
                    }
                }
            }

            return(new DisposableAction(() =>
            {
                lock (_watchersLock)
                {
                    Watchers.TryRemove(watcher);

                    if (Watchers.Count == 0)
                    {
                        StopBackgroundWorkers();
                    }

                    if (watcher.Writer is NotificationCenterWebSocketWriter)
                    {
                        var copy = _state;
                        if (Interlocked.Decrement(ref copy.NumberOfClients) == 0)
                        {
                            Interlocked.Exchange(ref _state, new State());
                            copy.AllWebSocketsRemoved.TrySetResult(null);
                        }
                    }
                }
            }));
        }
Exemple #2
0
        public IDisposable TrackActions(AsyncQueue <Notification> notificationsQueue, IWebsocketWriter webSockerWriter)
        {
            var watcher = new ConnectedWatcher
            {
                NotificationsQueue = notificationsQueue,
                Writer             = webSockerWriter
            };

            lock (_watchersLock)
            {
                _watchers.TryAdd(watcher);

                if (_watchers.Count == 1)
                {
                    StartBackgroundWorkers();
                }
            }

            return(new DisposableAction(() =>
            {
                lock (_watchersLock)
                {
                    _watchers.TryRemove(watcher);

                    if (_watchers.Count == 0)
                    {
                        StopBackgroundWorkers();
                    }
                }
            }));
        }
        public IDisposable TrackActions(AsyncQueue <DynamicJsonValue> notificationsQueue, IWebsocketWriter webSocketWriter)
        {
            var watcher = new ConnectedWatcher
            {
                NotificationsQueue = notificationsQueue,
                Writer             = webSocketWriter
            };

            lock (_watchersLock)
            {
                Watchers.TryAdd(watcher);

                if (Watchers.Count == 1)
                {
                    StartBackgroundWorkers();
                }

                if (watcher.Writer is NotificationCenterWebSocketWriter)
                {
                    if (_websocketClients == 0)
                    {
                        TaskExecutor.CompleteAndReplace(ref _newWebSocket);
                    }
                    _websocketClients++;
                }
            }

            return(new DisposableAction(() =>
            {
                lock (_watchersLock)
                {
                    Watchers.TryRemove(watcher);

                    if (Watchers.Count == 0)
                    {
                        StopBackgroundWorkers();
                    }

                    if (watcher.Writer is NotificationCenterWebSocketWriter)
                    {
                        _websocketClients--;
                        if (_websocketClients == 0)
                        {
                            TaskExecutor.CompleteAndReplace(ref _allWebSocketsRemoved);
                        }
                    }
                }
            }));
        }