2021-06-03 11:51:03 -04:00
|
|
|
|
using System;
|
2018-05-03 01:25:49 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-04-17 16:05:16 -04:00
|
|
|
|
using System.Collections.Generic;
|
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;
|
2019-02-06 21:12:35 -05:00
|
|
|
|
using System.Text;
|
2020-05-04 17:50:02 -04:00
|
|
|
|
using System.Text.RegularExpressions;
|
2020-12-12 22:43:27 -05:00
|
|
|
|
using System.Threading;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Serilog.Context;
|
2021-06-03 11:51:03 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Exceptions;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.RCon;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2021-06-03 11:51:03 -04:00
|
|
|
|
namespace Integrations.Cod
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// implementation of IRConConnection
|
|
|
|
|
/// </summary>
|
2021-06-03 11:51:03 -04:00
|
|
|
|
public class CodRConConnection : IRConConnection
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
private static readonly ConcurrentDictionary<EndPoint, ConnectionState> ActiveQueries = new();
|
2021-06-03 11:51:03 -04:00
|
|
|
|
public IPEndPoint Endpoint { get; }
|
|
|
|
|
public string RConPassword { get; }
|
2018-12-03 20:21:13 -05:00
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
private IRConParser _parser;
|
|
|
|
|
private IRConParserConfiguration _config;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
private readonly ILogger _log;
|
|
|
|
|
private readonly Encoding _gameEncoding;
|
2021-09-18 19:25:02 -04:00
|
|
|
|
private readonly int _retryAttempts;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2022-04-25 16:39:30 -04:00
|
|
|
|
public CodRConConnection(IPEndPoint ipEndpoint, string password, ILogger<CodRConConnection> log,
|
|
|
|
|
Encoding gameEncoding, int retryAttempts)
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
|
|
|
|
RConPassword = password;
|
2021-07-11 18:26:30 -04:00
|
|
|
|
_gameEncoding = gameEncoding;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_log = log;
|
2021-07-11 18:26:30 -04:00
|
|
|
|
Endpoint = ipEndpoint;
|
2021-09-18 19:25:02 -04:00
|
|
|
|
_retryAttempts = retryAttempts;
|
2019-02-01 20:49:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 19:53:23 -05:00
|
|
|
|
public void SetConfiguration(IRConParser parser)
|
2019-02-01 20:49:25 -05:00
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
_parser = parser;
|
2022-03-03 09:54:17 -05:00
|
|
|
|
_config = parser.Configuration;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
public async Task<string[]> SendQueryAsync(StaticHelpers.QueryType type, string parameters = "",
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await SendQueryAsyncInternal(type, parameters, token);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
using (LogContext.PushProperty("Server", Endpoint.ToString()))
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning(ex, "Could not complete RCon request");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
using (LogContext.PushProperty("Server", Endpoint.ToString()))
|
|
|
|
|
{
|
|
|
|
|
_log.LogDebug("Releasing OnComplete {Count}", ActiveQueries[Endpoint].OnComplete.CurrentCount);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
if (ActiveQueries[Endpoint].OnComplete.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
ActiveQueries[Endpoint].OnComplete.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-28 21:44:30 -05:00
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
|
|
|
|
private async Task<string[]> SendQueryAsyncInternal(StaticHelpers.QueryType type, string parameters = "",
|
|
|
|
|
CancellationToken token = default)
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
if (!ActiveQueries.ContainsKey(Endpoint))
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
ActiveQueries.TryAdd(Endpoint, new ConnectionState());
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-25 16:39:30 -04:00
|
|
|
|
if (!ActiveQueries.TryGetValue(Endpoint, out var connectionState))
|
|
|
|
|
{
|
|
|
|
|
_log.LogError("Could not retrieve connection state");
|
|
|
|
|
throw new InvalidOperationException("Could not get connection state");
|
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
_log.LogDebug("Waiting for semaphore to be released [{Endpoint}]", Endpoint);
|
2020-11-07 11:40:58 -05:00
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
// enter the semaphore so only one query is sent at a time per server.
|
2022-03-02 19:21:08 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await connectionState.OnComplete.WaitAsync(token);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("OnComplete did not complete before timeout {Count}",
|
|
|
|
|
connectionState.OnComplete.CurrentCount);
|
2022-03-02 19:21:08 -05:00
|
|
|
|
throw new RConException("Timed out waiting for access to rcon socket");
|
|
|
|
|
}
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
|
|
|
|
var timeSinceLastQuery = (DateTime.Now - connectionState.LastQuery).TotalMilliseconds;
|
2018-09-29 22:49:12 -04:00
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
if (timeSinceLastQuery < _config.FloodProtectInterval)
|
2018-09-29 22:49:12 -04:00
|
|
|
|
{
|
2022-03-02 19:21:08 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
var delay = _config.FloodProtectInterval - (int)timeSinceLastQuery;
|
|
|
|
|
_log.LogDebug("Delaying for {Delay}ms", delay);
|
|
|
|
|
await Task.Delay(delay, token);
|
2022-03-02 19:21:08 -05:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("Waiting for flood protect did not complete before timeout timeout {Count}",
|
|
|
|
|
connectionState.OnComplete.CurrentCount);
|
2022-03-02 19:21:08 -05:00
|
|
|
|
throw new RConException("Timed out waiting for flood protect to expire");
|
|
|
|
|
}
|
2018-09-29 22:49:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
_log.LogDebug("Semaphore has been released [{Endpoint}]", Endpoint);
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("Query {@QueryInfo}", new { endpoint = Endpoint.ToString(), type, parameters });
|
2018-04-09 15:17:10 -04:00
|
|
|
|
|
2018-04-23 01:43:48 -04:00
|
|
|
|
byte[] payload = null;
|
2022-03-03 09:54:17 -05:00
|
|
|
|
var waitForResponse = _config.WaitForResponse;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
string ConvertEncoding(string text)
|
2019-09-09 18:37:57 -04:00
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var convertedBytes = Utilities.EncodingType.GetBytes(text);
|
2020-02-11 17:44:06 -05:00
|
|
|
|
return _gameEncoding.GetString(convertedBytes);
|
2019-02-06 21:12:35 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 18:24:44 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var convertedRConPassword = ConvertEncoding(RConPassword);
|
|
|
|
|
var convertedParameters = ConvertEncoding(parameters);
|
2019-08-30 18:24:44 -04:00
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case StaticHelpers.QueryType.GET_DVAR:
|
|
|
|
|
waitForResponse |= true;
|
2022-03-02 19:21:08 -05:00
|
|
|
|
payload = string
|
2022-03-03 09:54:17 -05:00
|
|
|
|
.Format(_config.CommandPrefixes.RConGetDvar, convertedRConPassword,
|
2022-03-02 19:21:08 -05:00
|
|
|
|
convertedParameters + '\0').Select(Convert.ToByte).ToArray();
|
2019-08-30 18:24:44 -04:00
|
|
|
|
break;
|
|
|
|
|
case StaticHelpers.QueryType.SET_DVAR:
|
2022-03-02 19:21:08 -05:00
|
|
|
|
payload = string
|
2022-03-03 09:54:17 -05:00
|
|
|
|
.Format(_config.CommandPrefixes.RConSetDvar, convertedRConPassword,
|
2022-03-02 19:21:08 -05:00
|
|
|
|
convertedParameters + '\0').Select(Convert.ToByte).ToArray();
|
2019-08-30 18:24:44 -04:00
|
|
|
|
break;
|
|
|
|
|
case StaticHelpers.QueryType.COMMAND:
|
2022-03-02 19:21:08 -05:00
|
|
|
|
payload = string
|
2022-03-03 09:54:17 -05:00
|
|
|
|
.Format(_config.CommandPrefixes.RConCommand, convertedRConPassword,
|
2022-03-02 19:21:08 -05:00
|
|
|
|
convertedParameters + '\0').Select(Convert.ToByte).ToArray();
|
2019-08-30 18:24:44 -04:00
|
|
|
|
break;
|
|
|
|
|
case StaticHelpers.QueryType.GET_STATUS:
|
|
|
|
|
waitForResponse |= true;
|
2022-03-03 09:54:17 -05:00
|
|
|
|
payload = (_config.CommandPrefixes.RConGetStatus + '\0').Select(Convert.ToByte).ToArray();
|
2019-08-30 18:24:44 -04:00
|
|
|
|
break;
|
|
|
|
|
case StaticHelpers.QueryType.GET_INFO:
|
|
|
|
|
waitForResponse |= true;
|
2022-03-03 09:54:17 -05:00
|
|
|
|
payload = (_config.CommandPrefixes.RConGetInfo + '\0').Select(Convert.ToByte).ToArray();
|
2019-08-30 18:24:44 -04:00
|
|
|
|
break;
|
|
|
|
|
case StaticHelpers.QueryType.COMMAND_STATUS:
|
|
|
|
|
waitForResponse |= true;
|
2022-03-03 09:54:17 -05:00
|
|
|
|
payload = string.Format(_config.CommandPrefixes.RConCommand, convertedRConPassword, "status\0")
|
2022-03-02 19:21:08 -05:00
|
|
|
|
.Select(Convert.ToByte).ToArray();
|
2019-08-30 18:24:44 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-06 21:12:35 -05:00
|
|
|
|
|
2019-08-30 18:24:44 -04:00
|
|
|
|
// this happens when someone tries to send something that can't be converted into a 7 bit character set
|
|
|
|
|
// e.g: emoji -> windows-1252
|
2020-11-11 18:31:26 -05:00
|
|
|
|
catch (OverflowException ex)
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using (LogContext.PushProperty("Server", Endpoint.ToString()))
|
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
_log.LogError(ex, "Could not convert RCon data payload to desired encoding {Encoding} {Params}",
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_gameEncoding.EncodingName, parameters);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
throw new RConException("Invalid character encountered when converting encodings");
|
2022-03-02 19:21:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 16:05:16 -04:00
|
|
|
|
byte[][] response = null;
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2020-11-12 21:39:56 -05:00
|
|
|
|
retrySend:
|
|
|
|
|
if (connectionState.ConnectionAttempts > 1)
|
|
|
|
|
{
|
|
|
|
|
using (LogContext.PushProperty("Server", Endpoint.ToString()))
|
|
|
|
|
{
|
|
|
|
|
_log.LogInformation(
|
2022-04-25 16:39:30 -04:00
|
|
|
|
"Retrying RCon message ({ConnectionAttempts}/{AllowedConnectionFailures} attempts) with parameters {Payload}",
|
|
|
|
|
connectionState.ConnectionAttempts,
|
2021-09-18 19:25:02 -04:00
|
|
|
|
_retryAttempts, parameters);
|
2020-11-12 21:39:56 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-05 14:13:00 -05:00
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
|
2022-03-05 14:13:00 -05:00
|
|
|
|
{
|
|
|
|
|
DontFragment = false,
|
|
|
|
|
Ttl = 100,
|
|
|
|
|
ExclusiveAddressUse = true,
|
|
|
|
|
})
|
2018-10-02 13:39:08 -04:00
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
// wait for send to be ready
|
2022-03-02 19:21:08 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await connectionState.OnSentData.WaitAsync(token);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("OnSent did not complete in time");
|
2022-03-02 19:21:08 -05:00
|
|
|
|
throw new RConException("Timed out waiting for access to RCon send socket");
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
// wait for receive to be ready
|
2022-03-02 19:21:08 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await connectionState.OnReceivedData.WaitAsync(token);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("OnReceived did not complete in time");
|
2022-03-02 19:21:08 -05:00
|
|
|
|
if (connectionState.OnSentData.CurrentCount == 0)
|
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
connectionState.OnSentData.Release();
|
2022-03-02 19:21:08 -05:00
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
|
|
|
|
throw new RConException("Timed out waiting for access to RCon receive socket");
|
2022-03-02 19:21:08 -05:00
|
|
|
|
}
|
2022-03-05 14:13:00 -05:00
|
|
|
|
|
|
|
|
|
connectionState.SendEventArgs.UserToken = new ConnectionUserToken
|
|
|
|
|
{
|
|
|
|
|
Socket = socket,
|
|
|
|
|
CancellationToken = token
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
connectionState.ConnectionAttempts++;
|
2020-04-17 16:05:16 -04:00
|
|
|
|
connectionState.BytesReadPerSegment.Clear();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
_log.LogDebug(
|
|
|
|
|
"Sending {PayloadLength} bytes to [{Endpoint}] ({ConnectionAttempts}/{AllowedConnectionFailures})",
|
|
|
|
|
payload.Length, Endpoint, connectionState.ConnectionAttempts, _retryAttempts);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
try
|
2018-04-02 23:11:19 -04:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
connectionState.LastQuery = DateTime.Now;
|
2022-07-01 10:59:11 -04:00
|
|
|
|
var timeout = _parser.OverrideTimeoutForCommand(parameters);
|
|
|
|
|
waitForResponse = waitForResponse && timeout.HasValue;
|
2022-03-03 09:54:17 -05:00
|
|
|
|
response = await SendPayloadAsync(payload, waitForResponse,
|
2022-07-01 10:59:11 -04:00
|
|
|
|
timeout ?? TimeSpan.Zero, token);
|
2022-03-03 09:54:17 -05:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
if ((response?.Length == 0 || response[0].Length == 0) && waitForResponse)
|
2018-11-07 21:30:11 -05:00
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("0 bytes received from rcon request");
|
2020-11-11 18:31:26 -05:00
|
|
|
|
throw new RConException("Expected response but got 0 bytes back");
|
2018-11-07 21:30:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
connectionState.ConnectionAttempts = 0;
|
2018-04-02 23:11:19 -04:00
|
|
|
|
}
|
2018-09-29 22:49:12 -04:00
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
// if we timed out due to the cancellation token,
|
|
|
|
|
// we don't want to count that as an attempt
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("OperationCanceledException when waiting for payload send to complete");
|
2022-03-05 14:13:00 -05:00
|
|
|
|
connectionState.ConnectionAttempts = 0;
|
2022-03-03 09:54:17 -05:00
|
|
|
|
}
|
2018-11-07 21:30:11 -05:00
|
|
|
|
catch
|
2018-10-25 09:14:39 -04:00
|
|
|
|
{
|
2020-11-07 11:40:58 -05:00
|
|
|
|
// we want to retry with a delay
|
2021-09-18 19:25:02 -04:00
|
|
|
|
if (connectionState.ConnectionAttempts < _retryAttempts)
|
2018-10-25 09:14:39 -04:00
|
|
|
|
{
|
2022-03-02 19:21:08 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
await Task.Delay(StaticHelpers.SocketTimeout(connectionState.ConnectionAttempts), token);
|
2022-03-02 19:21:08 -05:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("OperationCancelled while waiting for retry");
|
|
|
|
|
throw;
|
2022-03-02 19:21:08 -05:00
|
|
|
|
}
|
2022-03-05 14:13:00 -05:00
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
goto retrySend;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using (LogContext.PushProperty("Server", Endpoint.ToString()))
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning(
|
2022-02-28 21:44:30 -05:00
|
|
|
|
"Made {ConnectionAttempts} attempts to send RCon data to server, but received no response",
|
2020-11-11 18:31:26 -05:00
|
|
|
|
connectionState.ConnectionAttempts);
|
|
|
|
|
}
|
2022-03-05 14:13:00 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
connectionState.ConnectionAttempts = 0;
|
|
|
|
|
throw new NetworkException("Reached maximum retry attempts to send RCon data to server");
|
2018-10-25 09:14:39 -04:00
|
|
|
|
}
|
2019-09-09 18:37:57 -04:00
|
|
|
|
finally
|
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
try
|
2019-09-09 18:37:57 -04:00
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
if (connectionState.OnSentData.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
connectionState.OnSentData.Release();
|
|
|
|
|
}
|
2022-02-02 17:21:08 -05:00
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
if (connectionState.OnReceivedData.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
connectionState.OnReceivedData.Release();
|
|
|
|
|
}
|
2022-02-02 17:21:08 -05:00
|
|
|
|
}
|
2022-03-05 14:13:00 -05:00
|
|
|
|
catch
|
2022-02-02 17:21:08 -05:00
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
// ignored because we can have the socket operation cancelled (which releases the semaphore) but
|
|
|
|
|
// this thread is not notified because it's an event
|
2022-02-02 17:21:08 -05:00
|
|
|
|
}
|
2019-09-09 18:37:57 -04:00
|
|
|
|
}
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
2018-04-02 23:11:19 -04:00
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
// at this point we can run in parallel and the next request can start because we have our data
|
2020-04-18 18:48:49 -04:00
|
|
|
|
if (response.Length == 0)
|
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
_log.LogDebug("Received empty response for RCon request {@Query}",
|
|
|
|
|
new { endpoint = Endpoint.ToString(), type, parameters });
|
|
|
|
|
return Array.Empty<string>();
|
2020-04-18 18:48:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-25 16:39:30 -04:00
|
|
|
|
var responseString = type == StaticHelpers.QueryType.COMMAND_STATUS
|
|
|
|
|
? ReassembleSegmentedStatus(response)
|
|
|
|
|
: RecombineMessages(response);
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
// note: not all games respond if the password is wrong or not set
|
2022-04-25 16:39:30 -04:00
|
|
|
|
if (responseString.Contains("Invalid password", StringComparison.InvariantCultureIgnoreCase) ||
|
|
|
|
|
responseString.Contains("rconpassword"))
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
throw new RConException(Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_RCON_INVALID"]);
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2020-01-13 17:51:16 -05:00
|
|
|
|
if (responseString.Contains("rcon_password"))
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
throw new RConException(Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_RCON_NOTSET"]);
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
if (responseString.Contains(_config.ServerNotRunningResponse))
|
2020-01-13 17:51:16 -05:00
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
throw new ServerException(Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_NOT_RUNNING"]
|
|
|
|
|
.FormatExt(Endpoint.ToString()));
|
2020-01-13 17:51:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
var responseHeaderMatch = Regex.Match(responseString, _config.CommandPrefixes.RConResponse).Value;
|
2022-04-25 16:39:30 -04:00
|
|
|
|
var headerSplit = responseString.Split(type == StaticHelpers.QueryType.GET_INFO
|
|
|
|
|
? _config.CommandPrefixes.RconGetInfoResponseHeader
|
|
|
|
|
: responseHeaderMatch);
|
2020-04-17 16:05:16 -04:00
|
|
|
|
|
2020-04-18 18:48:49 -04:00
|
|
|
|
if (headerSplit.Length != 2)
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using (LogContext.PushProperty("Server", Endpoint.ToString()))
|
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
_log.LogWarning("Invalid response header from server. Expected {Expected}, but got {Response}",
|
2022-03-03 09:54:17 -05:00
|
|
|
|
_config.CommandPrefixes.RConResponse, headerSplit.FirstOrDefault());
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new RConException("Unexpected response header from server");
|
2020-04-17 16:05:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 18:01:58 -04:00
|
|
|
|
var splitResponse = headerSplit.Last().Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Select(line => line.StartsWith("^7") ? line[2..] : line).ToArray();
|
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
return splitResponse;
|
|
|
|
|
}
|
2018-04-10 20:25:44 -04:00
|
|
|
|
|
2020-04-17 16:05:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// reassembles broken status segments into the 'correct' ordering
|
|
|
|
|
/// <remarks>this is primarily for T7, and is really only reliable for 2 segments</remarks>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="segments">array of segmented byte arrays</param>
|
|
|
|
|
/// <returns></returns>
|
2022-03-05 14:13:00 -05:00
|
|
|
|
private string ReassembleSegmentedStatus(IEnumerable<byte[]> segments)
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
|
|
|
|
var splitStatusStrings = new List<string>();
|
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
foreach (var segment in segments)
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
var responseString = _gameEncoding.GetString(segment, 0, segment.Length);
|
|
|
|
|
var statusHeaderMatch = _config.StatusHeader.PatternMatcher.Match(responseString);
|
2020-04-17 16:05:16 -04:00
|
|
|
|
if (statusHeaderMatch.Success)
|
|
|
|
|
{
|
2020-04-20 11:45:58 -04:00
|
|
|
|
splitStatusStrings.Insert(0, responseString.TrimEnd('\0'));
|
2020-04-17 16:05:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
splitStatusStrings.Add(responseString.Replace(_config.CommandPrefixes.RConResponse, "")
|
|
|
|
|
.TrimEnd('\0'));
|
2020-04-17 16:05:16 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Join("", splitStatusStrings);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 11:40:58 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Recombines multiple game messages into one
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="payload"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-03-03 09:54:17 -05:00
|
|
|
|
private string RecombineMessages(IReadOnlyList<byte[]> payload)
|
2020-11-07 11:40:58 -05:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
if (payload.Count == 1)
|
2020-11-07 11:40:58 -05:00
|
|
|
|
{
|
|
|
|
|
return _gameEncoding.GetString(payload[0]).TrimEnd('\n') + '\n';
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
for (var i = 0; i < payload.Count; i++)
|
2020-11-07 11:40:58 -05:00
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
var message = _gameEncoding.GetString(payload[i]).TrimEnd('\n') + '\n';
|
|
|
|
|
if (i > 0)
|
2020-11-07 11:40:58 -05:00
|
|
|
|
{
|
2022-03-05 14:13:00 -05:00
|
|
|
|
message = message.Replace(_config.CommandPrefixes.RConResponse, "");
|
2020-11-07 11:40:58 -05:00
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
builder.Append(message);
|
2020-11-07 11:40:58 -05:00
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
builder.Append('\n');
|
|
|
|
|
return builder.ToString();
|
2020-11-07 11:40:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
private async Task<byte[][]> SendPayloadAsync(byte[] payload, bool waitForResponse, TimeSpan overrideTimeout,
|
|
|
|
|
CancellationToken token = default)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
var connectionState = ActiveQueries[Endpoint];
|
2022-03-05 14:13:00 -05:00
|
|
|
|
var rconSocket = ((ConnectionUserToken)connectionState.SendEventArgs.UserToken)?.Socket;
|
2018-05-03 01:25:49 -04:00
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
if (rconSocket is null)
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("Invalid state");
|
2022-03-03 09:54:17 -05:00
|
|
|
|
throw new InvalidOperationException("State is not valid for socket operation");
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
if (connectionState.ReceiveEventArgs.RemoteEndPoint == null &&
|
|
|
|
|
connectionState.SendEventArgs.RemoteEndPoint == null)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
2018-10-02 13:39:08 -04:00
|
|
|
|
// setup the event handlers only once because we're reusing the event args
|
|
|
|
|
connectionState.SendEventArgs.Completed += OnDataSent;
|
|
|
|
|
connectionState.ReceiveEventArgs.Completed += OnDataReceived;
|
2022-04-25 16:39:30 -04:00
|
|
|
|
connectionState.ReceiveEventArgs.UserToken = connectionState.SendEventArgs.UserToken;
|
2022-03-05 14:13:00 -05:00
|
|
|
|
connectionState.SendEventArgs.RemoteEndPoint = Endpoint;
|
|
|
|
|
connectionState.ReceiveEventArgs.RemoteEndPoint = Endpoint;
|
2018-10-02 13:39:08 -04:00
|
|
|
|
connectionState.ReceiveEventArgs.DisconnectReuseSocket = true;
|
|
|
|
|
connectionState.SendEventArgs.DisconnectReuseSocket = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connectionState.SendEventArgs.SetBuffer(payload);
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
// send the data to the server
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var sendDataPending = rconSocket.SendToAsync(connectionState.SendEventArgs);
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
if (sendDataPending)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// the send has not been completed asynchronously
|
2020-11-07 11:40:58 -05:00
|
|
|
|
// this really shouldn't ever happen because it's UDP
|
2022-03-05 14:13:00 -05:00
|
|
|
|
var complete = await connectionState.OnSentData.WaitAsync(StaticHelpers.SocketTimeout(4), token);
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
if (!complete)
|
2018-10-02 13:39:08 -04:00
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
using (LogContext.PushProperty("Server", Endpoint.ToString()))
|
2020-11-11 18:31:26 -05:00
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
_log.LogWarning("Socket timed out while sending RCon data on attempt {Attempt}",
|
2020-11-11 18:31:26 -05:00
|
|
|
|
connectionState.ConnectionAttempts);
|
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
rconSocket.Close();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
throw new NetworkException("Timed out sending RCon data", rconSocket);
|
2018-10-02 13:39:08 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 22:15:59 -04:00
|
|
|
|
if (!waitForResponse)
|
|
|
|
|
{
|
2022-02-02 17:21:08 -05:00
|
|
|
|
return Array.Empty<byte[]>();
|
2018-10-08 22:15:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
connectionState.ReceiveEventArgs.SetBuffer(connectionState.ReceiveBuffer);
|
2018-04-02 23:11:19 -04:00
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
// get our response back
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var receiveDataPending = rconSocket.ReceiveFromAsync(connectionState.ReceiveEventArgs);
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
if (receiveDataPending)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("Waiting to asynchronously receive data on attempt #{ConnectionAttempts}",
|
|
|
|
|
connectionState.ConnectionAttempts);
|
2022-02-28 21:44:30 -05:00
|
|
|
|
|
|
|
|
|
var completed = false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
completed = await connectionState.OnReceivedData.WaitAsync(
|
|
|
|
|
new[]
|
|
|
|
|
{
|
|
|
|
|
StaticHelpers.SocketTimeout(connectionState.ConnectionAttempts),
|
|
|
|
|
overrideTimeout
|
|
|
|
|
}.Max(), token);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
2022-03-05 14:13:00 -05:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
if (!completed)
|
2018-10-02 13:39:08 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (connectionState.ConnectionAttempts > 1) // this reduces some spam for unstable connections
|
|
|
|
|
{
|
|
|
|
|
using (LogContext.PushProperty("Server", Endpoint.ToString()))
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning(
|
2022-03-03 09:54:17 -05:00
|
|
|
|
"Socket timed out while waiting for RCon response on attempt {Attempt} with timeout delay of {Timeout}",
|
2020-11-11 18:31:26 -05:00
|
|
|
|
connectionState.ConnectionAttempts,
|
|
|
|
|
StaticHelpers.SocketTimeout(connectionState.ConnectionAttempts));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 13:39:08 -04:00
|
|
|
|
rconSocket.Close();
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("OnDataReceived did not complete in allocated time");
|
2020-11-11 18:31:26 -05:00
|
|
|
|
throw new NetworkException("Timed out receiving RCon response", rconSocket);
|
2018-10-02 13:39:08 -04:00
|
|
|
|
}
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2019-04-23 18:27:20 -04:00
|
|
|
|
rconSocket.Close();
|
2020-11-07 11:40:58 -05:00
|
|
|
|
return GetResponseData(connectionState);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
private static byte[][] GetResponseData(ConnectionState connectionState)
|
2020-11-07 11:40:58 -05:00
|
|
|
|
{
|
2020-04-17 16:05:16 -04:00
|
|
|
|
var responseList = new List<byte[]>();
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var totalBytesRead = 0;
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
foreach (var bytesRead in connectionState.BytesReadPerSegment)
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
|
|
|
|
responseList.Add(connectionState.ReceiveBuffer
|
|
|
|
|
.Skip(totalBytesRead)
|
|
|
|
|
.Take(bytesRead)
|
|
|
|
|
.ToArray());
|
|
|
|
|
|
|
|
|
|
totalBytesRead += bytesRead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseList.ToArray();
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
private void OnDataReceived(object sender, SocketAsyncEventArgs e)
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("Read {BytesTransferred} bytes from {Endpoint}", e.BytesTransferred,
|
|
|
|
|
e.RemoteEndPoint?.ToString());
|
2020-04-17 16:05:16 -04:00
|
|
|
|
|
|
|
|
|
// this occurs when we close the socket
|
|
|
|
|
if (e.BytesTransferred == 0)
|
|
|
|
|
{
|
2020-12-12 22:43:27 -05:00
|
|
|
|
_log.LogDebug("No bytes were transmitted so the connection was probably closed");
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
var semaphore = ActiveQueries[Endpoint].OnReceivedData;
|
|
|
|
|
|
|
|
|
|
try
|
2022-02-02 17:21:08 -05:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
if (semaphore.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
2022-02-02 17:21:08 -05:00
|
|
|
|
}
|
2022-03-03 09:54:17 -05:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored because we can have the socket operation cancelled (which releases the semaphore) but
|
|
|
|
|
// this thread is not notified because it's an event
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 16:05:16 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
var state = ActiveQueries[Endpoint];
|
|
|
|
|
var cancellationRequested = ((ConnectionUserToken)e.UserToken)?.CancellationToken.IsCancellationRequested ??
|
|
|
|
|
false;
|
2020-04-17 16:05:16 -04:00
|
|
|
|
|
2022-03-05 14:13:00 -05:00
|
|
|
|
if (sender is not Socket sock || cancellationRequested)
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
var semaphore = ActiveQueries[Endpoint].OnReceivedData;
|
|
|
|
|
|
|
|
|
|
try
|
2022-02-02 17:21:08 -05:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
if (semaphore.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
2022-02-02 17:21:08 -05:00
|
|
|
|
}
|
2022-03-03 09:54:17 -05:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored because we can have the socket operation cancelled (which releases the semaphore) but
|
|
|
|
|
// this thread is not notified because it's an event
|
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2020-12-02 15:29:49 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2020-12-02 15:29:49 -05:00
|
|
|
|
state.BytesReadPerSegment.Add(e.BytesTransferred);
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2020-12-12 22:43:27 -05:00
|
|
|
|
// I don't even want to know why this works for getting more data from Cod4x
|
|
|
|
|
// but I'm leaving it in here as long as it doesn't break anything.
|
|
|
|
|
// it's very stupid...
|
|
|
|
|
Thread.Sleep(150);
|
|
|
|
|
|
2020-12-02 15:29:49 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var totalBytesTransferred = e.BytesTransferred;
|
2022-03-03 09:54:17 -05:00
|
|
|
|
_log.LogDebug("{Total} total bytes transferred with {Available} bytes remaining", totalBytesTransferred,
|
2022-02-02 17:21:08 -05:00
|
|
|
|
sock.Available);
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2020-12-02 15:29:49 -05:00
|
|
|
|
// we still have available data so the payload was segmented
|
|
|
|
|
while (sock.Available > 0)
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
_log.LogDebug("{Available} more bytes to be read", sock.Available);
|
2021-01-17 21:04:32 -05:00
|
|
|
|
|
|
|
|
|
var bufferSpaceAvailable = sock.Available + totalBytesTransferred - state.ReceiveBuffer.Length;
|
2022-02-02 17:21:08 -05:00
|
|
|
|
if (bufferSpaceAvailable >= 0)
|
2021-01-17 21:04:32 -05:00
|
|
|
|
{
|
2022-02-02 17:21:08 -05:00
|
|
|
|
_log.LogWarning(
|
2022-03-03 09:54:17 -05:00
|
|
|
|
"Not enough buffer space to store incoming data {BytesNeeded} additional bytes required",
|
2022-02-02 17:21:08 -05:00
|
|
|
|
bufferSpaceAvailable);
|
2021-01-17 23:12:18 -05:00
|
|
|
|
continue;
|
2021-01-17 21:04:32 -05:00
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2020-12-02 15:29:49 -05:00
|
|
|
|
state.ReceiveEventArgs.SetBuffer(state.ReceiveBuffer, totalBytesTransferred, sock.Available);
|
|
|
|
|
if (sock.ReceiveAsync(state.ReceiveEventArgs))
|
2020-04-17 16:05:16 -04:00
|
|
|
|
{
|
2020-12-12 22:43:27 -05:00
|
|
|
|
_log.LogDebug("Remaining bytes are async");
|
2020-12-02 15:29:49 -05:00
|
|
|
|
continue;
|
2020-04-17 16:05:16 -04:00
|
|
|
|
}
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
_log.LogDebug("Read {BytesTransferred} synchronous bytes from {Endpoint}",
|
|
|
|
|
state.ReceiveEventArgs.BytesTransferred, e.RemoteEndPoint?.ToString());
|
2022-04-25 16:39:30 -04:00
|
|
|
|
|
2020-12-02 15:29:49 -05:00
|
|
|
|
// we need to increment this here because the callback isn't executed if there's no pending IO
|
|
|
|
|
state.BytesReadPerSegment.Add(state.ReceiveEventArgs.BytesTransferred);
|
|
|
|
|
totalBytesTransferred += state.ReceiveEventArgs.BytesTransferred;
|
2020-04-17 16:05:16 -04:00
|
|
|
|
}
|
2020-12-02 15:29:49 -05:00
|
|
|
|
}
|
2020-04-17 16:05:16 -04:00
|
|
|
|
|
2020-12-02 15:29:49 -05:00
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
2020-12-12 22:43:27 -05:00
|
|
|
|
_log.LogDebug("Socket was disposed while receiving data");
|
2022-02-02 17:21:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
var semaphore = ActiveQueries[Endpoint].OnReceivedData;
|
|
|
|
|
try
|
2022-02-02 17:21:08 -05:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
if (semaphore.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored because we can have the socket operation cancelled (which releases the semaphore) but
|
|
|
|
|
// this thread is not notified because it's an event
|
2022-02-02 17:21:08 -05:00
|
|
|
|
}
|
2020-04-17 16:05:16 -04:00
|
|
|
|
}
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
private void OnDataSent(object sender, SocketAsyncEventArgs e)
|
|
|
|
|
{
|
2022-04-25 16:39:30 -04:00
|
|
|
|
_log.LogDebug("Sent {ByteCount} bytes to {Endpoint}", e.Buffer?.Length,
|
|
|
|
|
e.ConnectSocket?.RemoteEndPoint?.ToString());
|
|
|
|
|
|
2022-03-03 09:54:17 -05:00
|
|
|
|
var semaphore = ActiveQueries[Endpoint].OnSentData;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (semaphore.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
2022-02-02 17:21:08 -05:00
|
|
|
|
{
|
2022-03-03 09:54:17 -05:00
|
|
|
|
// ignored because we can have the socket operation cancelled (which releases the semaphore) but
|
|
|
|
|
// this thread is not notified because it's an event
|
2022-02-02 17:21:08 -05:00
|
|
|
|
}
|
2018-04-02 01:25:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-02 17:21:08 -05:00
|
|
|
|
}
|