Skip to content

Commit

Permalink
Use lazy provider
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Jul 14, 2021
1 parent b9fa118 commit 0656062
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 63 deletions.
40 changes: 20 additions & 20 deletions NatTypeTester.ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace NatTypeTester.ViewModels
Expand All @@ -18,9 +19,9 @@ namespace NatTypeTester.ViewModels
)]
public class MainWindowViewModel : ViewModelBase, IScreen
{
public RoutingState Router { get; } = new();
public RoutingState Router => LazyServiceProvider.LazyGetRequiredService<RoutingState>();

public Config Config { get; }
public Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();

private readonly IEnumerable<string> _defaultServers = new HashSet<string>
{
Expand All @@ -34,43 +35,42 @@ public class MainWindowViewModel : ViewModelBase, IScreen
private SourceList<string> List { get; } = new();
public readonly IObservableCollection<string> StunServers = new ObservableCollectionExtended<string>();

public MainWindowViewModel(Config config)
public MainWindowViewModel()
{
Config = config;

LoadStunServer();
List.Connect()
.DistinctValues(x => x)
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(StunServers)
.Subscribe();
}

private async void LoadStunServer()
public void LoadStunServer()
{
foreach (var server in _defaultServers)
{
List.Add(server);
}
Config.StunServer = _defaultServers.First();

const string path = @"stun.txt";
Config.StunServer = _defaultServers.First();

if (!File.Exists(path))
Task.Run(() =>
{
return;
}
const string path = @"stun.txt";

using var sw = new StreamReader(path);
string line;
var stun = new StunServer();
while ((line = await sw.ReadLineAsync()) != null)
{
if (!string.IsNullOrWhiteSpace(line) && stun.Parse(line))
if (!File.Exists(path))
{
List.Add(stun.ToString());
return;
}
}

var stun = new StunServer();
foreach (var line in File.ReadLines(path))
{
if (!string.IsNullOrWhiteSpace(line) && stun.Parse(line))
{
List.Add(stun.ToString());
}
}
});
}
}
}
17 changes: 7 additions & 10 deletions NatTypeTester.ViewModels/RFC3489ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,34 @@ namespace NatTypeTester.ViewModels
public class RFC3489ViewModel : ViewModelBase, IRoutableViewModel
{
public string UrlPathSegment => @"RFC3489";
public IScreen HostScreen { get; }
public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();

private readonly Config _config;
private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();

[Reactive]
public ClassicStunResult Result3489 { get; set; }

public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }

public RFC3489ViewModel(IScreen hostScreen, Config config)
public RFC3489ViewModel()
{
HostScreen = hostScreen;
_config = config;

Result3489 = new ClassicStunResult { LocalEndPoint = new IPEndPoint(IPAddress.Any, 0) };
TestClassicNatType = ReactiveCommand.CreateFromTask(TestClassicNatTypeImpl);
}

