melonvpn-original-cs/MelonVPNCore/GUISocketServer.cs

107 lines
3.9 KiB
C#
Raw Normal View History

2020-09-22 23:10:16 +01:00
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
2021-03-23 18:07:47 +00:00
using System.Threading;
2020-09-22 23:10:16 +01:00
namespace MelonVPNCore
{
public static class GUISocketServer
{
2021-03-23 18:07:47 +00:00
public static ClientResponseState SendDataMessage(DataMessage msg) => SendCustomMessage(Messages.GetMessage(msg));
public static int reconnectTimeout = 2000;
public static DaemonClient client;
2020-09-22 23:10:16 +01:00
public static event EventHandler<ClientResponseState> Receive;
public static event EventHandler<ConnectedClient[]> ClientListUpdate;
2021-03-23 18:07:47 +00:00
private static bool waitfcl = false;
private static bool exec = true;
2020-09-22 23:10:16 +01:00
public static void StartServer()
{
2021-03-23 18:07:47 +00:00
while (exec)
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
try
2020-09-22 23:10:16 +01:00
{
2021-03-23 18:07:47 +00:00
client = new DaemonClient();
client.connectionError += Client_ConnectionError;
client.messageReceived += Client_MessageReceived;
waitfcl = true;
client.start();
while (waitfcl) Thread.Sleep(reconnectTimeout);
client.connectionError -= Client_ConnectionError;
client.messageReceived -= Client_MessageReceived;
client.Dispose();
}
catch (InvalidOperationException e)
{
Console.WriteLine(e);
}
Thread.Sleep(reconnectTimeout);
}
Console.WriteLine("GUISocketServer has reached the end.");
}
2020-09-22 23:10:16 +01:00
2021-03-23 18:07:47 +00:00
public static void oneConnect()
{
try
{
client = new DaemonClient();
client.messageReceived += Client_MessageReceived;
waitfcl = true;
client.start();
}
catch (InvalidOperationException e)
{
Console.WriteLine(e);
}
}
2020-09-22 23:10:16 +01:00
2021-03-23 18:07:47 +00:00
public static void oneDispose()
{
client.messageReceived -= Client_MessageReceived;
client.Dispose();
}
2020-09-22 23:10:16 +01:00
2021-03-23 18:07:47 +00:00
static void Client_ConnectionError(object sender, Exception e)
{
waitfcl = false;
}
2020-09-22 23:10:16 +01:00
2021-03-23 18:07:47 +00:00
static void Client_MessageReceived(object sender, string data)
{
try
{
ClientResponseState ret = ClientResponseState.None;
if (data == Messages.OnlineMsg) ret = ClientResponseState.Online;
if (data == Messages.OfflineMsg) ret = ClientResponseState.Offline;
if (data == Messages.ErrorMsg) ret = ClientResponseState.ServerError;
if (data == Messages.StartingMsg) ret = ClientResponseState.Starting;
if (data == Messages.RestartingMsg) ret = ClientResponseState.Restarting;
if (data == Messages.RestartOnMsg) ret = ClientResponseState.RestartOn;
if (data == Messages.RestartOffMsg) ret = ClientResponseState.RestartOff;
Console.WriteLine(data);
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture))
{
string jsonWithEof = data.Substring(Messages.ClientListStartMsg.Length);
string jsonData = jsonWithEof.Substring(0, jsonWithEof.Length - Messages.EOF.Length);
Console.WriteLine("clients: " + jsonData);
ConnectedClient[] clients = ClientListParser.Parse(jsonData);
ClientListUpdate?.Invoke(null, clients);
2020-09-22 23:10:16 +01:00
}
2021-03-23 18:07:47 +00:00
Receive?.Invoke(null, ret);
2020-09-22 23:10:16 +01:00
}
catch (Exception e)
{
Console.WriteLine(e);
}
2021-03-23 18:07:47 +00:00
}
public static ClientResponseState SendCustomMessage(string msg)
{
var toret = client?.SendCustomMessage(msg);
return (toret.HasValue) ? toret.Value : ClientResponseState.Error;
2020-09-22 23:10:16 +01:00
}
}
}