From f019b4e493d8b8d00782f4ed2c5d5b677642aed8 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Fri, 28 Jan 2011 20:30:32 +0000 Subject: [PATCH] Fixing Usage of Network port --- NetOpenVPNGUI/Form1.cs | 2 +- NetOpenVPNGUI/Runner.cs | 105 +++++++++++++++++++++++++++++++--------- 2 files changed, 84 insertions(+), 23 deletions(-) diff --git a/NetOpenVPNGUI/Form1.cs b/NetOpenVPNGUI/Form1.cs index 052a4f9..89b29aa 100644 --- a/NetOpenVPNGUI/Form1.cs +++ b/NetOpenVPNGUI/Form1.cs @@ -111,7 +111,7 @@ namespace NetOpenVpnGui { this.Statuslog.Items.Add(text); this.trayIcon.BalloonTipIcon = toolTipIcon; - this.trayIcon.BalloonTipText = text; + this.trayIcon.BalloonTipText = sr.service + " " + text; this.trayIcon.BalloonTipTitle = title; } } diff --git a/NetOpenVPNGUI/Runner.cs b/NetOpenVPNGUI/Runner.cs index 716c4d1..75cddd9 100644 --- a/NetOpenVPNGUI/Runner.cs +++ b/NetOpenVPNGUI/Runner.cs @@ -14,45 +14,106 @@ namespace NetOpenVpnGui private IPEndPoint net_send_port; private UdpClient net_gets; private IPEndPoint net_gets_port; + private string messageReceivedString; + private bool messageReceived; + private bool net_init = false; public Runner(String p) { this.service = p; - this.net_send = new UdpClient(); - this.net_send_port = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34523); - this.net_gets = new UdpClient(34524); - this.net_gets_port = new IPEndPoint(IPAddress.Loopback, 0); } public bool run_service() { - byte[] data = Encoding.UTF8.GetBytes("service start " + this.service); - this.net_send.Send(data, data.Length, this.net_send_port); - bool ret = Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - if (ret) - return true; - return false; + return this.send("service start " + this.service); } public bool stop_service() { - byte[] data = Encoding.UTF8.GetBytes("service stop " + this.service); - this.net_send.Send(data, data.Length, this.net_send_port); - bool ret = Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - if (ret) - return true; - return false; + return this.send("service stop " + this.service); } public bool check_service() { - byte[] data = Encoding.UTF8.GetBytes("service status " + this.service); - this.net_send.Send(data, data.Length, this.net_send_port); - bool ret = Boolean.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port))); - if (ret) - return true; - return false; + return this.send("service status " + this.service); } + private bool send(string p) + { + this.init(); + byte[] data = Encoding.UTF8.GetBytes(p); + this.net_send.Send(data, data.Length, this.net_send_port); + //net_gets.Receive(ref net_gets_port); + + UdpState s = new UdpState(); + s.e = net_gets_port; + s.u = net_gets; + this.messageReceived = false; + net_gets.BeginReceive(new AsyncCallback(ReceiveCallback), s); + int i=0; + while (!this.messageReceived && i++<100) + { + System.Threading.Thread.Sleep(100); + } + if (!this.messageReceived) + { + this.close(); + return false; + } + this.close(); + return Boolean.Parse(this.messageReceivedString); + + } + + private void close() + { + if (this.net_init) + { + this.net_init = false; + this.net_gets.Close(); + this.net_send.Close(); + } + } + + private void init() + { + if (!this.net_init) + { + this.net_init = true; + this.net_send = new UdpClient(); + this.net_gets = new UdpClient(34524); + this.net_send_port = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34523); + this.net_gets_port = new IPEndPoint(IPAddress.Loopback, 0); + } + } + + + + private void ReceiveCallback(IAsyncResult ar) + { + UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u; + IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e; + try + { + Byte[] receiveBytes = u.EndReceive(ar, ref e); + this.messageReceivedString = Encoding.UTF8.GetString(receiveBytes); + this.messageReceived = true; + } + catch (Exception) + { + this.messageReceivedString = ""; + this.messageReceived = false; + } + + } + + public string service { get; set; } } + + public class UdpState + { + public IPEndPoint e; + public UdpClient u; + } } +