From 2a74f7d25df3a4686b59ba2dde35b674809ed00a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 4 Nov 2021 08:57:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BBtab=E4=B8=BA=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.UI/App.xaml.cs | 2 +- FastGithub.UI/IssuesWebbrowser.xaml | 12 ++++ FastGithub.UI/IssuesWebbrowser.xaml.cs | 41 ++++++++++++++ FastGithub.UI/MainWindow.xaml | 2 +- FastGithub.UI/MainWindow.xaml.cs | 25 +-------- FastGithub.UI/UdpLog.cs | 8 +++ FastGithub.UI/UdpLogListBox.xaml.cs | 37 ++----------- FastGithub.UI/UdpLogger.cs | 77 ++++++++++++++++++++++++++ FastGithub.UI/UdpLoggerPort.cs | 49 ---------------- 9 files changed, 147 insertions(+), 106 deletions(-) create mode 100644 FastGithub.UI/IssuesWebbrowser.xaml create mode 100644 FastGithub.UI/IssuesWebbrowser.xaml.cs create mode 100644 FastGithub.UI/UdpLogger.cs delete mode 100644 FastGithub.UI/UdpLoggerPort.cs diff --git a/FastGithub.UI/App.xaml.cs b/FastGithub.UI/App.xaml.cs index a6c4b6b..527a1bc 100644 --- a/FastGithub.UI/App.xaml.cs +++ b/FastGithub.UI/App.xaml.cs @@ -89,7 +89,7 @@ namespace FastGithub.UI var startInfo = new ProcessStartInfo { FileName = fileName, - Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id} UdpLoggerPort={UdpLoggerPort.Value}", + Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id} UdpLoggerPort={UdpLogger.Port}", UseShellExecute = false, CreateNoWindow = true }; diff --git a/FastGithub.UI/IssuesWebbrowser.xaml b/FastGithub.UI/IssuesWebbrowser.xaml new file mode 100644 index 0000000..ffc0ed7 --- /dev/null +++ b/FastGithub.UI/IssuesWebbrowser.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/FastGithub.UI/IssuesWebbrowser.xaml.cs b/FastGithub.UI/IssuesWebbrowser.xaml.cs new file mode 100644 index 0000000..92b64e7 --- /dev/null +++ b/FastGithub.UI/IssuesWebbrowser.xaml.cs @@ -0,0 +1,41 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; + +namespace FastGithub.UI +{ + /// + /// IssuesWebbrowser.xaml 的交互逻辑 + /// + public partial class IssuesWebbrowser : UserControl + { + public IssuesWebbrowser() + { + InitializeComponent(); + + this.NavigateIssueHtml(); + this.webBrowser.AddHandler(KeyDownEvent, new RoutedEventHandler(WebBrowser_KeyDown), true); + } + + /// + /// 拦截F5 + /// + /// + /// + private void WebBrowser_KeyDown(object sender, RoutedEventArgs e) + { + var @event = (KeyEventArgs)e; + if (@event.Key == Key.F5) + { + this.NavigateIssueHtml(); + } + } + + private void NavigateIssueHtml() + { + var resource = Application.GetResourceStream(new Uri("Resource/issue.html", UriKind.Relative)); + this.webBrowser.NavigateToStream(resource.Stream); + } + } +} diff --git a/FastGithub.UI/MainWindow.xaml b/FastGithub.UI/MainWindow.xaml index 97b6371..e85179d 100644 --- a/FastGithub.UI/MainWindow.xaml +++ b/FastGithub.UI/MainWindow.xaml @@ -158,7 +158,7 @@ - + diff --git a/FastGithub.UI/MainWindow.xaml.cs b/FastGithub.UI/MainWindow.xaml.cs index de7bc5f..709299b 100644 --- a/FastGithub.UI/MainWindow.xaml.cs +++ b/FastGithub.UI/MainWindow.xaml.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using System.IO; using System.Windows; -using System.Windows.Input; using System.Windows.Interop; namespace FastGithub.UI @@ -49,27 +48,9 @@ namespace FastGithub.UI { var version = FileVersionInfo.GetVersionInfo(fileName); this.Title = $"{FAST_GITHUB} v{version.ProductVersion}"; - } - - this.webBrowserIssue.AddHandler(KeyDownEvent, new RoutedEventHandler(WebBrowser_KeyDown), true); - var resource = Application.GetResourceStream(new Uri("Resource/issue.html", UriKind.Relative)); - this.webBrowserIssue.NavigateToStream(resource.Stream); - } - - /// - /// 拦截F5 - /// - /// - /// - private void WebBrowser_KeyDown(object sender, RoutedEventArgs e) - { - var @event = (KeyEventArgs)e; - if (@event.Key == Key.F5) - { - var resource = Application.GetResourceStream(new Uri("Resource/issue.html", UriKind.Relative)); - this.webBrowserIssue.NavigateToStream(resource.Stream); - } - } + } + } + /// /// 拦截最小化事件 diff --git a/FastGithub.UI/UdpLog.cs b/FastGithub.UI/UdpLog.cs index 8eba2f0..99c3f90 100644 --- a/FastGithub.UI/UdpLog.cs +++ b/FastGithub.UI/UdpLog.cs @@ -1,4 +1,8 @@ using System; +using System.Collections.Generic; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; using System.Windows; namespace FastGithub.UI @@ -15,10 +19,14 @@ namespace FastGithub.UI 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.cs b/FastGithub.UI/UdpLogListBox.xaml.cs index 2c1a3f5..47038d3 100644 --- a/FastGithub.UI/UdpLogListBox.xaml.cs +++ b/FastGithub.UI/UdpLogListBox.xaml.cs @@ -1,10 +1,4 @@ -using Newtonsoft.Json; -using System; -using System.Collections.ObjectModel; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; +using System.Collections.ObjectModel; using System.Windows.Controls; namespace FastGithub.UI @@ -14,9 +8,6 @@ namespace FastGithub.UI /// public partial class UdpLogListBox : UserControl { - private readonly byte[] buffer = new byte[ushort.MaxValue]; - private readonly Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp); - public ObservableCollection LogList { get; } = new ObservableCollection(); public UdpLogListBox() @@ -28,36 +19,16 @@ namespace FastGithub.UI } private async void InitUdpLoggerAsync() - { - this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value)); + { while (this.Dispatcher.HasShutdownStarted == false) { - var log = await this.GetUdpLogAsync(); + var log = await UdpLogger.GetUdpLogAsync(); if (log != null) { this.LogList.Add(log); } } - } - - private async Task GetUdpLogAsync() - { - EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); - var taskCompletionSource = new TaskCompletionSource(); - this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, this.EndReceiveFrom, taskCompletionSource); - var length = await taskCompletionSource.Task; - - var json = Encoding.UTF8.GetString(buffer, 0, length); - return JsonConvert.DeserializeObject(json); - } - - private void EndReceiveFrom(IAsyncResult ar) - { - EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); - var length = this.socket.EndReceiveFrom(ar, ref remoteEP); - var taskCompletionSource = (TaskCompletionSource)ar.AsyncState; - taskCompletionSource.TrySetResult(length); - } + } private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e) { diff --git a/FastGithub.UI/UdpLogger.cs b/FastGithub.UI/UdpLogger.cs new file mode 100644 index 0000000..cd96af3 --- /dev/null +++ b/FastGithub.UI/UdpLogger.cs @@ -0,0 +1,77 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace FastGithub.UI +{ + static class UdpLogger + { + private static readonly byte[] buffer = new byte[ushort.MaxValue]; + private static readonly Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp); + + /// + /// 获取日志端口 + /// + public static int Port { get; } = GetAvailableUdpPort(38457); + + + static UdpLogger() + { + socket.Bind(new IPEndPoint(IPAddress.Loopback, Port)); + } + + /// + /// 获取可用的随机Udp端口 + /// + /// + /// + /// + private static int GetAvailableUdpPort(int minValue, AddressFamily addressFamily = AddressFamily.InterNetwork) + { + var hashSet = new HashSet(); + var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners(); + + foreach (var endpoint in tcpListeners) + { + if (endpoint.AddressFamily == addressFamily) + { + hashSet.Add(endpoint.Port); + } + } + + for (var port = minValue; port < IPEndPoint.MaxPort; port++) + { + if (hashSet.Contains(port) == false) + { + return port; + } + } + + throw new ArgumentException("当前无可用的端口"); + } + + public static async Task GetUdpLogAsync() + { + EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); + var taskCompletionSource = new TaskCompletionSource(); + socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, EndReceiveFrom, taskCompletionSource); + var length = await taskCompletionSource.Task; + + var json = Encoding.UTF8.GetString(buffer, 0, length); + return JsonConvert.DeserializeObject(json); + } + + private static void EndReceiveFrom(IAsyncResult ar) + { + EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); + var length = socket.EndReceiveFrom(ar, ref remoteEP); + var taskCompletionSource = (TaskCompletionSource)ar.AsyncState; + taskCompletionSource.TrySetResult(length); + } + } +} diff --git a/FastGithub.UI/UdpLoggerPort.cs b/FastGithub.UI/UdpLoggerPort.cs deleted file mode 100644 index 4861ca2..0000000 --- a/FastGithub.UI/UdpLoggerPort.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.NetworkInformation; -using System.Net.Sockets; - -namespace FastGithub.UI -{ - /// - /// udp日志工具类 - /// - static class UdpLoggerPort - { - /// - /// 获取日志端口 - /// - public static int Value { get; } = GetAvailableUdpPort(38457); - - /// - /// 获取可用的随机Udp端口 - /// - /// - /// - /// - private static int GetAvailableUdpPort(int minValue, AddressFamily addressFamily = AddressFamily.InterNetwork) - { - var hashSet = new HashSet(); - var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners(); - - foreach (var endpoint in tcpListeners) - { - if (endpoint.AddressFamily == addressFamily) - { - hashSet.Add(endpoint.Port); - } - } - - for (var port = minValue; port < IPEndPoint.MaxPort; port++) - { - if (hashSet.Contains(port) == false) - { - return port; - } - } - - throw new ArgumentException("当前无可用的端口"); - } - } -}