Exemplo n.º 1
0
        public async Task <bool> Connect(Connection selectedConnection)
        {
            bool result = false;

            if (currentConnection != null)
            {
                service.Disconnect(currentConnection);
                await Task.Delay(1000);
            }

            try
            {
                bool worked = false;
                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    appSettings.CurrentConnectionState = (int)ConnectionState.Connecting;
                });

                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    worked = await service.Connect(selectedConnection);

                    if (!worked)
                    {
                        appSettings.CurrentConnectionState = (int)ConnectionState.CouldNotConnect;
                        telemetry.TrackEvent("VirtualShieldConnectionFail");
                    }
                    else
                    {
                        appSettings.CurrentConnectionState = (int)ConnectionState.Connected;
                        currentConnection = selectedConnection;
                        appSettings.PreviousConnectionName = currentConnection.DisplayName;
                        telemetry.TrackEvent("VirtualShieldConnectionSuccess");
                        result = true;
                    }
                });

                if (service.CharEventHandlerCount == 0)
                {
                    service.CharReceived += c => manager.OnCharsReceived(c.ToString());
                    service.CharEventHandlerCount++;
                }
            }
            catch (Exception e)
            {
                this.Log("!:error connecting:" + e.Message);

                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    appSettings.CurrentConnectionState = (int)ConnectionState.CouldNotConnect;
                });

                telemetry.TrackException(e);
            }

            return(result);
        }