melonvpn-original-cs/MelonVPNCore/DaemonMessaging.cs

330 lines
10 KiB
C#
Raw Normal View History

2021-03-23 12:57:51 +00:00
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
2021-03-23 18:07:47 +00:00
using System.Collections.Generic;
2021-03-23 12:57:51 +00:00
namespace MelonVPNCore
{
public class DaemonClient : IDisposable
{
2021-03-23 18:07:47 +00:00
public ClientResponseState SendDataMessage(DataMessage msg) => SendCustomMessage(Messages.GetMessage(msg));
2021-03-23 12:57:51 +00:00
private Socket _sock;
public static int timeout = 5000;
private Thread _recvThread;
private bool _stayConnected = false;
public event EventHandler<String> messageReceived;
2021-03-23 18:07:47 +00:00
public event EventHandler<Exception> connectionError;
2021-03-23 12:57:51 +00:00
2021-03-23 18:07:47 +00:00
public DaemonClient() { }
public DaemonClient(Socket sockIn)
{
if (sockIn is null) throw new ArgumentNullException(nameof(sockIn));
}
public void start()
2021-03-23 12:57:51 +00:00
{
try
{
2021-03-23 18:07:47 +00:00
if (_sock is null)
{
IPHostEntry host = Dns.GetHostEntry("localhost");
IPAddress ipAddress = host.AddressList[0];
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, DaemonServer.port);
_sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_stayConnected = true;
_sock.Connect(remoteEndPoint);
}
else
{
_stayConnected = true;
}
2021-03-23 12:57:51 +00:00
_sock.ReceiveTimeout = timeout;
_sock.SendTimeout = timeout;
_recvThread = new Thread(recvThreader)
{
IsBackground = true
};
_recvThread.Start();
2021-03-23 18:07:47 +00:00
}
catch (Exception e)
2021-03-23 12:57:51 +00:00
{
_stayConnected = false;
Console.WriteLine(e);
_sock = null;
2021-03-23 18:07:47 +00:00
if (!(_recvThread is null))
2021-03-23 12:57:51 +00:00
{
if (_recvThread.IsAlive) _recvThread.Abort();
_recvThread = null;
}
throw new InvalidOperationException("Daemon Client Init Failed.");
2021-03-23 18:07:47 +00:00
}
2021-03-23 12:57:51 +00:00
}
public ClientResponseState SendCustomMessage(string msg)
{
try
{
if (msg == Messages.EOF || msg == "") return ClientResponseState.Blank;
byte[] bytes = Encoding.ASCII.GetBytes(msg);
_sock.Send(bytes);
return ClientResponseState.Sent;
}
catch (Exception e)
{
Console.WriteLine(e);
2021-03-23 18:07:47 +00:00
connectionError?.Invoke(this, e);
2021-03-23 12:57:51 +00:00
return ClientResponseState.Error;
}
}
public void recvThreader()
{
while (_stayConnected)
{
try
{
string data = null;
byte[] bytes = null;
while (_stayConnected)
{
bytes = new byte[1024];
int bytesRec = _sock.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf(Messages.EOF, StringComparison.CurrentCulture) > -1) break;
}
2021-03-23 18:07:47 +00:00
messageReceived?.Invoke(this, data);
} catch (SocketException e)
{
Console.WriteLine(e);
connectionError?.Invoke(this, e);
2021-03-23 12:57:51 +00:00
} catch (Exception e)
{
Console.WriteLine(e);
}
}
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_stayConnected = false;
2021-03-23 18:07:47 +00:00
try
{
_sock.Shutdown(SocketShutdown.Both);
} catch (SocketException e)
{
Console.WriteLine(e);
}
2021-03-23 12:57:51 +00:00
if (!(_recvThread is null))
{
if (_recvThread.IsAlive) _recvThread.Abort();
_recvThread = null;
}
_sock.Close();
}
_sock = null;
_recvThread = null;
disposedValue = true;
}
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
}
#endregion
}
public class DaemonServer : IDisposable
{
public static int port = 22037;
private Socket _sock;
private Thread _lThread;
private bool _stayListening = false;
public event EventHandler<DaemonClient> clientConnected;
2021-03-23 18:07:47 +00:00
public void start()
2021-03-23 12:57:51 +00:00
{
try
{
IPHostEntry host = Dns.GetHostEntry("localhost");
IPAddress ipAddress = host.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
_sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_stayListening = true;
_sock.Bind(localEndPoint);
_sock.Listen(64);
_lThread = new Thread(lThreader)
{
IsBackground = true
};
_lThread.Start();
2021-03-23 18:07:47 +00:00
}
catch (Exception e)
2021-03-23 12:57:51 +00:00
{
_stayListening = false;
Console.WriteLine(e);
_sock = null;
if (!(_lThread is null))
{
if (_lThread.IsAlive) _lThread.Abort();
_lThread = null;
}
throw new InvalidOperationException("Daemon Server Init Failed.");
}
}
public void lThreader()
{
while (_stayListening)
{
try
{
2021-03-23 18:07:47 +00:00
DaemonClient dc = new DaemonClient(_sock.Accept());
clientConnected?.Invoke(this,dc);
2021-03-23 12:57:51 +00:00
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_stayListening = false;
if (!(_lThread is null))
{
if (_lThread.IsAlive) _lThread.Abort();
_lThread = null;
}
_sock.Close();
}
_sock = null;
_lThread = null;
disposedValue = true;
}
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
}
#endregion
}
2021-03-23 18:07:47 +00:00
public class DaemonClientMultiplexor : IDisposable
{
public ClientResponseState SendDataMessage(DataMessage msg) => SendCustomMessage(Messages.GetMessage(msg));
private List<DaemonClient> plexed = new List<DaemonClient>();
private object slockplexed = new object();
private object slockcon = new object();
private object slockrecv = new object();
public event EventHandler<String> clientMessageReceived;
public event EventHandler<DaemonClient> clientConnectComplete;
public bool startClients = true;
public bool disconnectErroredClients = true;
public DaemonClientMultiplexor(DaemonServer dsIn)
{
if (dsIn is null) throw new ArgumentNullException(nameof(dsIn));
dsIn.clientConnected += DsIn_ClientConnected;
}
void DsIn_ClientConnected(object sender, DaemonClient e)
{
lock (slockplexed) plexed.Add(e);
e.messageReceived += E_MessageReceived;
e.connectionError += E_ConnectionError;
if (startClients) e.start();
lock(slockcon) clientConnectComplete?.Invoke(this, e);
}
void E_MessageReceived(object sender, string e)
{
lock(slockrecv) clientMessageReceived?.Invoke(this, e);
}
void E_ConnectionError(object sender, Exception e)
{
if(disconnectErroredClients) ((DaemonClient)sender).Dispose();
}
public ClientResponseState SendCustomMessage(string msg)
{
var toret = ClientResponseState.Sent;
lock (slockplexed) {
foreach (DaemonClient c in plexed)
{
try
{
if (c.SendCustomMessage(msg) != ClientResponseState.Sent) toret = ClientResponseState.Error;
} catch (Exception e)
{
Console.WriteLine(e);
}
}
}
return toret;
}
public void disconnectAllClients()
{
lock (slockplexed) foreach (DaemonClient c in plexed) c.Dispose();
lock (slockplexed) plexed.Clear();
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
lock(slockplexed) foreach (DaemonClient c in plexed) c.Dispose();
lock (slockplexed) plexed.Clear();
}
plexed = null;
slockcon = null;
slockrecv = null;
slockplexed = null;
disposedValue = true;
}
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
}
#endregion
}
2021-03-23 12:57:51 +00:00
}