XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

添加了局域网探测器

72c720d
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

4 个文件 +139 -7
Modified XFEExtension.NetCore.Analyzer.Test/TestClass.cs +10 -7
@@ -1,15 +1,18 @@
1 using XFEExtension.NetCore.ProfileExtension;
1 using XFEExtension.NetCore.WebExtension.LANDeviceDetector;
2 2
3 3 namespace XFEExtension.NetCore.Analyzer.Test;
4 4
5 5 internal class TestClass
6 6 {
7 static void Main(string[] args)
7 static async Task Main(string[] args)
8 8 {
9 Console.WriteLine(SystemProfile.Name);
10 Console.WriteLine(SystemProfile.Age);
11 XFEProfile.ImportProfiles("|{+-[+-SystemProfile-+][+-|{++-[++-name-++][++-\"New Name\"-++]-++}||{++-[++-_age-++][++-33-++]-++}|-+]-+}|");
12 Console.WriteLine(SystemProfile.Name);
13 Console.WriteLine(SystemProfile.Age);
9 var lANDeviceDetector = new LANDeviceDetector();
10 lANDeviceDetector.DeviceFind += LANDeviceDetector_DeviceFind;
11 await lANDeviceDetector.StartDetecting();
12 }
13
14 private static void LANDeviceDetector_DeviceFind(LANDevice sender)
15 {
16 Console.WriteLine($"IP:{sender.IPAddress}\tHost:{sender.DeviceName}");
14 17 }
15 18 }
Added XFEExtension.NetCore/WebExtension/LANDeviceDetector/LANDevice.cs +22 -0
@@ -0,0 +1,22 @@
1 using System.Net;
2
3 namespace XFEExtension.NetCore.WebExtension.LANDeviceDetector;
4
5 /// <summary>
6 /// 局域网设备
7 /// </summary>
8 public abstract class LANDevice(IPAddress iPAddress, IPHostEntry? hostEntry, string? deviceName)
9 {
10 /// <summary>
11 /// 设备名称
12 /// </summary>
13 public string? DeviceName { get; set; } = deviceName;
14 /// <summary>
15 /// Host入口
16 /// </summary>
17 public IPHostEntry? HostEntry { get; set; } = hostEntry;
18 /// <summary>
19 /// IP地址
20 /// </summary>
21 public IPAddress IPAddress { get; set; } = iPAddress;
22 }
Added XFEExtension.NetCore/WebExtension/LANDeviceDetector/LANDeviceDetector.cs +102 -0
@@ -0,0 +1,102 @@
1 using System.Net;
2 using System.Net.NetworkInformation;
3 using XFEExtension.NetCore.DelegateExtension;
4
5 namespace XFEExtension.NetCore.WebExtension.LANDeviceDetector;
6
7 /// <summary>
8 /// 局域网设备探测器
9 /// </summary>
10 /// <param name="iPStart">网络地址搜索的起始位置</param>
11 /// <param name="timeOut">超时</param>
12 public class LANDeviceDetector(string iPStart = "192.168.1.*", int timeOut = 100)
13 {
14 /// <summary>
15 /// 找到设备
16 /// </summary>
17 public event XFEEventHandler<LANDevice>? DeviceFind;
18
19 /// <summary>
20 /// 网络地址搜索的起始位置
21 /// </summary>
22 public string IPStart { get; set; } = iPStart;
23
24 /// <summary>
25 /// 每个设备检测超时
26 /// </summary>
27 public int TimeOut { get; set; } = timeOut;
28
29 /// <summary>
30 /// 是否正在检测
31 /// </summary>
32 public bool IsDetecting { get; set; }
33
34 /// <summary>
35 /// 已找到的设备
36 /// </summary>
37 public List<LANDevice> FindDevices { get; set; } = [];
38
39 /// <summary>
40 /// 开始探测设备
41 /// </summary>
42 /// <returns></returns>
43 public async Task StartDetecting()
44 {
45 IsDetecting = true;
46 while (IsDetecting)
47 {
48 FindDevices.AddRange(await InnerDetect(reply =>
49 {
50 IPHostEntry? hostEntry = null;
51 try { hostEntry = Dns.GetHostEntry(reply.Address); } catch { }
52 var device = new LANDeviceImpl(reply.Address, hostEntry, hostEntry?.HostName);
53 if (FindDevices.Any(d => d.IPAddress.ToString() == reply.Address.ToString()))
54 return;
55 else
56 FindDevices.Add(device);
57 DeviceFind?.Invoke(device);
58 }));
59 }
60 }
61
62 /// <summary>
63 /// 检测一次
64 /// </summary>
65 /// <returns></returns>
66 public async Task<List<LANDevice>> Detect() => await InnerDetect(reply =>
67 {
68 IPHostEntry? hostEntry = null;
69 try { hostEntry = Dns.GetHostEntry(reply.Address); } catch { }
70 DeviceFind?.Invoke(new LANDeviceImpl(reply.Address, hostEntry, hostEntry?.HostName));
71 });
72
73 /// <summary>
74 /// 停止探测设备
75 /// </summary>
76 public void Stop() => IsDetecting = false;
77
78 private async Task<List<LANDevice>> InnerDetect(Action<PingReply> action)
79 {
80 var tasks = new List<Task>();
81 var findDevices = new List<LANDevice>();
82 for (int i = 1; i < 256; i++)
83 {
84 var currentIndex = i;
85 tasks.Add(Task.Run(async () =>
86 {
87 var ipAddress = IPStart.Replace("*", currentIndex.ToString());
88 using var ping = new Ping();
89 var reply = await ping.SendPingAsync(ipAddress, TimeOut);
90 if (reply.Status == IPStatus.Success)
91 {
92 IPHostEntry? hostEntry = null;
93 try { hostEntry = Dns.GetHostEntry(ipAddress); } catch { }
94 findDevices.Add(new LANDeviceImpl(reply.Address, hostEntry, hostEntry?.HostName));
95 action.Invoke(reply);
96 }
97 }));
98 }
99 await Task.WhenAll(tasks);
100 return findDevices;
101 }
102 }
Added XFEExtension.NetCore/WebExtension/LANDeviceDetector/LANDeviceImpl.cs +5 -0
@@ -0,0 +1,5 @@
1 using System.Net;
2
3 namespace XFEExtension.NetCore.WebExtension.LANDeviceDetector;
4
5 internal class LANDeviceImpl(IPAddress iPAddress, IPHostEntry? hostEntry, string? deviceName) : LANDevice(iPAddress, hostEntry, deviceName) { }