private async void Button_Click(object sender, System.Windows.RoutedEventArgs e) { var btn = (Button)sender; try { _vm.IsUIEnabled = false; if (!GameIdGenerator.Validate(_vm.GameID)) { _vm.StatusMsg = "Invalid Game ID"; return; } _myWindow.Cursor = Cursors.Wait; var scope = await Task.Run(_vm.ValidateGameID); if (scope != null) { _ = _vm.IdTaskSource.TrySetResult(scope); } } catch { _vm.StatusMsg = "Failed to Connect"; return; } finally { _vm.IsUIEnabled = true; _myWindow.Cursor = Cursors.Arrow; txt_GameID.Text = ""; _ = txt_GameID.Focus(); } }
public async Task <IServiceScope> GetValidGameScope() { while (true) { Console.Write("Enter Game ID: "); var gameID = Console.ReadLine(); var isValid = GameIdGenerator.Validate(gameID); if (!isValid) { Console.Write("Invalid Game ID. "); continue; } var disposeScope = true; var scope = _serviceProvider.CreateScope(); var idService = scope.ServiceProvider.GetRequiredService <GameIdService>(); idService.GameID = gameID; var ardClient = scope.ServiceProvider.GetRequiredService <IArdNetClient>(); using (var tokenSrc = new CancellationTokenSource(_connectionTimeout)) { try { var endptTask = ardClient.ConnectAsync(tokenSrc.Token); Console.WriteLine("Connecting..."); var endpt = await endptTask; if (endpt != null && !tokenSrc.IsCancellationRequested) { Console.WriteLine("Connected."); disposeScope = false; return(scope); } else { Console.Write("Cannot connect to the target host. "); } } catch (OperationCanceledException) { //noop //continue search Console.Write("Cannot connect to the target host. "); } finally { if (disposeScope) { scope.Dispose(); } } } } }
static ArdNetClientConfig configFactory(IServiceProvider sp) { var ardConfig = sp.GetRequiredService <IOptions <ArdNetBasicConfig> >().Value; var IpResolver = sp.GetRequiredService <IIpResolverService>(); var gameIdService = sp.GetRequiredService <GameIdService>(); var gameIdStr = gameIdService.GameID; if (!GameIdGenerator.Validate(gameIdStr)) { throw new InvalidOperationException("Invalid game ID string"); } int gameID = int.Parse(gameIdStr); var appID = ardConfig.AppID; var myIP = IpResolver.GetIP(); var serverPort = gameID; var clientPort = ardConfig.ClientPort; return(new ArdNetClientConfig(appID, myIP, serverPort, clientPort)); }