netmonitor/NetOpenVPNGUI/Runner.cs

120 lines
3.2 KiB
C#
Raw Normal View History

2011-01-27 00:35:09 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
2011-01-27 11:19:41 +01:00
namespace NetOpenVpnGui
2011-01-27 00:35:09 +01:00
{
class Runner
{
private UdpClient net_send;
private IPEndPoint net_send_port;
private UdpClient net_gets;
private IPEndPoint net_gets_port;
2011-01-28 21:30:32 +01:00
private string messageReceivedString;
private bool messageReceived;
private bool net_init = false;
2011-01-27 11:19:41 +01:00
public Runner(String p)
2011-01-27 00:35:09 +01:00
{
2011-01-27 11:19:41 +01:00
this.service = p;
2011-01-27 00:35:09 +01:00
}
2011-01-27 11:19:41 +01:00
public bool run_service()
2011-01-27 00:35:09 +01:00
{
2011-01-28 21:30:32 +01:00
return this.send("service start " + this.service);
2011-01-27 00:35:09 +01:00
}
2011-01-27 11:19:41 +01:00
public bool stop_service()
2011-01-27 00:35:09 +01:00
{
2011-01-28 21:30:32 +01:00
return this.send("service stop " + this.service);
2011-01-27 00:35:09 +01:00
}
2011-01-27 11:19:41 +01:00
public bool check_service()
2011-01-27 00:35:09 +01:00
{
2011-01-28 21:30:32 +01:00
return this.send("service status " + this.service);
}
private bool send(string p)
{
this.init();
byte[] data = Encoding.UTF8.GetBytes(p);
2011-01-27 00:35:09 +01:00
this.net_send.Send(data, data.Length, this.net_send_port);
2011-01-28 21:30:32 +01:00
//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();
}
2011-01-27 00:35:09 +01:00
}
2011-01-28 21:30:32 +01:00
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;
}
}
2011-01-27 11:19:41 +01:00
public string service { get; set; }
2011-01-27 00:35:09 +01:00
}
2011-01-28 21:30:32 +01:00
public class UdpState
{
public IPEndPoint e;
public UdpClient u;
}
2011-01-27 00:35:09 +01:00
}
2011-01-28 21:30:32 +01:00