2014-07-11 16:38:10 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
namespace NetMonitorServer {
|
|
|
|
|
public partial class MainService : ServiceBase {
|
|
|
|
|
private Thread serverThread = new Thread(new ThreadStart(overwatch));
|
|
|
|
|
private static UdpClient receiveStream;
|
|
|
|
|
private static FileLogger log;
|
|
|
|
|
private static OwnObject.LogLevel loglevel;
|
|
|
|
|
public MainService() {
|
|
|
|
|
string folder = Factory.getSettings().getValue("program", "logfolder");
|
|
|
|
|
folder = folder.EndsWith("\\") ? folder : folder + "\\";
|
|
|
|
|
log = FileLogger.getInstance(folder + "server.log", false);
|
|
|
|
|
loglevel = (OwnObject.LogLevel)Enum.Parse(typeof(OwnObject.LogLevel), Factory.getSettings().getValue("program", "loglevel"));
|
|
|
|
|
LogEvents("MainService", "Server Starten...", OwnObject.LogLevel.Notice);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
this.serverThread.IsBackground = true;
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
private static void overwatch() {
|
|
|
|
|
LogEvents("MainService.overwatch", "Empfangsthread gestartet.", OwnObject.LogLevel.Notice);
|
|
|
|
|
IPEndPoint networkStreamAddress = new IPEndPoint(IPAddress.Loopback, 0);
|
|
|
|
|
while(true) {
|
|
|
|
|
if(Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested) {
|
|
|
|
|
receiveStream.Close();
|
|
|
|
|
break;
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
byte[] data = receiveStream.Receive(ref networkStreamAddress);
|
|
|
|
|
Thread queryThread = new Thread(new ParameterizedThreadStart(ServerCommandProcessor));
|
|
|
|
|
queryThread.Start(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
private static void ServerCommandProcessor(object threadParam) {
|
|
|
|
|
string data = Encoding.UTF8.GetString((byte[])threadParam);
|
|
|
|
|
LogEvents("MainService.ServerCommandProcessor", "Befehl empfangen: "+data, OwnObject.LogLevel.Notice);
|
|
|
|
|
Queue<string> arguments = new Queue<string>(data.Split(' '));
|
|
|
|
|
if(arguments.Count < 2) {
|
|
|
|
|
LogEvents("MainService.ServerCommandProcessor", "Zu wenig Argumente", OwnObject.LogLevel.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ActionClass a = Factory.getAction(arguments.Dequeue());
|
|
|
|
|
a.EventLog += LogEvents;
|
|
|
|
|
if(a == null) {
|
|
|
|
|
LogEvents("MainService.ServerCommandProcessor", "Falsche Angabe der ActionClass", OwnObject.LogLevel.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
a.SetAnswerStream(new UdpClient(), new IPEndPoint(IPAddress.Loopback, Int32.Parse(arguments.Dequeue())));
|
|
|
|
|
a.Run(arguments);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
static void LogEvents(string location, string message, OwnObject.LogLevel level) {
|
|
|
|
|
LogEvents(location, message, level, DateTime.Now);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
static void LogEvents(string location, string message, OwnObject.LogLevel level, DateTime date) {
|
|
|
|
|
string msg = OwnObject.logToString(location, message, level, date, true, true);
|
|
|
|
|
//if(level >= loglevel) {
|
|
|
|
|
log.setLine(msg);
|
|
|
|
|
//}
|
|
|
|
|
Console.WriteLine(msg);
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
protected override void OnStart(string[] args) {
|
|
|
|
|
LogEvents("MainService.OnStart", "Server gestartet", OwnObject.LogLevel.Notice);
|
|
|
|
|
this.initServerThread();
|
|
|
|
|
this.serverThread.Start();
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
private void initServerThread() {
|
|
|
|
|
int port = Int32.Parse(Factory.getSettings().getValue("ports", "server"));
|
|
|
|
|
try {
|
|
|
|
|
receiveStream = new UdpClient(port);
|
|
|
|
|
} catch(Exception e) {
|
|
|
|
|
LogEvents("MainService.initServerThread", "Fehler beim Initialisieren des Ports "+port+": "+e.Message, OwnObject.LogLevel.Error);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnStop() {
|
|
|
|
|
LogEvents("MainService.OnStop", "Server gestoppt", OwnObject.LogLevel.Notice);
|
|
|
|
|
this.serverThread.Abort();
|
|
|
|
|
try { receiveStream.Close(); } catch { };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StartServiceConsole(string[] args) {
|
|
|
|
|
loglevel = OwnObject.LogLevel.Debug;
|
|
|
|
|
LogEvents("MainService.StartServiceConsole", "Server über die Konsole gestartet", OwnObject.LogLevel.Notice);
|
|
|
|
|
this.OnStart(args);
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|