Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request BytexDigital#16 from BytexDigital/dev
Browse files Browse the repository at this point in the history
Update to 1.0.3-beta.5
  • Loading branch information
RyanTT authored Dec 13, 2021
2 parents fa3f13a + 6f8b98b commit fa7523f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion BytexDigital.BattlEye.Rcon.TestClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Program
{
static void Main(string[] args)
{
RconClient networkClient = new RconClient("127.0.0.1", 2310, "test");
RconClient networkClient = new RconClient("127.0.0.1", 2310, "local");
networkClient.Connected += NetworkClient_Connected;
networkClient.Disconnected += NetworkClient_Disconnected;
networkClient.MessageReceived += NetworkClient_MessageReceived;
Expand All @@ -31,6 +31,10 @@ static void Main(string[] args)
Console.WriteLine($"Players online: {onlinePlayers.Count}");
}

var bansFetchSuccess = networkClient.Fetch(new GetBansRequest(), 5000, out List<PlayerBan> bans);

if (bansFetchSuccess) Console.WriteLine($"{bans.Count} bans");

networkClient.Send(new SendMessageCommand("This is a global message"));

Console.ReadLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageLicenseUrl></PackageLicenseUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Description>This library provides an easy way of communicating with a BattlEye RCON server.</Description>
<Version>1.0.3-beta.4</Version>
<Version>1.0.3-beta.5</Version>
<PackageReleaseNotes>Improved parsing of player lists
Fixed an issue regarding sequence numbers of outbound packets
Renamed RconClient.Fetch to FetchAsync</PackageReleaseNotes>
Expand Down
43 changes: 30 additions & 13 deletions BytexDigital.BattlEye.Rcon/NetworkMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BytexDigital.BattlEye.Rcon.Requests;
using BytexDigital.BattlEye.Rcon.Responses;

using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -89,9 +90,9 @@ private void HandleMessagePacket(IEnumerable<byte> data)

try { _networkConnection.FireMessageReceived(content); } catch { }

string playerConnectedPattern = @"Verified GUID \((\S{32})\) of player #(\d+) (.+)";
string playerDisconnectedPattern = @"Player #(\d+) (.+) disconnected";
string playerRemovedPattern = @"Player #(\d+) (.+) \((\S{32})\) has been kicked by BattlEye: Admin (Kick|Ban)(?: \((.+)\))?";
const string playerConnectedPattern = @"Verified GUID \((\S{32})\) of player #(\d+) (.+)";
const string playerDisconnectedPattern = @"Player #(\d+) (.+) disconnected";
const string playerRemovedPattern = @"Player #(\d+) (.+) \((\S{32})\) has been kicked by BattlEye: Admin (Kick|Ban)(?: \((.+)\))?";

if (Regex.IsMatch(content, playerConnectedPattern))
{
Expand Down Expand Up @@ -141,11 +142,14 @@ private void HandleCommandResponse(IEnumerable<byte> data)
return;
}

var remainingBytes = data.Skip(1);
// Payload is either a list of bytes or empty.
// If the payload is empty, it is the acknowledgement packet of a command we sent.
// If the payload is not empty, its a response and implicitly also an acknowledgement packet.
var payload = data.Skip(1);

if (remainingBytes.Count() > 0)
if (payload.Count() > 0)
{
var header = remainingBytes.First();
var header = payload.First();

if (header == 0x00)
{ // Multi-part message
Expand All @@ -158,11 +162,17 @@ private void HandleCommandResponse(IEnumerable<byte> data)
var currentIndex = data.Skip(3).First();
var partData = data.Skip(4);

string result = _stringEncoder.GetString(partData.ToArray());
(request.Response as CommandNetworkResponse).AppendContent(result);
bool isLastPartOfMessage = expectedAmount == currentIndex + 1;

//string result = _stringEncoder.GetString(partData.ToArray());
(request.Response as CommandNetworkResponse).AppendContentBytes(partData.ToArray());

if (isLastPartOfMessage) // End of multi-part message
{
// Convert the collected bytes into a string
(request.Response as CommandNetworkResponse).ConvertCollectedBytesToContentString(bytes => _stringEncoder.GetString(bytes.ToArray()));

if (expectedAmount == currentIndex + 1)
{ // End of multi-part message
// Cleanup and fire off
RemoveRequest(request);
request.MarkResponseReceived();
}
Expand All @@ -177,10 +187,17 @@ private void HandleCommandResponse(IEnumerable<byte> data)
request.MarkResponseReceived();
}
}

if (!request.Acknowledged)
else
{
request.MarkAcknowledged();
// We received a packet without any further payload, mark the origin message as acknowledged locally if we haven't already (actually, this should only ever happen once!)
if (!request.Acknowledged)
{
request.MarkAcknowledged();
}

// Important! Remove the request from our local bag of requests that are awaiting some kind of response (or acknowledgement).
// This is important so that we never get requests with duplicate sequence numbers in our local storage. Duplicate sequence numbers must not exist!
RemoveRequest(request);
}
}

Expand Down
12 changes: 9 additions & 3 deletions BytexDigital.BattlEye.Rcon/Responses/CommandNetworkResponse.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BytexDigital.BattlEye.Rcon.Responses
{
public class CommandNetworkResponse : NetworkResponse
{
public string Content { get; private set; }

private readonly List<byte> _data = new List<byte>();

public CommandNetworkResponse(string content)
{
Content = content;
}

internal void AppendContent(string content)
internal void AppendContentBytes(IEnumerable<byte> data)
{
_data.AddRange(data);
}

internal void ConvertCollectedBytesToContentString(Func<List<byte>, string> converter)
{
Content += content;
Content = converter.Invoke(_data);
}
}
}

0 comments on commit fa7523f

Please sign in to comment.