12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using IDeviceDiscovery;
- using Shaker.Model;
- using Shaker.Models;
- using Shaker.Tools;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace DeviceDiscovery
- {
- internal class DeviceDiscovery : IDeviceDiscovery.IDeviceDiscovery
- {
- [AllowNull]
- private CancellationTokenSource deviceDiscoveryTokenSource;
- public int DiscoveryPort { get; private set; }
- [AllowNull]
- public string MulticastGroup { get; private set; }
- [AllowNull]
- public Func<SearchDeviceModel, ResultDeviceModel> OnDeviceFound { get; set; }
- public bool IsStart { get; private set; }
- public void Init(int discoveryPort = 16656, string multicastGroup = "239.1.0.3")
- {
- DiscoveryPort = discoveryPort;
- MulticastGroup = multicastGroup;
- }
- private void Log(string message)
- {
- EventBus.EventBroker.Instance.GetEvent(Shaker.Model.Topic.LOG).Publish(this, null, message);
- }
- public void Start()
- {
- Task.Run(async () =>
- {
- System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient(Topic.DISCOVERYPORT);
- udpClient.JoinMulticastGroup(IPAddress.Parse(Topic.MulticastGroup));
- Log("网络发现服务启动");
- deviceDiscoveryTokenSource = new CancellationTokenSource();
- IsStart = true;
- while (!deviceDiscoveryTokenSource.IsCancellationRequested)
- {
- var result = await udpClient.ReceiveAsync(deviceDiscoveryTokenSource.Token);
- if (OnDeviceFound == null) continue;
- try
- {
- var msg = result.Buffer.GetValue<SearchDeviceModel>();
- if (msg != null)
- {
- Log($"{result.RemoteEndPoint.Address}开始扫描");
- var networks = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
- var aa = networks.Where(x => x.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up).Select(x => x.GetIPProperties().UnicastAddresses).SelectMany(x => x).Where(x => x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToList();
- int index = aa.FindIndex(x =>
- {
- return (BitConverter.ToUInt32(x.Address.GetAddressBytes(), 0) & BitConverter.ToUInt32(x.IPv4Mask.GetAddressBytes(), 0)) == (BitConverter.ToUInt32(result.RemoteEndPoint.Address.GetAddressBytes()) & BitConverter.ToUInt32(x.IPv4Mask.GetAddressBytes()));
- });
- ResultDeviceModel resultDevice = OnDeviceFound(msg);
- resultDevice ??= new ResultDeviceModel();
- if (index == -1)
- {
- resultDevice.DeviceInfoModel.IP = "127.0.0.1";
- }
- else
- {
- resultDevice.DeviceInfoModel.IP = aa[index].Address.ToString();
- }
- Log($"返回地址{resultDevice.DeviceInfoModel.IP}:{resultDevice.DeviceInfoModel.Port}");
- udpClient.Send(resultDevice.GetBytes(), new System.Net.IPEndPoint(IPAddress.Broadcast, Topic.DISCOVERYPORT - 1));
- }
- }
- catch
- { }
- }
- udpClient.Close();
- });
- }
- public void Stop()
- {
- deviceDiscoveryTokenSource?.Cancel();
- IsStart = false;
- }
- }
- }
|