netmonitor/NetMonitorServer/MainService.cs

85 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using BlubbFish.Utils;
namespace NetMonitorServer
{
public partial class MainService : ServiceBase
{
private Thread serverThread = new Thread(new ThreadStart(overwatch));
private static UdpClient receiveStream;
public MainService()
{
InitializeComponent();
this.serverThread.IsBackground = true;
}
private static void overwatch()
{
IPEndPoint networkStreamAddress = new IPEndPoint(IPAddress.Loopback, 0);
while (true)
{
if (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested)
{
receiveStream.Close();
break;
}
byte[] data = receiveStream.Receive(ref networkStreamAddress);
Thread queryThread = new Thread(new ParameterizedThreadStart(ServerCommandProcessor));
queryThread.Start(data);
}
}
private static void ServerCommandProcessor(object threadParam)
{
string data = Encoding.UTF8.GetString((byte[])threadParam);
Console.WriteLine(data);
Queue<string> arguments = new Queue<string>(data.Split(' '));
if (arguments.Count < 2)
{
Console.WriteLine("Zu wenig Argumente");
return;
}
ActionClass a = Factory.getAction(arguments.Dequeue());
if (a == null)
{
Console.WriteLine("Falsche Angabe der ActionClass");
return;
}
a.SetAnswerStream(new UdpClient(), new IPEndPoint(IPAddress.Loopback, Int32.Parse(arguments.Dequeue())));
a.Run(arguments);
}
protected override void OnStart(string[] args)
{
this.initServerThread();
this.serverThread.Start();
}
private void initServerThread()
{
receiveStream = new UdpClient(Int32.Parse(Factory.getSettings().getValue("ports", "server")));
}
protected override void OnStop()
{
this.serverThread.Abort();
try { receiveStream.Close(); } catch { };
}
public void StartServiceConsole(string[] args)
{
this.OnStart(args);
}
}
}