2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Exceptions;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
using System;
|
2018-05-03 01:25:49 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
2018-04-07 15:49:00 -04:00
|
|
|
|
using System.Linq;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore.RCon
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
|
|
|
|
class ConnectionState
|
|
|
|
|
{
|
2018-04-04 15:38:34 -04:00
|
|
|
|
public Socket Client { get; private set; }
|
|
|
|
|
public int BufferSize { get; private set; }
|
|
|
|
|
public byte[] Buffer { get; private set; }
|
|
|
|
|
|
|
|
|
|
private readonly StringBuilder sb;
|
|
|
|
|
|
|
|
|
|
public StringBuilder ResponseString
|
|
|
|
|
{
|
|
|
|
|
get => sb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConnectionState(Socket cl)
|
|
|
|
|
{
|
|
|
|
|
BufferSize = 8192;
|
|
|
|
|
Buffer = new byte[BufferSize];
|
|
|
|
|
Client = cl;
|
|
|
|
|
sb = new StringBuilder();
|
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Connection
|
|
|
|
|
{
|
2018-04-15 21:27:43 -04:00
|
|
|
|
public IPEndPoint Endpoint { get; private set; }
|
|
|
|
|
public string RConPassword { get; private set; }
|
2018-04-02 01:25:06 -04:00
|
|
|
|
ILogger Log;
|
2018-04-02 23:11:19 -04:00
|
|
|
|
int FailedSends;
|
|
|
|
|
int FailedReceives;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
DateTime LastQuery;
|
2018-04-04 15:38:34 -04:00
|
|
|
|
string response;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
|
|
|
|
ManualResetEvent OnConnected;
|
|
|
|
|
ManualResetEvent OnSent;
|
|
|
|
|
ManualResetEvent OnReceived;
|
|
|
|
|
|
|
|
|
|
public Connection(string ipAddress, int port, string password, ILogger log)
|
|
|
|
|
{
|
|
|
|
|
Endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
|
|
|
|
|
RConPassword = password;
|
|
|
|
|
Log = log;
|
|
|
|
|
|
|
|
|
|
OnConnected = new ManualResetEvent(false);
|
|
|
|
|
OnSent = new ManualResetEvent(false);
|
|
|
|
|
OnReceived = new ManualResetEvent(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnConnectedCallback(IAsyncResult ar)
|
|
|
|
|
{
|
|
|
|
|
var serverSocket = (Socket)ar.AsyncState;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
serverSocket.EndConnect(ar);
|
|
|
|
|
#if DEBUG
|
|
|
|
|
Log.WriteDebug($"Successfully initialized socket to {serverSocket.RemoteEndPoint}");
|
|
|
|
|
#endif
|
|
|
|
|
OnConnected.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (SocketException e)
|
|
|
|
|
{
|
2018-04-02 23:11:19 -04:00
|
|
|
|
throw new NetworkException($"Could not initialize socket for RCon - {e.Message}");
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSentCallback(IAsyncResult ar)
|
|
|
|
|
{
|
|
|
|
|
Socket serverConnection = (Socket)ar.AsyncState;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int sentByteNum = serverConnection.EndSend(ar);
|
|
|
|
|
#if DEBUG
|
2018-05-03 01:25:49 -04:00
|
|
|
|
Log.WriteDebug($"Sent {sentByteNum} bytes to {serverConnection.RemoteEndPoint}");
|
2018-04-02 01:25:06 -04:00
|
|
|
|
#endif
|
2018-05-03 01:25:49 -04:00
|
|
|
|
// this is where we override our await to make it
|
2018-04-02 01:25:06 -04:00
|
|
|
|
OnSent.Set();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 13:55:10 -04:00
|
|
|
|
catch (Exception)
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnReceivedCallback(IAsyncResult ar)
|
|
|
|
|
{
|
|
|
|
|
var connectionState = (ConnectionState)ar.AsyncState;
|
|
|
|
|
var serverConnection = connectionState.Client;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int bytesRead = serverConnection.EndReceive(ar);
|
|
|
|
|
|
|
|
|
|
if (bytesRead > 0)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
2018-05-03 01:25:49 -04:00
|
|
|
|
Log.WriteDebug($"Received {bytesRead} bytes from {serverConnection.RemoteEndPoint}");
|
2018-04-02 01:25:06 -04:00
|
|
|
|
#endif
|
2018-04-25 02:38:59 -04:00
|
|
|
|
FailedReceives = 0;
|
2018-04-21 18:18:20 -04:00
|
|
|
|
connectionState.ResponseString.Append(Utilities.EncodingType.GetString(connectionState.Buffer, 0, bytesRead).TrimEnd('\0') + '\n');
|
2018-04-09 15:17:10 -04:00
|
|
|
|
|
|
|
|
|
if (!connectionState.Buffer.Take(4).ToArray().SequenceEqual(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }))
|
|
|
|
|
throw new NetworkException("Unexpected packet received");
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-05-10 01:34:29 -04:00
|
|
|
|
/* if (FailedReceives == 0 && serverConnection.Available > 0)
|
|
|
|
|
{
|
|
|
|
|
serverConnection.BeginReceive(connectionState.Buffer, 0, connectionState.Buffer.Length, 0,
|
|
|
|
|
new AsyncCallback(OnReceivedCallback), connectionState);
|
|
|
|
|
}
|
|
|
|
|
else*/
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2018-04-04 15:38:34 -04:00
|
|
|
|
response = connectionState.ResponseString.ToString();
|
2018-04-02 01:25:06 -04:00
|
|
|
|
OnReceived.Set();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-04-04 15:38:34 -04:00
|
|
|
|
response = connectionState.ResponseString.ToString();
|
2018-04-02 01:25:06 -04:00
|
|
|
|
OnReceived.Set();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 15:38:34 -04:00
|
|
|
|
catch (SocketException)
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
2018-05-03 01:25:49 -04:00
|
|
|
|
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
2018-05-10 01:34:29 -04:00
|
|
|
|
// Log.WriteWarning($"Tried to check for more available bytes for disposed socket on {Endpoint}");
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 18:24:21 -04:00
|
|
|
|
public async Task<string[]> SendQueryAsync(StaticHelpers.QueryType type, string parameters = "", bool waitForResponse = true)
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2018-04-02 23:11:19 -04:00
|
|
|
|
// will this really prevent flooding?
|
2018-05-14 13:55:10 -04:00
|
|
|
|
if ((DateTime.Now - LastQuery).TotalMilliseconds < 350)
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2018-05-14 13:55:10 -04:00
|
|
|
|
await Task.Delay(350);
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 15:38:34 -04:00
|
|
|
|
LastQuery = DateTime.Now;
|
|
|
|
|
|
2018-04-02 01:25:06 -04:00
|
|
|
|
OnSent.Reset();
|
|
|
|
|
OnReceived.Reset();
|
2018-04-23 01:43:48 -04:00
|
|
|
|
byte[] payload = null;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case StaticHelpers.QueryType.DVAR:
|
|
|
|
|
case StaticHelpers.QueryType.COMMAND:
|
2018-04-23 01:43:48 -04:00
|
|
|
|
var header = "ÿÿÿÿrcon ".Select(Convert.ToByte).ToList();
|
|
|
|
|
byte[] p = Utilities.EncodingType.GetBytes($"{RConPassword} {parameters}");
|
|
|
|
|
header.AddRange(p);
|
|
|
|
|
payload = header.ToArray();
|
2018-04-02 01:25:06 -04:00
|
|
|
|
break;
|
|
|
|
|
case StaticHelpers.QueryType.GET_STATUS:
|
2018-04-23 01:43:48 -04:00
|
|
|
|
payload = "ÿÿÿÿgetstatus".Select(Convert.ToByte).ToArray();
|
2018-04-02 01:25:06 -04:00
|
|
|
|
break;
|
2018-04-24 18:01:27 -04:00
|
|
|
|
case StaticHelpers.QueryType.GET_INFO:
|
|
|
|
|
payload = "ÿÿÿÿgetinfo".Select(Convert.ToByte).ToArray();
|
|
|
|
|
break;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
using (var socketConnection = new Socket(Endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp))
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2018-05-03 01:25:49 -04:00
|
|
|
|
socketConnection.BeginConnect(Endpoint, new AsyncCallback(OnConnectedCallback), socketConnection);
|
2018-04-02 23:11:19 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
retrySend:
|
|
|
|
|
try
|
2018-04-02 23:11:19 -04:00
|
|
|
|
{
|
2018-05-10 01:34:29 -04:00
|
|
|
|
#if DEBUG
|
|
|
|
|
Console.WriteLine($"Sending Command {parameters}");
|
|
|
|
|
#endif
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (!OnConnected.WaitOne(StaticHelpers.SocketTimeout))
|
|
|
|
|
throw new SocketException((int)SocketError.TimedOut);
|
|
|
|
|
|
|
|
|
|
socketConnection.BeginSend(payload, 0, payload.Length, 0, new AsyncCallback(OnSentCallback), socketConnection);
|
|
|
|
|
bool success = await Task.FromResult(OnSent.WaitOne(StaticHelpers.SocketTimeout));
|
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
FailedSends++;
|
2018-04-02 23:11:19 -04:00
|
|
|
|
#if DEBUG
|
2018-05-03 01:25:49 -04:00
|
|
|
|
Log.WriteDebug($"{FailedSends} failed sends to {socketConnection.RemoteEndPoint.ToString()}");
|
2018-04-02 23:11:19 -04:00
|
|
|
|
#endif
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (FailedSends < 4)
|
|
|
|
|
goto retrySend;
|
|
|
|
|
else if (FailedSends == 4)
|
|
|
|
|
Log.WriteError($"Failed to send data to {socketConnection.RemoteEndPoint}");
|
|
|
|
|
}
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
else
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (FailedSends >= 4)
|
|
|
|
|
{
|
|
|
|
|
Log.WriteVerbose($"Resumed send RCon connection with {socketConnection.RemoteEndPoint}");
|
|
|
|
|
FailedSends = 0;
|
|
|
|
|
}
|
2018-04-04 15:38:34 -04:00
|
|
|
|
}
|
2018-04-02 23:11:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
catch (SocketException e)
|
|
|
|
|
{
|
|
|
|
|
// this result is normal if the server is not listening
|
|
|
|
|
if (e.NativeErrorCode != (int)SocketError.ConnectionReset &&
|
|
|
|
|
e.NativeErrorCode != (int)SocketError.TimedOut)
|
|
|
|
|
throw new NetworkException($"Unexpected error while sending data to server - {e.Message}");
|
|
|
|
|
}
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (!waitForResponse)
|
|
|
|
|
return await Task.FromResult(new string[] { "" });
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
var connectionState = new ConnectionState(socketConnection);
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
retryReceive:
|
|
|
|
|
try
|
2018-04-02 23:11:19 -04:00
|
|
|
|
{
|
2018-05-03 01:25:49 -04:00
|
|
|
|
socketConnection.BeginReceive(connectionState.Buffer, 0, connectionState.Buffer.Length, 0,
|
|
|
|
|
new AsyncCallback(OnReceivedCallback), connectionState);
|
|
|
|
|
bool success = await Task.FromResult(OnReceived.WaitOne(StaticHelpers.SocketTimeout));
|
2018-04-10 20:25:44 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
FailedReceives++;
|
2018-04-02 23:11:19 -04:00
|
|
|
|
#if DEBUG
|
2018-05-03 01:25:49 -04:00
|
|
|
|
Log.WriteDebug($"{FailedReceives} failed receives from {socketConnection.RemoteEndPoint.ToString()}");
|
2018-04-02 23:11:19 -04:00
|
|
|
|
#endif
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (FailedReceives < 4)
|
|
|
|
|
goto retrySend;
|
|
|
|
|
else if (FailedReceives == 4)
|
|
|
|
|
{
|
2018-05-21 17:09:27 -04:00
|
|
|
|
// Log.WriteError($"Failed to receive data from {socketConnection.RemoteEndPoint} after {FailedReceives} tries");
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FailedReceives >= 4)
|
|
|
|
|
{
|
2018-05-16 00:57:37 -04:00
|
|
|
|
throw new NetworkException($"{Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_COMMUNICATION"]} {socketConnection.RemoteEndPoint.ToString()}");
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}
|
2018-04-04 15:38:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
else
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (FailedReceives >= 4)
|
|
|
|
|
{
|
2018-05-16 00:57:37 -04:00
|
|
|
|
Log.WriteVerbose($"Resumed receive RCon connection from {socketConnection.RemoteEndPoint.ToString()}");
|
2018-05-03 01:25:49 -04:00
|
|
|
|
FailedReceives = 0;
|
|
|
|
|
}
|
2018-04-04 15:38:34 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
catch (SocketException e)
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2018-05-03 01:25:49 -04:00
|
|
|
|
// this result is normal if the server is not listening
|
|
|
|
|
if (e.NativeErrorCode != (int)SocketError.ConnectionReset &&
|
|
|
|
|
e.NativeErrorCode != (int)SocketError.TimedOut)
|
|
|
|
|
throw new NetworkException($"Unexpected error while receiving data from server - {e.Message}");
|
|
|
|
|
else if (FailedReceives < 4)
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2018-05-03 01:25:49 -04:00
|
|
|
|
goto retryReceive;
|
2018-04-04 15:38:34 -04:00
|
|
|
|
}
|
2018-04-02 23:11:19 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
else if (FailedReceives == 4)
|
|
|
|
|
{
|
2018-05-21 17:09:27 -04:00
|
|
|
|
// Log.WriteError($"Failed to receive data from {socketConnection.RemoteEndPoint} after {FailedReceives} tries");
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (FailedReceives >= 4)
|
|
|
|
|
{
|
|
|
|
|
throw new NetworkException(e.Message);
|
|
|
|
|
}
|
2018-04-04 15:38:34 -04:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
string queryResponse = response;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (queryResponse.Contains("Invalid password"))
|
|
|
|
|
throw new NetworkException("RCON password is invalid");
|
|
|
|
|
if (queryResponse.ToString().Contains("rcon_password"))
|
|
|
|
|
throw new NetworkException("RCON password has not been set");
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
string[] splitResponse = queryResponse.Split(new char[]
|
|
|
|
|
{
|
2018-04-09 15:17:10 -04:00
|
|
|
|
'\n'
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Select(line => line.Trim()).ToArray();
|
|
|
|
|
return splitResponse;
|
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|