diff --git a/FastGithub.UI/App.xaml.cs b/FastGithub.UI/App.xaml.cs index 66cbf36..2fafef7 100644 --- a/FastGithub.UI/App.xaml.cs +++ b/FastGithub.UI/App.xaml.cs @@ -87,7 +87,7 @@ namespace FastGithub.UI var startInfo = new ProcessStartInfo { FileName = fileName, - Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id}", + Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id} UdpLoggerPort={UdpLoggerPort.Value}", UseShellExecute = false, CreateNoWindow = true }; diff --git a/FastGithub.UI/FastGithub.UI.csproj b/FastGithub.UI/FastGithub.UI.csproj index d13fa68..fec018e 100644 --- a/FastGithub.UI/FastGithub.UI.csproj +++ b/FastGithub.UI/FastGithub.UI.csproj @@ -76,10 +76,16 @@ MSBuild:Compile Designer + + MSBuild:Compile Designer + + Designer + MSBuild:Compile + MSBuild:Compile Designer @@ -92,6 +98,9 @@ FlowChart.xaml + + LogListBox.xaml + MainWindow.xaml Code diff --git a/FastGithub.UI/LogListBox.xaml b/FastGithub.UI/LogListBox.xaml new file mode 100644 index 0000000..7f10abc --- /dev/null +++ b/FastGithub.UI/LogListBox.xaml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + diff --git a/FastGithub.UI/LogListBox.xaml.cs b/FastGithub.UI/LogListBox.xaml.cs new file mode 100644 index 0000000..7e0b51c --- /dev/null +++ b/FastGithub.UI/LogListBox.xaml.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.ObjectModel; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Windows.Controls; + +namespace FastGithub.UI +{ + /// + /// LogListBox.xaml 的交互逻辑 + /// + public partial class LogListBox : 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 LogListBox() + { + InitializeComponent(); + DataContext = this; + + this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value)); + this.BeginReceiveFrom(); + } + + private void BeginReceiveFrom() + { + EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); + this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, this.EndReceiveFrom, null); + } + + private void EndReceiveFrom(IAsyncResult ar) + { + EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); + var length = this.socket.EndReceiveFrom(ar, ref remoteEP); + var json = Encoding.UTF8.GetString(buffer, 0, length); + var log = Newtonsoft.Json.JsonConvert.DeserializeObject(json); + this.Dispatcher.Invoke(() => this.LogList.Add(log)); + this.BeginReceiveFrom(); + } + } +} diff --git a/FastGithub.UI/MainWindow.xaml b/FastGithub.UI/MainWindow.xaml index cab2e39..97b193a 100644 --- a/FastGithub.UI/MainWindow.xaml +++ b/FastGithub.UI/MainWindow.xaml @@ -150,6 +150,12 @@ + + + + + + diff --git a/FastGithub.UI/UdpLog.cs b/FastGithub.UI/UdpLog.cs new file mode 100644 index 0000000..34d7e0d --- /dev/null +++ b/FastGithub.UI/UdpLog.cs @@ -0,0 +1,18 @@ +using System; +using System.Windows.Media; + +namespace FastGithub.UI +{ + public class UdpLog + { + public DateTime Timestamp { get; set; } + + public string Level { get; set; } + + public string Message { get; set; } + + public string SourceContext { get; set; } + + public string Color => this.Level == "Information" ? "#333" : "IndianRed"; + } +} diff --git a/FastGithub.UI/UdpLoggerPort.cs b/FastGithub.UI/UdpLoggerPort.cs new file mode 100644 index 0000000..4861ca2 --- /dev/null +++ b/FastGithub.UI/UdpLoggerPort.cs @@ -0,0 +1,49 @@ +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("当前无可用的端口"); + } + } +}