Skip to content

Commit

Permalink
v0.0.3 rc
Browse files Browse the repository at this point in the history
optimize node ip scan
  • Loading branch information
allenkey666 committed May 7, 2019
1 parent aefafb5 commit a930046
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 19 deletions.
49 changes: 36 additions & 13 deletions BXCNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net;
using System.Web.Script.Serialization;
using System.Windows.Media;
using System.Net.Sockets;

namespace bxc_bound
{
Expand All @@ -25,21 +26,24 @@ 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;
this.IsBoundOK = false;

try
{
MyWebClient wc = new MyWebClient();
this.Discovery = new JavaScriptSerializer().Deserialize<api_discovery>(wc.DownloadString("http://" + ip + ":9017/discovery"));
this.Status = new JavaScriptSerializer().Deserialize<api_status>(wc.DownloadString("http://" + ip + ":9017/status"));
this.Version = new JavaScriptSerializer().Deserialize<api_version>(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<api_discovery>(wc.DownloadString("http://" + ip + ":9017/discovery"));
this.Status = new JavaScriptSerializer().Deserialize<api_status>(wc.DownloadString("http://" + ip + ":9017/status"));
this.Version = new JavaScriptSerializer().Deserialize<api_version>(wc.DownloadString("http://" + ip + ":9017/version"));
if(this.Discovery.cksum == "BonusCloud")
{
this.IsOK = true;
}
}
}
catch
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

}
Expand Down
37 changes: 32 additions & 5 deletions Window1.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<TextBox
Height="20"
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Margin="0,38,18,0"
Width="35"
MaxLength="4"
IsTabStop="True"
TabIndex="25"
x:Name="txb_timeout"
Text="100"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
PreviewTextInput="txb_timeout_PreviewTextInput" />
<Label
HorizontalContentAlignment="Right"
Content="超时(毫秒):"
Grid.Column="0"
Grid.Row="0"
VerticalAlignment="Top"
Height="24"
Width="70"
HorizontalAlignment="Left"
Margin="105,35,0,8" />
</Grid>
</GroupBox>
<GroupBox
Expand All @@ -142,14 +168,15 @@
Margin="8,6,0,0"
Content="Email:" />
<TextBox
Height="21"
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Margin="80,8,8,0"
x:Name="txb_email"
TabIndex="30" />
TabIndex="30"
Height="20"
VerticalContentAlignment="Center" />
<Label
HorizontalContentAlignment="Right"
VerticalContentAlignment="Bottom"
Expand Down
26 changes: 25 additions & 1 deletion Window1.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace bxc_bound
{
Expand All @@ -32,6 +33,7 @@ public partial class Window1 : Window
{
List<BXCNode> bxcnodelist = new List<BXCNode>();
string localip;
TimeSpan scan_timeout;
string str_email;
string str_bcode;
string str_result;
Expand All @@ -44,6 +46,13 @@ public Window1()
lookupNetInterface();
}

static readonly Regex _regex = new Regex("[^0-9]+");

static bool IsTextAllowed(string text)
{
return !_regex.IsMatch(text);
}

void lookupNetInterface()
{
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
Expand Down Expand Up @@ -74,6 +83,17 @@ void btn_scanIP_Click(object sender, RoutedEventArgs e)
startup.Show();

localip = this.cbx_ip.Text;
int t;
try
{
t = Convert.ToInt32(this.txb_timeout.Text);
}
catch
{
this.txb_timeout.Text = "100";
t = 100;
}
scan_timeout = new TimeSpan(0,0,0,0,t);
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_queryBXCNode);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_queryBXCNode_RunWorkerCompleted);
Expand All @@ -82,7 +102,7 @@ void btn_scanIP_Click(object sender, RoutedEventArgs e)

void queryBXCNode(Object ip)
{
BXCNode bxcnode = new BXCNode((string)ip);
BXCNode bxcnode = new BXCNode((string)ip, scan_timeout);
if(bxcnode.IsOK)
{
bxcnodelist.Add(bxcnode);
Expand Down Expand Up @@ -262,6 +282,10 @@ void window1_Loaded(object sender, RoutedEventArgs e)
{

}
void txb_timeout_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text);
}

}

Expand Down

0 comments on commit a930046

Please sign in to comment.