forked from shiftwinting/FastGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdpLogger.cs
77 lines (66 loc) · 2.55 KB
/
UdpLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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);
/// <summary>
/// 获取日志端口
/// </summary>
public static int Port { get; } = GetAvailableUdpPort(38457);
static UdpLogger()
{
socket.Bind(new IPEndPoint(IPAddress.Loopback, Port));
}
/// <summary>
/// 获取可用的随机Udp端口
/// </summary>
/// <param name="minValue"></param>
/// <param name="addressFamily"></param>
/// <returns></returns>
private static int GetAvailableUdpPort(int minValue, AddressFamily addressFamily = AddressFamily.InterNetwork)
{
var hashSet = new HashSet<int>();
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<UdpLog?> GetUdpLogAsync()
{
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var taskCompletionSource = new TaskCompletionSource<int>();
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<UdpLog>(json);
}
private static void EndReceiveFrom(IAsyncResult ar)
{
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var length = socket.EndReceiveFrom(ar, ref remoteEP);
var taskCompletionSource = (TaskCompletionSource<int>)ar.AsyncState;
taskCompletionSource.TrySetResult(length);
}
}
}