Exemple #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);
        }
        private void Handler_HasCome(object sender, Message msg)
        {
            _toServiceMessenger = Utils.GetAnswerMessenger(msg);
            switch ((ServiceOperation)msg.What)
            {
            case ServiceOperation.GetClientSettings:
                _callbacks.Dequeue(ServiceOperation.GetClientSettings, Utils.GetData <ConnectionCredentials>(msg));
                break;

            case ServiceOperation.GetIsConnected:
                _callbacks.Dequeue(ServiceOperation.GetIsConnected, Utils.GetData <ManagerConnectionState>(msg));
                break;

            case ServiceOperation.GetScenarios:
                _callbacks.Dequeue(ServiceOperation.GetScenarios, Utils.GetData <ScenarioInfo[]>(msg));
                break;

            case ServiceOperation.GetNotifications:
                _callbacks.Dequeue(ServiceOperation.GetNotifications, Utils.GetData <LazuriteNotification[]>(msg));
                break;

            case ServiceOperation.ConnectionLost:
                ConnectionLost?.Invoke();
                break;

            case ServiceOperation.ConnectionRestored:
                ConnectionRestored?.Invoke();
                break;

            case ServiceOperation.CredentialsInvalid:
                LoginOrPasswordInvalid?.Invoke();
                break;

            case ServiceOperation.CredentialsLoaded:
                CredentialsLoaded?.Invoke();
                break;

            case ServiceOperation.NeedClientSettings:
                NeedClientSettings?.Invoke();
                break;

            case ServiceOperation.NeedRefresh:
                NeedRefresh?.Invoke();
                break;

            case ServiceOperation.ScenariosChanged:
                ScenariosChanged?.Invoke(Utils.GetData <ScenarioInfo[]>(msg));
                break;

            case ServiceOperation.SecretCodeInvalid:
                SecretCodeInvalid?.Invoke();
                break;

            case ServiceOperation.ConnectionError:
                ConnectionError?.Invoke();
                break;
            }
        }