69 lines
2.6 KiB
C#
69 lines
2.6 KiB
C#
|
using System;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Text;
|
|||
|
|
|||
|
namespace MelonVPNCore
|
|||
|
{
|
|||
|
public static class GUISocketServer
|
|||
|
{
|
|||
|
public static event EventHandler<ClientResponseState> Receive;
|
|||
|
public static event EventHandler<ConnectedClient[]> ClientListUpdate;
|
|||
|
|
|||
|
public static void StartServer()
|
|||
|
{
|
|||
|
IPHostEntry host = Dns.GetHostEntry("localhost");
|
|||
|
IPAddress ipAddress = host.AddressList[0];
|
|||
|
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 22036);
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
listener.Bind(localEndPoint);
|
|||
|
listener.Listen(10);
|
|||
|
|
|||
|
while (true)
|
|||
|
{
|
|||
|
Socket handler = listener.Accept();
|
|||
|
|
|||
|
string data = "";
|
|||
|
byte[] bytes = null;
|
|||
|
|
|||
|
while (true)
|
|||
|
{
|
|||
|
bytes = new byte[1024];
|
|||
|
int bytesRec = handler.Receive(bytes);
|
|||
|
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
|
|||
|
if (data.IndexOf(Messages.EOF, StringComparison.CurrentCulture) > -1) break;
|
|||
|
}
|
|||
|
|
|||
|
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;
|
|||
|
Console.WriteLine(data);
|
|||
|
if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture))
|
|||
|
{
|
|||
|
string jsonWithEof = data.Substring(Messages.ClientListStartMsg.Length);
|
|||
|
string jsonData = data.Substring(0, jsonWithEof.Length - Messages.EOF.Length);
|
|||
|
Console.WriteLine("clients: " + jsonData);
|
|||
|
//ClientListParser.Parse(jsonData);
|
|||
|
ConnectedClient[] clients = new ConnectedClient[0];
|
|||
|
ClientListUpdate?.Invoke(null, clients);
|
|||
|
}
|
|||
|
|
|||
|
handler.Shutdown(SocketShutdown.Both);
|
|||
|
handler.Close();
|
|||
|
|
|||
|
Receive?.Invoke(null, ret);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
Console.WriteLine(e);
|
|||
|
}
|
|||
|
Console.WriteLine("GUI socket server reathed the end");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|