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 @@ - - - + + +