Esempio n. 1
0
        private void HandleConnectionClose()
        {
            // change channel state
            var tryReconnect = (State == ChannelState.Connected || State == ChannelState.Reconnecting) && !_isClosedByClient;

            if (tryReconnect)
            {
                SetChannelState(ChannelState.Reconnecting);
                Task.Run(async() => await Reconnect());
            }
            else
            {
                SetChannelState(ChannelState.Disconnected);
            }

            // fail pending requests
            var exception = new DeviceHiveException(State == ChannelState.Connected ?
                                                    "WebSocket connection was closed!" : "Could not open a WebSocket connection!");

            lock (_requests)
            {
                foreach (var requestInfo in _requests.Where(r => !r.Value.Task.IsCompleted))
                {
                    requestInfo.Value.SetException(exception);
                }
                _requests.Clear();
            }

            // unblock waiting tasks
            _closeTaskCompletionSource.TrySetResult(true);
            _authTaskCompletionSource.TrySetException(exception);
        }
        private void HandleConnectionClose()
        {
            // change channel state
            var tryReconnect = (State == ChannelState.Connected || State == ChannelState.Reconnecting) && !_isClosedByClient;
            if (tryReconnect)
            {
                SetChannelState(ChannelState.Reconnecting);
                Task.Run(async () => await Reconnect());
            }
            else
            {
                SetChannelState(ChannelState.Disconnected);
            }

            // fail pending requests
            var exception = new DeviceHiveException(State == ChannelState.Connected ?
                "WebSocket connection was closed!" : "Could not open a WebSocket connection!");
            lock (_requests)
            {
                foreach (var requestInfo in _requests.Where(r => !r.Value.Task.IsCompleted))
                    requestInfo.Value.SetException(exception);
                _requests.Clear();
            }

            // unblock waiting tasks
            _closeTaskCompletionSource.TrySetResult(true);
            _authTaskCompletionSource.TrySetException(exception);
        }