From 0fc356df1902c73a1ddefe2d3a65976453a6744b Mon Sep 17 00:00:00 2001 From: klavirshik Date: Tue, 16 Jul 2024 19:08:25 +0200 Subject: [PATCH] pinger --- Reseter2/Pinger.cs | 41 +++++++++++++++++++++++++++++++- Reseter2/ReseteTask.cs | 15 ++++++++++-- Reseter2/StausPreReboot.cs | 3 ++- Reseter2/TaskControl.Designer.cs | 19 +++++++-------- Reseter2/TaskControl.cs | 5 ++-- 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/Reseter2/Pinger.cs b/Reseter2/Pinger.cs index 119df18..0b564c3 100644 --- a/Reseter2/Pinger.cs +++ b/Reseter2/Pinger.cs @@ -4,9 +4,48 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Net.NetworkInformation; + namespace Reseter2 { - internal class Pinger + internal class Pinger { + private string NameOrAddress; + private int TimeoutCount; + + public Pinger(string nameOrAddress) + { + this.NameOrAddress = nameOrAddress; + } + public int Timeout() { + return TimeoutCount; + } + public long PingHost() + { + bool pingable = false; + long ping = 0; + Ping pinger = null; + try + { + pinger = new Ping(); + PingReply reply = pinger.Send(this.NameOrAddress); + pingable = reply.Status == IPStatus.TimedOut; + ping = reply.RoundtripTime; + } + catch (PingException) + { + // Discard PingExceptions and return false; + } + finally + { + if (pinger != null) + { + pinger.Dispose(); + } + } + if (pingable) TimeoutCount++; + return ping; + } + } } diff --git a/Reseter2/ReseteTask.cs b/Reseter2/ReseteTask.cs index 4de5555..8cdce94 100644 --- a/Reseter2/ReseteTask.cs +++ b/Reseter2/ReseteTask.cs @@ -11,12 +11,14 @@ namespace Reseter2 private IComp Comp; private AStatusTask StatusTask; private TaskControl taskControl; + private Pinger Pingers; public ReseterTask(IComp comp, TaskControl taskCntrl) { Comp = comp; taskControl = taskCntrl; StatusTask = new StatusPreReboot(this); + Pingers = new Pinger(Comp.GetName()); } public string GetName() { @@ -28,9 +30,18 @@ namespace Reseter2 StatusTask.Tick(); } - public void DataContrl(string srt) + public long Ping() { - taskControl.DataContrl(srt); + return Pingers.PingHost(); + } + + public int Timeout() { + return Pingers.Timeout(); + } + + public void DataContrl(string ping, string timeout) + { + taskControl.DataContrl(ping, timeout); } private void Clear() { diff --git a/Reseter2/StausPreReboot.cs b/Reseter2/StausPreReboot.cs index a8a12ef..5d067b3 100644 --- a/Reseter2/StausPreReboot.cs +++ b/Reseter2/StausPreReboot.cs @@ -16,7 +16,8 @@ namespace Reseter2 public override void Tick() { time++; - resetertask.DataContrl(time.ToString()); + + resetertask.DataContrl(resetertask.Ping().ToString(), resetertask.Timeout().ToString()); } public override void Next() { diff --git a/Reseter2/TaskControl.Designer.cs b/Reseter2/TaskControl.Designer.cs index 8425d2c..7483c62 100644 --- a/Reseter2/TaskControl.Designer.cs +++ b/Reseter2/TaskControl.Designer.cs @@ -36,7 +36,7 @@ this.label4 = new System.Windows.Forms.Label(); this.lb_ping = new System.Windows.Forms.Label(); this.Timeout = new System.Windows.Forms.Label(); - this.label5 = new System.Windows.Forms.Label(); + this.lb_timeout = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); @@ -116,14 +116,13 @@ this.Timeout.TabIndex = 7; this.Timeout.Text = "Timeout"; // - // label5 + // lb_timeout // - this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(225, 49); - this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(25, 13); - this.label5.TabIndex = 8; - this.label5.Text = "123"; + this.lb_timeout.AutoSize = true; + this.lb_timeout.Location = new System.Drawing.Point(225, 49); + this.lb_timeout.Name = "lb_timeout"; + this.lb_timeout.Size = new System.Drawing.Size(0, 13); + this.lb_timeout.TabIndex = 8; // // button1 // @@ -160,7 +159,7 @@ this.Controls.Add(this.pictureBox1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); - this.Controls.Add(this.label5); + this.Controls.Add(this.lb_timeout); this.Controls.Add(this.Timeout); this.Controls.Add(this.lb_ping); this.Controls.Add(this.label4); @@ -187,7 +186,7 @@ private System.Windows.Forms.Label label4; private System.Windows.Forms.Label lb_ping; private System.Windows.Forms.Label Timeout; - private System.Windows.Forms.Label label5; + private System.Windows.Forms.Label lb_timeout; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.PictureBox pictureBox1; diff --git a/Reseter2/TaskControl.cs b/Reseter2/TaskControl.cs index bc774ee..6cc0417 100644 --- a/Reseter2/TaskControl.cs +++ b/Reseter2/TaskControl.cs @@ -26,9 +26,10 @@ namespace Reseter2 lb_name.Text = reseterTask.GetName(); } - public void DataContrl(string ping) + public void DataContrl(string ping, string timeout) { - label5.Text = ping; + lb_ping.Text = ping; + lb_timeout.Text = timeout; } private void label1_Click(object sender, EventArgs e)