Skip to content

Commit

Permalink
[fix]修正Api搜索工具在本地拥有多个IP时,无法使用所有IP广播的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 25, 2024
1 parent dcff378 commit 356c3c4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 70 deletions.
10 changes: 5 additions & 5 deletions XCoder/CrazyCoder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
<ItemGroup>
<PackageReference Include="NewLife.Map" Version="2.5.2024.410-beta1241" />
<PackageReference Include="NewLife.ModbusRTU" Version="1.8.2024.217" />
<PackageReference Include="NewLife.Remoting" Version="2.8.2024.402" />
<PackageReference Include="NewLife.Remoting" Version="2.8.2024.419-beta0703" />
<PackageReference Include="System.Speech" Version="8.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualBasic" Version="10.3.0" />
<PackageReference Include="NewLife.Core">
<Version>10.9.2024.417-beta1447</Version>
<Version>10.10.2024.424-beta1020</Version>
</PackageReference>
<PackageReference Include="NewLife.MQTT">
<Version>2.0.2024.407</Version>
Expand All @@ -82,13 +82,13 @@
<Version>4.3.2024.408-beta0604</Version>
</PackageReference>
<PackageReference Include="NewLife.Redis">
<Version>5.6.2024.411-beta0755</Version>
<Version>5.6.2024.420-beta0005</Version>
</PackageReference>
<PackageReference Include="NewLife.Stardust">
<Version>2.9.2024.417-beta1339</Version>
<Version>2.9.2024.425-beta0447</Version>
</PackageReference>
<PackageReference Include="NewLife.XCode">
<Version>11.11.2024.417-beta0711</Version>
<Version>11.11.2024.424-beta1026</Version>
</PackageReference>
<PackageReference Include="SSH.NET">
<Version>2024.0.0</Version>
Expand Down
147 changes: 85 additions & 62 deletions XCoder/XNet/FrmApiDiscover.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using NewLife;
using NewLife.Log;
using NewLife.Messaging;
using NewLife.Net;
using NewLife.Remoting;
using NewLife.Security;
using NewLife.Serialization;
using Stardust.Models;
using XCoder;
using XCoder.Common;
Expand Down Expand Up @@ -54,7 +51,7 @@ private async void btnConnect_Click(Object sender, EventArgs e)
var task = Task.Run(() => DiscoverUdp(null, ep));
ts.Add(task);
}
foreach (var ip in NetHelper.GetIPs().Where(e => e.IsIPv4()))
foreach (var ip in NetHelper.GetIPs())
{
var ep = new IPEndPoint(IPAddress.Broadcast, port);
var task = Task.Run(() => DiscoverUdp(ip, ep));
Expand All @@ -70,68 +67,94 @@ private async void btnConnect_Click(Object sender, EventArgs e)
}
}

class MyClient : ApiClient
{
public IPAddress Local { get; set; }

public MyClient(String uri) : base(uri) { }

protected override ISocketClient OnCreate(String svr)
{
var client = base.OnCreate(svr);
if (Local != null)
client.Local = new NetUri { Address = Local };

return client;
}
}

