private static string GetConnectionDetail(Connection connection) { if (null == connection.Source) { return string.Empty; } var deviceInformation = connection.Source as DeviceInformation; if ( deviceInformation != null ) { return deviceInformation.Id; } var endpointPair = connection.Source as EndpointPair; if ( endpointPair != null ) { return string.Format("{0}:{1}", endpointPair.RemoteHostName, endpointPair.RemoteServiceName); } return connection.Source.ToString(); }
public async Task<bool> Connect(Connection selectedConnection) { var result = false; if (this.currentConnection != null) { this.service.Disconnect(this.currentConnection); await Task.Delay(1000); } try { var worked = false; this.connectionStopwatch.Reset(); this.connectionStopwatch.Start(); await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { this.appSettings.CurrentConnectionState = (int)ConnectionState.Connecting; App.Telemetry.Context.Properties["connection.state"] = ConnectionState.Connecting.ToString(); App.Telemetry.Context.Properties["connection.name"] = string.Format( "{0:X}", selectedConnection.DisplayName.GetHashCode()); App.Telemetry.Context.Properties["connection.detail"] = string.Format( "{0:X}", GetConnectionDetail(selectedConnection).GetHashCode()); }); await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, async () => { App.Telemetry.TrackEvent("Connection_Attempt"); worked = await this.service.Connect(selectedConnection); this.connectionStopwatch.Stop(); if (!worked) { this.appSettings.CurrentConnectionState = (int)ConnectionState.CouldNotConnect; App.Telemetry.Context.Properties["connection.state"] = "Failed"; } else { this.appSettings.CurrentConnectionState = (int)ConnectionState.Connected; this.currentConnection = selectedConnection; this.appSettings.PreviousConnectionName = this.currentConnection.DisplayName; App.Telemetry.Context.Properties["connection.state"] = ConnectionState.Connected.ToString(); result = true; } App.Telemetry.TrackEvent("Connection"); }); if (this.service.CharEventHandlerCount == 0) { this.service.CharReceived += c => this.manager.OnCharsReceived(c.ToString()); this.service.CharEventHandlerCount++; } } catch (Exception e) { this.Log("!:error connecting:" + e.Message); await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { this.appSettings.CurrentConnectionState = (int)ConnectionState.CouldNotConnect; }); App.Telemetry.TrackException(e); } return result; }
public async Task<bool> Connect(Connection selectedConnection) { bool result = false; if (currentConnection != null) { service.Disconnect(currentConnection); await Task.Delay(1000); } try { await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { appSettings.CurrentConnectionState = (int)ConnectionState.Connecting; }); var worked = await service.Connect(selectedConnection); await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { //Refresh.IsEnabled = true; 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; }
public async void Disconnect() { if (this.currentConnection != null) { await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { this.appSettings.CurrentConnectionState = (int)ConnectionState.Disconnecting; App.Telemetry.Context.Properties["connection.state"] = ConnectionState.Disconnecting.ToString(); }); this.service.Disconnect(this.currentConnection); this.currentConnection = null; await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { this.appSettings.CurrentConnectionState = (int)ConnectionState.NotConnected; App.Telemetry.Context.Properties["connection.state"] = ConnectionState.NotConnected.ToString(); }); App.Telemetry.TrackEvent("Disconnect"); } }
public async void Disconnect() { if (currentConnection != null) { await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { appSettings.CurrentConnectionState = (int)ConnectionState.Disconnecting; }); service.Disconnect(currentConnection); currentConnection = null; await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { appSettings.CurrentConnectionState = (int)ConnectionState.NotConnected; }); telemetry.TrackEvent("VirtualShieldDisconnect"); } }