private async Task TestClassicNatTypeImpl(CancellationToken token)
{
var server = new StunServer();
if (!server.Parse(_config.StunServer))
if (!server.Parse(Config.StunServer))
{
throw new Exception(@"Wrong STUN Server!");
}

using var proxy = ProxyFactory.CreateProxy(
_config.ProxyType,
Config.ProxyType,
Result3489.LocalEndPoint,
NetUtils.ParseEndpoint(_config.ProxyServer),
_config.ProxyUser, _config.ProxyPassword
NetUtils.ParseEndpoint(Config.ProxyServer),
Config.ProxyUser, Config.ProxyPassword
);

using var client = new StunClient3489(server.Hostname, server.Port, Result3489.LocalEndPoint, proxy);
Expand Down
17 changes: 7 additions & 10 deletions NatTypeTester.ViewModels/RFC5780ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,34 @@ namespace NatTypeTester.ViewModels
public class RFC5780ViewModel : ViewModelBase, IRoutableViewModel
{
public string UrlPathSegment => @"RFC5780";
public IScreen HostScreen { get; }
public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();

private readonly Config _config;
private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();

[Reactive]
public StunResult5389 Result5389 { get; set; }

public ReactiveCommand<Unit, Unit> DiscoveryNatType { get; }

public RFC5780ViewModel(IScreen hostScreen, Config config)
public RFC5780ViewModel()
{
HostScreen = hostScreen;
_config = config;

Result5389 = new StunResult5389 { LocalEndPoint = new IPEndPoint(IPAddress.Any, 0) };
DiscoveryNatType = ReactiveCommand.CreateFromTask(DiscoveryNatTypeImpl);
}

private async Task DiscoveryNatTypeImpl(CancellationToken token)
{
var server = new StunServer();
if (!server.Parse(_config.StunServer))
if (!server.Parse(Config.StunServer))
{
throw new Exception(@"Wrong STUN Server!");
}

using var proxy = ProxyFactory.CreateProxy(
_config.ProxyType,
Config.ProxyType,
Result5389.LocalEndPoint,
NetUtils.ParseEndpoint(_config.ProxyServer),
_config.ProxyUser, _config.ProxyPassword
NetUtils.ParseEndpoint(Config.ProxyServer),
Config.ProxyUser, Config.ProxyPassword
);

using var client = new StunClient5389UDP(server.Hostname, server.Port, Result5389.LocalEndPoint, proxy);
Expand Down
10 changes: 2 additions & 8 deletions NatTypeTester.ViewModels/SettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ namespace NatTypeTester.ViewModels
public class SettingViewModel : ViewModelBase, IRoutableViewModel
{
public string UrlPathSegment => @"Settings";
public IScreen HostScreen { get; }
public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();

public Config Config { get; }

public SettingViewModel(IScreen hostScreen, Config config)
{
HostScreen = hostScreen;
Config = config;
}
public Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
}
}
1 change: 1 addition & 0 deletions NatTypeTester.ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace NatTypeTester.ViewModels
{
public abstract class ViewModelBase : ReactiveObject, ISingletonDependency
{
public IAbpLazyServiceProvider LazyServiceProvider { get; set; } = null!;
}
}
7 changes: 3 additions & 4 deletions NatTypeTester/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Splat.Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows;
using Volo.Abp;
Expand Down Expand Up @@ -41,16 +42,14 @@ protected override async void OnExit(ExitEventArgs e)
private void Initialize(IServiceProvider serviceProvider)
{
_application.Initialize(serviceProvider);
serviceProvider.UseMicrosoftDependencyResolver();
}

private static IHost CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.UseAutofac()
.ConfigureServices((hostContext, services) =>
{
services.AddApplication<NatTypeTesterModule>();
})
.ConfigureServices((_, services) => services.AddApplication<NatTypeTesterModule>())
.Build();
}
}
Expand Down
19 changes: 10 additions & 9 deletions NatTypeTester/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using ModernWpf;
using ModernWpf.Controls;
using NatTypeTester.ViewModels;
Expand All @@ -12,15 +13,13 @@ namespace NatTypeTester
{
public partial class MainWindow : ISingletonDependency
{
public MainWindow(MainWindowViewModel viewModel,
RFC5780ViewModel rfc5780ViewModel,
RFC3489ViewModel rfc3489ViewModel,
SettingViewModel settingViewModel
)
public IServiceProvider ServiceProvider { get; set; } = null!;

public MainWindow(MainWindowViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
ThemeManager.Current.ApplicationTheme = null;
ThemeManager.Current.ApplicationTheme = default;

this.WhenActivated(d =>
{
Expand All @@ -44,7 +43,7 @@ SettingViewModel settingViewModel
{
if (args.EventArgs.IsSettingsSelected)
{
ViewModel.Router.Navigate.Execute(settingViewModel);
ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService<SettingViewModel>());
return;
}

Expand All @@ -57,17 +56,19 @@ SettingViewModel settingViewModel
{
case @"1":
{
ViewModel.Router.Navigate.Execute(rfc5780ViewModel);
ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService<RFC5780ViewModel>());
break;
}
case @"2":
{
ViewModel.Router.Navigate.Execute(rfc3489ViewModel);
ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService<RFC3489ViewModel>());
break;
}
}
}).DisposeWith(d);
NavigationView.SelectedItem = NavigationView.MenuItems.OfType<NavigationViewItem>().First();

ViewModel.LoadStunServer();
});
}
}
Expand Down
6 changes: 6 additions & 0 deletions NatTypeTester/NatTypeTesterModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NatTypeTester.Models;
using NatTypeTester.ViewModels;
using ReactiveUI;
Expand All @@ -23,5 +24,10 @@ public override void PreConfigureServices(ServiceConfigurationContext context)
Locator.CurrentMutable.InitializeSplat();
Locator.CurrentMutable.InitializeReactiveUI(RegistrationNamespace.Wpf);
}

public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.TryAddTransient<RoutingState>();
}
}
}
3 changes: 1 addition & 2 deletions NatTypeTester/Views/SettingView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ namespace NatTypeTester.Views
[UsedImplicitly]
public partial class SettingView : ITransientDependency
{
public SettingView(SettingViewModel viewModel)
public SettingView()
{
InitializeComponent();
ViewModel = viewModel;

this.WhenActivated(d =>
{
Expand Down

0 comments on commit 0656062

Please sign in to comment.