From 356c3c4291deb0f2a3317cdb9263b07b2552c094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Fri, 26 Apr 2024 01:23:54 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E4=BF=AE=E6=AD=A3Api=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=9C=A8=E6=9C=AC=E5=9C=B0=E6=8B=A5=E6=9C=89?= =?UTF-8?q?=E5=A4=9A=E4=B8=AAIP=E6=97=B6=EF=BC=8C=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=89=80=E6=9C=89IP=E5=B9=BF=E6=92=AD?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XCoder/CrazyCoder.csproj | 10 +-- XCoder/XNet/FrmApiDiscover.cs | 147 +++++++++++++++++++-------------- XCoderLinux/XCoderLinux.csproj | 6 +- 3 files changed, 93 insertions(+), 70 deletions(-) diff --git a/XCoder/CrazyCoder.csproj b/XCoder/CrazyCoder.csproj index 7f76626..31d3b32 100644 --- a/XCoder/CrazyCoder.csproj +++ b/XCoder/CrazyCoder.csproj @@ -68,12 +68,12 @@ - + - 10.9.2024.417-beta1447 + 10.10.2024.424-beta1020 2.0.2024.407 @@ -82,13 +82,13 @@ 4.3.2024.408-beta0604 - 5.6.2024.411-beta0755 + 5.6.2024.420-beta0005 - 2.9.2024.417-beta1339 + 2.9.2024.425-beta0447 - 11.11.2024.417-beta0711 + 11.11.2024.424-beta1026 2024.0.0 diff --git a/XCoder/XNet/FrmApiDiscover.cs b/XCoder/XNet/FrmApiDiscover.cs index 6887d6f..68acfa4 100644 --- a/XCoder/XNet/FrmApiDiscover.cs +++ b/XCoder/XNet/FrmApiDiscover.cs @@ -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; @@ -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)); @@ -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("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("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) diff --git a/XCoderLinux/XCoderLinux.csproj b/XCoderLinux/XCoderLinux.csproj index d7d6be7..acea640 100644 --- a/XCoderLinux/XCoderLinux.csproj +++ b/XCoderLinux/XCoderLinux.csproj @@ -49,9 +49,9 @@ - - - + + +