netmonitor/NetMonitorServer/Service1.cs

66 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
2010-04-26 12:46:03 +02:00
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace NetMonitorServer
{
public partial class Service1 : ServiceBase
{
2010-04-26 12:46:03 +02:00
private Thread ot;
public Service1()
{
InitializeComponent();
2010-04-26 12:46:03 +02:00
this.ot = new Thread(new ThreadStart(overwatch));
this.ot.IsBackground = true;
}
private static void overwatch()
{
UdpClient net_gets = new UdpClient(34523);
IPEndPoint net_gets_port = new IPEndPoint(IPAddress.Loopback, 0);
2010-04-26 12:46:03 +02:00
while (true)
{
if (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested)
{
net_gets.Close();
2010-04-26 12:46:03 +02:00
break;
}
byte[] data = net_gets.Receive(ref net_gets_port);
2010-04-26 12:46:03 +02:00
string text = Encoding.UTF8.GetString(data);
switchCommand(text);
}
}
private static void switchCommand(string data)
{
Console.WriteLine(data);
if (data.Substring(0, 3).ToLower() == "net")
new SetNetworks(data);
2011-01-27 00:35:09 +01:00
if (data.Substring(0, 7).ToLower() == "service")
new SetService(data);
}
protected override void OnStart(string[] args)
{
2010-04-26 12:46:03 +02:00
this.ot.Start();
}
protected override void OnStop()
{
2010-04-26 12:46:03 +02:00
this.ot.Abort();
}
public void StartServiceConsole(string[] args)
{
this.OnStart(args);
}
}
}