|
@@ -3,43 +3,57 @@ using System.Collections.ObjectModel;
|
|
using System.Net;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Text;
|
|
|
|
+using System.Threading.Tasks;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Controls;
|
|
|
|
|
|
namespace FastGithub.UI
|
|
namespace FastGithub.UI
|
|
{
|
|
{
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// LogListBox.xaml 的交互逻辑
|
|
|
|
|
|
+ /// UdpLogListBox.xaml 的交互逻辑
|
|
/// </summary>
|
|
/// </summary>
|
|
- public partial class LogListBox : UserControl
|
|
|
|
|
|
+ public partial class UdpLogListBox : UserControl
|
|
{
|
|
{
|
|
private readonly byte[] buffer = new byte[ushort.MaxValue];
|
|
private readonly byte[] buffer = new byte[ushort.MaxValue];
|
|
private readonly Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
|
|
private readonly Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
|
|
|
|
|
|
public ObservableCollection<UdpLog> LogList { get; } = new ObservableCollection<UdpLog>();
|
|
public ObservableCollection<UdpLog> LogList { get; } = new ObservableCollection<UdpLog>();
|
|
|
|
|
|
- public LogListBox()
|
|
|
|
|
|
+ public UdpLogListBox()
|
|
{
|
|
{
|
|
InitializeComponent();
|
|
InitializeComponent();
|
|
DataContext = this;
|
|
DataContext = this;
|
|
|
|
|
|
|
|
+ this.InitUdpLoggerAsync();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private async void InitUdpLoggerAsync()
|
|
|
|
+ {
|
|
this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value));
|
|
this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value));
|
|
- this.BeginReceiveFrom();
|
|
|
|
|
|
+ while (true)
|
|
|
|
+ {
|
|
|
|
+ var log = await this.GetUdpLogAsync();
|
|
|
|
+ this.LogList.Add(log);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- private void BeginReceiveFrom()
|
|
|
|
|
|
+ private async Task<UdpLog> GetUdpLogAsync()
|
|
{
|
|
{
|
|
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
|
|
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
|
|
- this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, this.EndReceiveFrom, null);
|
|
|
|
|
|
+ var taskCompletionSource = new TaskCompletionSource<int>();
|
|
|
|
+ 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 Newtonsoft.Json.JsonConvert.DeserializeObject<UdpLog>(json);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
private void EndReceiveFrom(IAsyncResult ar)
|
|
private void EndReceiveFrom(IAsyncResult ar)
|
|
{
|
|
{
|
|
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
|
|
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
|
|
var length = this.socket.EndReceiveFrom(ar, ref remoteEP);
|
|
var length = this.socket.EndReceiveFrom(ar, ref remoteEP);
|
|
- var json = Encoding.UTF8.GetString(buffer, 0, length);
|
|
|
|
- var log = Newtonsoft.Json.JsonConvert.DeserializeObject<UdpLog>(json);
|
|
|
|
- this.Dispatcher.Invoke(() => this.LogList.Add(log));
|
|
|
|
- this.BeginReceiveFrom();
|
|
|
|
|
|
+ var taskCompletionSource = (TaskCompletionSource<int>)ar.AsyncState;
|
|
|
|
+ taskCompletionSource.TrySetResult(length);
|
|
}
|
|
}
|
|
|
|
|
|
private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e)
|
|
private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e)
|