From a930046fb0050d85829b36db2590579561431d54 Mon Sep 17 00:00:00 2001 From: bettermanbao <17328315+bettermanbao@users.noreply.github.com> Date: Tue, 7 May 2019 13:26:21 +0800 Subject: [PATCH] v0.0.3 rc optimize node ip scan --- BXCNode.cs | 49 ++++++++++++++++++++++++++++++++++++------------- Window1.xaml | 37 ++++++++++++++++++++++++++++++++----- Window1.xaml.cs | 26 +++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 19 deletions(-) diff --git a/BXCNode.cs b/BXCNode.cs index f30380a..0c06f09 100644 --- a/BXCNode.cs +++ b/BXCNode.cs @@ -10,6 +10,7 @@ using System.Net; using System.Web.Script.Serialization; using System.Windows.Media; +using System.Net.Sockets; namespace bxc_bound { @@ -25,7 +26,7 @@ public class BXCNode public bool IsSelected; public bool IsBoundOK; - public BXCNode(string ip) + public BXCNode(string ip, TimeSpan timeout) { this.IsOK = false; this.IsSelected = false; @@ -33,13 +34,16 @@ public BXCNode(string ip) try { - MyWebClient wc = new MyWebClient(); - this.Discovery = new JavaScriptSerializer().Deserialize(wc.DownloadString("http://" + ip + ":9017/discovery")); - this.Status = new JavaScriptSerializer().Deserialize(wc.DownloadString("http://" + ip + ":9017/status")); - this.Version = new JavaScriptSerializer().Deserialize(wc.DownloadString("http://" + ip + ":9017/version")); - if(this.Discovery.cksum == "BonusCloud") + if(BXCNode.IsPortOpen(ip, 9017, timeout)) { - this.IsOK = true; + WebClient wc = new WebClient(); + this.Discovery = new JavaScriptSerializer().Deserialize(wc.DownloadString("http://" + ip + ":9017/discovery")); + this.Status = new JavaScriptSerializer().Deserialize(wc.DownloadString("http://" + ip + ":9017/status")); + this.Version = new JavaScriptSerializer().Deserialize(wc.DownloadString("http://" + ip + ":9017/version")); + if(this.Discovery.cksum == "BonusCloud") + { + this.IsOK = true; + } } } catch @@ -68,7 +72,12 @@ public SolidColorBrush ToBrush() public override string ToString() { - return this.Discovery.ip + System.Environment.NewLine + this.Discovery.mac + " " + this.Version.version ; + string process = ""; + if(this.Status.status.bound && !this.Status.status.process) + { + process = " 节点离线"; + } + return this.Discovery.ip + process + System.Environment.NewLine + this.Discovery.mac + " " + this.Version.version ; } public class api_discovery @@ -94,14 +103,28 @@ public class api_version public string version { get; set; } } - private class MyWebClient : WebClient + static bool IsPortOpen(string host, int port, TimeSpan timeout) { - protected override WebRequest GetWebRequest(Uri uri) + try + { + using(var client = new TcpClient()) + { + var result = client.BeginConnect(host, port, null, null); + var success = result.AsyncWaitHandle.WaitOne(timeout); + if (!success) + { + return false; + } + + client.EndConnect(result); + } + + } + catch { - WebRequest w = base.GetWebRequest(uri); - w.Timeout = 100; - return w; + return false; } + return true; } } diff --git a/Window1.xaml b/Window1.xaml index d226cf4..b039da7 100644 --- a/Window1.xaml +++ b/Window1.xaml @@ -111,11 +111,37 @@ Content="扫描节点" Grid.Column="0" Grid.Row="0" - HorizontalAlignment="Center" VerticalAlignment="Top" - Margin="0,38,0,0" Click="btn_scanIP_Click" - TabIndex="20" /> + TabIndex="20" + HorizontalAlignment="Left" + Margin="18,38,0,0" /> + +