async Task DiscoverUdp(IPAddress local, IPEndPoint ep)
{
XTrace.WriteLine("DiscoverUdp: {0} -> {1}", local, ep);

//var uri = new NetUri("udp://255.255.255.255:5500");
var client = new ApiClient($"udp://{ep.Address}:{ep.Port}");
client.Received += Client_Received;

// 异步发送,但是不等待返回,因为可能会有多个返回,在事件里处理
_ = client.InvokeAsync<Object>("Api/Info");

await Task.Delay(1_000);

//client.InvokeOneWay("Info");

//// 构建请求
//var enc = new JsonEncoder();
//var msg = enc.CreateRequest("Api/Info", null);
//var req = msg.ToPacket().ReadBytes();

//var udp = new UdpClient(ep.AddressFamily)
//{
// EnableBroadcast = true
//};

//if (local != null) udp.Client.Bind(new IPEndPoint(local, Rand.Next(1000, 60000)));

//// 发送
//udp.Send(req, req.Length, ep);

//// 多次接收
//while (true)
//{
// try
// {
// var source = new CancellationTokenSource(3_000);
// var rs = await udp.ReceiveAsync(source.Token);
// if (rs.Buffer != null)
// {
// msg = new DefaultMessage();
// msg.Read(rs.Buffer);

// if (enc.Decode(msg, out var action, out var code, out var data) && code == 0)
// {
// // 解码结果
// var result = enc.DecodeResult(action, data, msg);
// XTrace.WriteLine("Receive[{0}] {1}", udp.Client.LocalEndPoint, result.ToJson());

// if (enc.Convert(result, typeof(ApiItem)) is ApiItem ai)
// {
// ai.RemoteIP = rs.RemoteEndPoint.Address + "";

// Invoke(() => ShowItem(ai));
// }
// }
// }
// }
// catch (OperationCanceledException)
// {
// break;
// }
//}
try
{
//var uri = new NetUri("udp://255.255.255.255:5500");
var client = new MyClient($"udp://{ep.Address}:{ep.Port}");
if (local != null) client.Local = local;
client.Received += Client_Received;

// 异步发送,但是不等待返回,因为可能会有多个返回,在事件里处理
await client.InvokeAsync<Object>("Api/Info");

await Task.Delay(1_000);

//client.InvokeOneWay("Info");

//// 构建请求
//var enc = new JsonEncoder();
//var msg = enc.CreateRequest("Api/Info", null);
//var req = msg.ToPacket().ReadBytes();

//var udp = new UdpClient(ep.AddressFamily)
//{
// EnableBroadcast = true
//};

//if (local != null) udp.Client.Bind(new IPEndPoint(local, Rand.Next(1000, 60000)));

//// 发送
//udp.Send(req, req.Length, ep);

//// 多次接收
//while (true)
//{
// try
// {
// var source = new CancellationTokenSource(3_000);
// var rs = await udp.ReceiveAsync(source.Token);
// if (rs.Buffer != null)
// {
// msg = new DefaultMessage();
// msg.Read(rs.Buffer);

// if (enc.Decode(msg, out var action, out var code, out var data) && code == 0)
// {
// // 解码结果
// var result = enc.DecodeResult(action, data, msg);
// XTrace.WriteLine("Receive[{0}] {1}", udp.Client.LocalEndPoint, result.ToJson());

// if (enc.Convert(result, typeof(ApiItem)) is ApiItem ai)
// {
// ai.RemoteIP = rs.RemoteEndPoint.Address + "";

// Invoke(() => ShowItem(ai));
// }
// }
// }
// }
// catch (OperationCanceledException)
// {
// break;
// }
//}
}
catch (Exception ex)
{
XTrace.WriteException(ex);

MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void Client_Received(Object sender, ApiReceivedEventArgs e)
Expand Down
6 changes: 3 additions & 3 deletions XCoderLinux/XCoderLinux.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@

<ItemGroup>
<PackageReference Include="GtkSharp" Version="3.24.24.95" />
<PackageReference Include="NewLife.Core" Version="10.9.2024.417-beta1447" />
<PackageReference Include="NewLife.Remoting" Version="2.8.2024.402" />
<PackageReference Include="NewLife.XCode" Version="11.11.2024.417-beta0711" />
<PackageReference Include="NewLife.Core" Version="10.10.2024.424-beta1020" />
<PackageReference Include="NewLife.Remoting" Version="2.8.2024.419-beta0703" />
<PackageReference Include="NewLife.XCode" Version="11.11.2024.424-beta1026" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
<PackageReference Include="System.Management" Version="8.0.0" />
</ItemGroup>
Expand Down

0 comments on commit 356c3c4

Please sign in to comment.