From 4db6ab6b3feddba355e22b388e6aebbe84e3f7a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Wed, 3 Nov 2021 22:36:13 +0800 Subject: [PATCH] nullable --- Directory.Build.props | 4 +- .../FastGithub.Configuration.csproj | 3 - .../FastGithub.DomainResolve.csproj | 3 - .../FastGithub.FlowAnalyze.csproj | 12 +-- FastGithub.Http/FastGithub.Http.csproj | 5 +- .../FastGithub.HttpServer.csproj | 1 - .../FastGithub.PacketIntercept.csproj | 3 +- FastGithub.UI/App.xaml.cs | 60 ++++++------- FastGithub.UI/FastGithub.UI.csproj | 54 ++++++------ FastGithub.UI/FlowChart.xaml.cs | 17 ++-- FastGithub.UI/LogLevel.cs | 15 ++++ FastGithub.UI/MainWindow.xaml.cs | 4 +- FastGithub.UI/UdpLog.cs | 10 +-- FastGithub.UI/UdpLogListBox.xaml | 4 +- FastGithub.UI/UdpLogListBox.xaml.cs | 19 ++-- FastGithub/FastGithub.csproj | 86 +++++++++---------- 16 files changed, 156 insertions(+), 144 deletions(-) create mode 100644 FastGithub.UI/LogLevel.cs diff --git a/Directory.Build.props b/Directory.Build.props index b905fe7..01237bc 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,10 +1,10 @@ 2.0.5 + enable net6.0 true - true - true + true github加速神器 https://github.com/dotnetcore/FastGithub win-x64 diff --git a/FastGithub.Configuration/FastGithub.Configuration.csproj b/FastGithub.Configuration/FastGithub.Configuration.csproj index f298d87..6eee387 100644 --- a/FastGithub.Configuration/FastGithub.Configuration.csproj +++ b/FastGithub.Configuration/FastGithub.Configuration.csproj @@ -1,7 +1,4 @@  - - enable - diff --git a/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj b/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj index f665766..f7441b6 100644 --- a/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj +++ b/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj @@ -1,7 +1,4 @@  - - enable - diff --git a/FastGithub.FlowAnalyze/FastGithub.FlowAnalyze.csproj b/FastGithub.FlowAnalyze/FastGithub.FlowAnalyze.csproj index 943784e..65cbe96 100644 --- a/FastGithub.FlowAnalyze/FastGithub.FlowAnalyze.csproj +++ b/FastGithub.FlowAnalyze/FastGithub.FlowAnalyze.csproj @@ -1,11 +1,7 @@ - - net6.0 - enable - - - - - + + + + diff --git a/FastGithub.Http/FastGithub.Http.csproj b/FastGithub.Http/FastGithub.Http.csproj index 49e9287..204c90e 100644 --- a/FastGithub.Http/FastGithub.Http.csproj +++ b/FastGithub.Http/FastGithub.Http.csproj @@ -1,8 +1,5 @@  - - enable - - + diff --git a/FastGithub.HttpServer/FastGithub.HttpServer.csproj b/FastGithub.HttpServer/FastGithub.HttpServer.csproj index d702999..066d9dd 100644 --- a/FastGithub.HttpServer/FastGithub.HttpServer.csproj +++ b/FastGithub.HttpServer/FastGithub.HttpServer.csproj @@ -1,7 +1,6 @@  - enable true diff --git a/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj b/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj index 913b4f2..e738033 100644 --- a/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj +++ b/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj @@ -1,7 +1,6 @@  - enable true @@ -10,7 +9,7 @@ - + diff --git a/FastGithub.UI/App.xaml.cs b/FastGithub.UI/App.xaml.cs index 2fafef7..a6c4b6b 100644 --- a/FastGithub.UI/App.xaml.cs +++ b/FastGithub.UI/App.xaml.cs @@ -13,27 +13,13 @@ namespace FastGithub.UI /// public partial class App : Application { - private Mutex globalMutex; + private readonly Mutex globalMutex; + private readonly bool isFirstInstance; - /// - /// 程序启动 - /// - /// - protected override void OnStartup(StartupEventArgs e) + public App() { - this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out var firstInstance); - if (firstInstance == false) - { - this.Shutdown(); - } - else - { - AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; - SetWebBrowserVersion(9000); - StartFastGithub(); - } - - base.OnStartup(e); + this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out this.isFirstInstance); + AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; } /// @@ -42,25 +28,41 @@ namespace FastGithub.UI /// /// /// - private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) + private static Assembly? OnAssemblyResolve(object sender, ResolveEventArgs args) { var name = new AssemblyName(args.Name).Name; - return name.EndsWith(".resources") ? null : LoadAssembly(name); - } + if (name.EndsWith(".resources")) + { + return default; + } - /// - /// 从资源加载程序集 - /// - /// - /// - private static Assembly LoadAssembly(string name) - { var stream = GetResourceStream(new Uri($"Resource/{name}.dll", UriKind.Relative)).Stream; var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); return Assembly.Load(buffer); } + + /// + /// 程序启动 + /// + /// + protected override void OnStartup(StartupEventArgs e) + { + if (this.isFirstInstance == false) + { + this.Shutdown(); + } + else + { + StartFastGithub(); + SetWebBrowserVersion(9000); + } + + base.OnStartup(e); + } + + /// /// 设置浏览器版本 /// diff --git a/FastGithub.UI/FastGithub.UI.csproj b/FastGithub.UI/FastGithub.UI.csproj index 8fac8f3..77b647b 100644 --- a/FastGithub.UI/FastGithub.UI.csproj +++ b/FastGithub.UI/FastGithub.UI.csproj @@ -1,32 +1,34 @@  - - WinExe - net45 - true - true - MIT - app.manifest - app.ico - False - + + true + true + WinExe + 8.0 + enable + net45 + app.ico + app.manifest + MIT + False + - - - - + + + + - - - + + + + + + + all + + + all + + - - - all - - - all - - - diff --git a/FastGithub.UI/FlowChart.xaml.cs b/FastGithub.UI/FlowChart.xaml.cs index 6052e8d..bbe6f55 100644 --- a/FastGithub.UI/FlowChart.xaml.cs +++ b/FastGithub.UI/FlowChart.xaml.cs @@ -1,5 +1,6 @@ using LiveCharts; using LiveCharts.Wpf; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; @@ -40,18 +41,18 @@ namespace FastGithub.UI this.Series.Add(this.readSeries); this.Series.Add(this.writeSeries); - DataContext = this; + this.DataContext = this; this.InitFlowChart(); } private async void InitFlowChart() { - var httpClient = new HttpClient(); - while (true) + using var httpClient = new HttpClient(); + while (this.Dispatcher.HasShutdownStarted == false) { try { - await this.GetFlowStatisticsAsync(httpClient); + await this.FlushFlowStatisticsAsync(httpClient); } catch (Exception) { @@ -63,11 +64,15 @@ namespace FastGithub.UI } } - private async Task GetFlowStatisticsAsync(HttpClient httpClient) + private async Task FlushFlowStatisticsAsync(HttpClient httpClient) { var response = await httpClient.GetAsync("http://127.0.0.1/flowStatistics"); var json = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); - var flowStatistics = Newtonsoft.Json.JsonConvert.DeserializeObject(json); + var flowStatistics = JsonConvert.DeserializeObject(json); + if (flowStatistics == null) + { + return; + } this.textBlockRead.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalRead); this.textBlockWrite.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalWrite); diff --git a/FastGithub.UI/LogLevel.cs b/FastGithub.UI/LogLevel.cs new file mode 100644 index 0000000..2b1e07d --- /dev/null +++ b/FastGithub.UI/LogLevel.cs @@ -0,0 +1,15 @@ +namespace FastGithub.UI +{ + /// + /// 日志等级 + /// + public enum LogLevel + { + Verbose, + Debug, + Information, + Warning, + Error, + Fatal + } +} diff --git a/FastGithub.UI/MainWindow.xaml.cs b/FastGithub.UI/MainWindow.xaml.cs index ffade81..de7bc5f 100644 --- a/FastGithub.UI/MainWindow.xaml.cs +++ b/FastGithub.UI/MainWindow.xaml.cs @@ -12,7 +12,7 @@ namespace FastGithub.UI /// public partial class MainWindow : Window { - private System.Windows.Forms.NotifyIcon notifyIcon; + private readonly System.Windows.Forms.NotifyIcon notifyIcon; private const string FAST_GITHUB = "FastGithub"; private const string PROJECT_URI = "https://github.com/dotnetcore/FastGithub"; @@ -78,7 +78,7 @@ namespace FastGithub.UI protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); - var hwndSource = PresentationSource.FromVisual(this) as HwndSource; + var hwndSource = (HwndSource)PresentationSource.FromVisual(this); hwndSource.AddHook(WndProc); IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) diff --git a/FastGithub.UI/UdpLog.cs b/FastGithub.UI/UdpLog.cs index 6317665..8eba2f0 100644 --- a/FastGithub.UI/UdpLog.cs +++ b/FastGithub.UI/UdpLog.cs @@ -1,6 +1,5 @@ using System; using System.Windows; -using System.Windows.Media; namespace FastGithub.UI { @@ -8,17 +7,18 @@ namespace FastGithub.UI { public DateTime Timestamp { get; set; } - public string Level { get; set; } + public LogLevel Level { get; set; } - public string Message { get; set; } + public string Message { get; set; } = string.Empty; - public string SourceContext { get; set; } + public string SourceContext { get; set; } = string.Empty; - public string Color => this.Level == "Information" ? "#333" : "IndianRed"; + public string Color => this.Level <= LogLevel.Information ? "#333" : "IndianRed"; public void SetToClipboard() { Clipboard.SetText($"{this.Timestamp:yyyy-MM-dd HH:mm:ss.fff}\r\n{this.Message}"); } } + } diff --git a/FastGithub.UI/UdpLogListBox.xaml b/FastGithub.UI/UdpLogListBox.xaml index 5b8151c..4e5bd88 100644 --- a/FastGithub.UI/UdpLogListBox.xaml +++ b/FastGithub.UI/UdpLogListBox.xaml @@ -34,8 +34,8 @@ - - + + diff --git a/FastGithub.UI/UdpLogListBox.xaml.cs b/FastGithub.UI/UdpLogListBox.xaml.cs index be1421b..2c1a3f5 100644 --- a/FastGithub.UI/UdpLogListBox.xaml.cs +++ b/FastGithub.UI/UdpLogListBox.xaml.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.ObjectModel; using System.Net; using System.Net.Sockets; @@ -21,22 +22,25 @@ namespace FastGithub.UI public UdpLogListBox() { InitializeComponent(); - DataContext = this; + this.DataContext = this; this.InitUdpLoggerAsync(); } private async void InitUdpLoggerAsync() - { + { this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value)); - while (true) + while (this.Dispatcher.HasShutdownStarted == false) { var log = await this.GetUdpLogAsync(); - this.LogList.Add(log); + if (log != null) + { + this.LogList.Add(log); + } } } - private async Task GetUdpLogAsync() + private async Task GetUdpLogAsync() { EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); var taskCompletionSource = new TaskCompletionSource(); @@ -44,10 +48,9 @@ namespace FastGithub.UI var length = await taskCompletionSource.Task; var json = Encoding.UTF8.GetString(buffer, 0, length); - return Newtonsoft.Json.JsonConvert.DeserializeObject(json); + return JsonConvert.DeserializeObject(json); } - private void EndReceiveFrom(IAsyncResult ar) { EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index f85a77e..193c4a5 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -1,52 +1,52 @@  - - enable - fastgithub - Exe - MIT - app.manifest - + + fastgithub + Exe + MIT + app.manifest + true + - - - - - - - - + + + + + + + + - - - - - - + + + + + + - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + - - - PreserveNewest - - - PreserveNewest - - + + + PreserveNewest + + + PreserveNewest + + - - - + + +