Beispiel #1
0
        private bool HandleExceptions(Action action)
        {
            var  cancellationToken = _operationCancellationTokenSource.Token;
            bool success           = false;

            try
            {
                if (_credentials != null && (_serviceClient?.IsClosedOrFaulted ?? false))
                {
                    _serviceClient = ClientsManager.Create(_credentials.Value);
                }
                if (ConnectionState == ManagerConnectionState.Disconnected)
                {
                    ConnectionState = ManagerConnectionState.Connecting;
                }
                action();
                success = true;
                if (ConnectionState != ManagerConnectionState.Connected)
                {
                    ConnectionState = ManagerConnectionState.Connected;
                    ConnectionRestored?.Invoke();
                }
            }
            catch (System.Exception e)
            {
                if (!cancellationToken.IsCancellationRequested)
                {
                    if (SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.AccessDenied))
                    {
                        LoginOrPasswordInvalid?.Invoke();
                    }
                    //if data is wrong or secretKey.Length is wrong
                    else if (
                        SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.DecryptionError) ||
                        e is SerializationException ||
                        e is DecryptException ||
                        e.Message == "Key length not 128/192/256 bits.")
                    {
                        SecretCodeInvalid?.Invoke();
                    }
                    else if (ConnectionState != ManagerConnectionState.Connected)
                    {
                        ConnectionLost?.Invoke();
                    }
                    else
                    {
                        ConnectionError?.Invoke();
                    }
                    ConnectionState = ManagerConnectionState.Disconnected;
                    success         = false;
                }
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return(false);
            }
            return(success);
        }
Beispiel #2
0
 private void RecreateConnection()
 {
     ConnectionState = ManagerConnectionState.Connecting;
     if (_serviceClient != null)
     {
         _serviceClient.Close();
     }
     _serviceClient = ClientsManager.Create(_credentials.Value);
 }
Beispiel #3
0
 public void StopListenChanges()
 {
     _listenersCancellationTokenSource?.Cancel();
     _serviceClient?.Close();
     ConnectionState = ManagerConnectionState.Disconnected;
 }