Skip to content

Commit

Permalink
Update Protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Jan 20, 2024
1 parent 24ca65e commit c4ad68f
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 76 deletions.
2 changes: 1 addition & 1 deletion OpenGSQ/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace OpenGSQ
/// <summary>
/// Provides extension methods for the BinaryReader class.
/// </summary>
public static class BinaryReaderExtensions
internal static class BinaryReaderExtensions
{
/// <summary>
/// Calculates the number of bytes remaining in the BinaryReader's BaseStream.
Expand Down
28 changes: 23 additions & 5 deletions OpenGSQ/OpenGSQ.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<PropertyGroup>
<NetCoreAppCurrent>net8.0</NetCoreAppCurrent>
<NetCoreAppPrevious>net7.0</NetCoreAppPrevious>
<NetCoreAppMinimum>net6.0</NetCoreAppMinimum>
<NetFrameworkMinimum>net462</NetFrameworkMinimum>
</PropertyGroup>

<PropertyGroup>
<AssemblyTitle>OpenGSQ</AssemblyTitle>
<VersionPrefix>2.1.0</VersionPrefix>
<Version>2.1.0</Version>
<VersionPrefix>2.1.1</VersionPrefix>
<Version>2.1.1</Version>
<Authors>OpenGSQ, BattlefieldDuck</Authors>
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.1;netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AssemblyName>OpenGSQ</AssemblyName>
<PackageId>OpenGSQ</PackageId>
<PackageIcon>icon.png</PackageIcon>
Expand All @@ -22,6 +25,17 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup Label="SourceLink">
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
Expand All @@ -43,4 +57,8 @@
</None>
</ItemGroup>

<ItemGroup Label="SourceLink">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

</Project>
5 changes: 1 addition & 4 deletions OpenGSQ/Protocols/GameSpy1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,8 @@ private async Task<byte[]> ConnectAndSend(string request)
{
using (var udpClient = new System.Net.Sockets.UdpClient())
{
udpClient.Client.SendTimeout = Timeout;
udpClient.Client.ReceiveTimeout = Timeout;

// Connect to remote host
udpClient.Connect(Host, Port);
udpClient.Connect(Host, Port, Timeout);

// Send Request
byte[] datagram = Encoding.ASCII.GetBytes(request);
Expand Down
8 changes: 3 additions & 5 deletions OpenGSQ/Protocols/GameSpy3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GameSpy3 : ProtocolBase
/// <summary>
/// A boolean indicating whether to use the challenge method.
/// </summary>
protected bool Challenge;
protected bool ChallengeRequired;

/// <summary>
/// Initializes a new instance of the GameSpy3 class.
Expand Down Expand Up @@ -64,14 +64,12 @@ private async Task<byte[]> ConnectAndSendPackets()
using (var udpClient = new System.Net.Sockets.UdpClient())
{
// Connect to remote host
udpClient.Connect(Host, Port);
udpClient.Client.SendTimeout = Timeout;
udpClient.Client.ReceiveTimeout = Timeout;
udpClient.Connect(Host, Port, Timeout);

// Packet 1: Initial request
byte[] responseData, challenge = new byte[] { }, requestData = new byte[] { 0xFE, 0xFD, 0x09, 0x04, 0x05, 0x06, 0x07 };

if (Challenge)
if (ChallengeRequired)
{
await udpClient.SendAsync(requestData, requestData.Length);

Expand Down
2 changes: 1 addition & 1 deletion OpenGSQ/Protocols/GameSpy4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GameSpy4 : GameSpy3
/// <param name="timeout">The timeout for the connection in milliseconds.</param>
public GameSpy4(string host, int port, int timeout = 5000) : base(host, port, timeout)
{
Challenge = true;
ChallengeRequired = true;
}
}
}
2 changes: 1 addition & 1 deletion OpenGSQ/Protocols/Minecraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<Dictionary<string, object>> GetStatus(int version = 47)
using (var tcpClient = new System.Net.Sockets.TcpClient())
{
tcpClient.ReceiveTimeout = Timeout;
await tcpClient.ConnectAsync(Host, Port);
await tcpClient.ConnectAsync(Host, Port, Timeout);
await tcpClient.SendAsync(request);

byte[] response = await tcpClient.ReceiveAsync();
Expand Down
7 changes: 2 additions & 5 deletions OpenGSQ/Protocols/Source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,8 @@ private async Task<byte[]> ConnectAndSendChallenge(byte[] header)
{
using (var udpClient = new System.Net.Sockets.UdpClient())
{
udpClient.Client.SendTimeout = Timeout;
udpClient.Client.ReceiveTimeout = Timeout;

// Connect to remote host
udpClient.Connect(Host, Port);
udpClient.Connect(Host, Port, Timeout);

// Set up request base
byte[] requestBase = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }.Concat(header).ToArray();
Expand Down Expand Up @@ -490,7 +487,7 @@ public async Task Authenticate(string password)

// Connect
_tcpClient = new System.Net.Sockets.TcpClient();
await _tcpClient.ConnectAsync(Host, Port);
await _tcpClient.ConnectAsync(Host, Port, Timeout);
_tcpClient.Client.SendTimeout = Timeout;
_tcpClient.Client.ReceiveTimeout = Timeout;

Expand Down
6 changes: 1 addition & 5 deletions OpenGSQ/Protocols/TeamSpeak3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ private async Task<string> SendAndReceive(byte[] data)
{
using (var tcpClient = new System.Net.Sockets.TcpClient())
{
// Set the timeout
tcpClient.SendTimeout = Timeout;
tcpClient.ReceiveTimeout = Timeout;

await tcpClient.ConnectAsync(Host, Port);
await tcpClient.ConnectAsync(Host, Port, Timeout);

// Welcome message from the server
await tcpClient.ReceiveAsync();
Expand Down
42 changes: 41 additions & 1 deletion OpenGSQ/Responses/Battlefield/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,55 @@ public class Info
public int RoundTime { get; set; }

// Additional properties based on your code

/// <summary>
/// Gets or sets the game mod. This property is optional.
/// </summary>
public string Mod { get; set; }

/// <summary>
/// Gets or sets the IP port of the game server. This property is optional.
/// </summary>
public string IpPort { get; set; }

/// <summary>
/// Gets or sets the version of PunkBuster. This property is optional.
/// </summary>
public string PunkBusterVersion { get; set; }
public bool JoinQueue { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the join queue is enabled. This property is optional.
/// </summary>
public bool? JoinQueue { get; set; }

/// <summary>
/// Gets or sets the region of the game server. This property is optional.
/// </summary>
public string Region { get; set; }

/// <summary>
/// Gets or sets the ping site of the game server. This property is optional.
/// </summary>
public string PingSite { get; set; }

/// <summary>
/// Gets or sets the country of the game server. This property is optional.
/// </summary>
public string Country { get; set; }

/// <summary>
/// Gets or sets the number of players in the Blaze game state. This property is optional.
/// </summary>
public int? BlazePlayerCount { get; set; }

/// <summary>
/// Gets or sets the Blaze game state. This property is optional.
/// </summary>
public string BlazeGameState { get; set; }

/// <summary>
/// Gets or sets a value indicating whether quick match is enabled. This property is optional.
/// </summary>
public bool? QuickMatch { get; set; }
}
}
62 changes: 53 additions & 9 deletions OpenGSQ/Socket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using OpenGSQ.Exceptions;

Expand All @@ -7,8 +8,27 @@ namespace OpenGSQ
/// <summary>
/// A class for handling UDP client communication.
/// </summary>
public static class UdpClient
internal static class UdpClient
{
/// <summary>
/// Extension method for the UdpClient class to connect to a server with a specified timeout.
/// </summary>
/// <param name="udpClient">The UdpClient instance on which the extension method is invoked.</param>
/// <param name="host">The host name or IP address of the remote server.</param>
/// <param name="port">The port number of the remote server.</param>
/// <param name="timeout">The send and receive timeout value for the UdpClient in milliseconds.</param>
public static void Connect(this System.Net.Sockets.UdpClient udpClient, string host, int port, int timeout)
{
// Set the send timeout
udpClient.Client.SendTimeout = timeout;

// Set the receive timeout
udpClient.Client.ReceiveTimeout = timeout;

// Connect to the remote server
udpClient.Connect(host, port);
}

/// <summary>
/// Sends data to a connected UdpClient and returns the response.
/// </summary>
Expand Down Expand Up @@ -39,7 +59,7 @@ public static async Task<byte[]> CommunicateAsync(ProtocolBase protocolBase, byt
/// <summary>
/// A class that extends the UdpClient class with a Communicate method.
/// </summary>
public static class UdpClientExtensions
internal static class UdpClientExtensions
{
/// <summary>
/// Receives a UDP datagram asynchronously with a timeout.
Expand Down Expand Up @@ -67,7 +87,7 @@ public static async Task<byte[]> ReceiveAsyncWithTimeout(this System.Net.Sockets
/// <summary>
/// A class for handling TCP client communication.
/// </summary>
public static class TcpClient
internal static class TcpClient
{
/// <summary>
/// Sends data to a connected TcpClient and returns the response.
Expand All @@ -80,12 +100,8 @@ public static async Task<byte[]> CommunicateAsync(ProtocolBase protocolBase, byt
{
using (var tcpClient = new System.Net.Sockets.TcpClient())
{
// Set the timeout
tcpClient.SendTimeout = protocolBase.Timeout;
tcpClient.ReceiveTimeout = protocolBase.Timeout;

// Connect to the server
await tcpClient.ConnectAsync(protocolBase.Host, protocolBase.Port);
await tcpClient.ConnectAsync(protocolBase.Host, protocolBase.Port, protocolBase.Timeout);

// Send the data
await tcpClient.SendAsync(data);
Expand All @@ -98,8 +114,36 @@ public static async Task<byte[]> CommunicateAsync(ProtocolBase protocolBase, byt
/// <summary>
/// A class that extends the TcpClient class with a Communicate method.
/// </summary>
public static class TcpClientExtensions
internal static class TcpClientExtensions
{
/// <summary>
/// Extension method for the TcpClient class to connect to a server with a specified timeout.
/// </summary>
/// <param name="tcpClient">The TcpClient instance on which the extension method is invoked.</param>
/// <param name="host">The host name or IP address of the remote server.</param>
/// <param name="port">The port number of the remote server.</param>
/// <param name="timeout">The send and receive timeout value for the TcpClient in milliseconds.</param>
/// <returns>A Task representing the asynchronous operation.</returns>
/// <exception cref="TimeoutException">Thrown when the operation times out.</exception>
public static async Task ConnectAsync(this System.Net.Sockets.TcpClient tcpClient, string host, int port, int timeout)
{
// Set the timeout
tcpClient.SendTimeout = timeout;
tcpClient.ReceiveTimeout = timeout;

try
{
using (var cts = new CancellationTokenSource(timeout))
{
await tcpClient.ConnectAsync(host, port, cts.Token);

Check failure on line 138 in OpenGSQ/Socket.cs

View workflow job for this annotation

GitHub Actions / build

No overload for method 'ConnectAsync' takes 3 arguments

Check failure on line 138 in OpenGSQ/Socket.cs

View workflow job for this annotation

GitHub Actions / build

No overload for method 'ConnectAsync' takes 3 arguments

Check failure on line 138 in OpenGSQ/Socket.cs

View workflow job for this annotation

GitHub Actions / build

No overload for method 'ConnectAsync' takes 3 arguments

Check failure on line 138 in OpenGSQ/Socket.cs

View workflow job for this annotation

GitHub Actions / build

No overload for method 'ConnectAsync' takes 3 arguments

Check failure on line 138 in OpenGSQ/Socket.cs

View workflow job for this annotation

GitHub Actions / build

No overload for method 'ConnectAsync' takes 3 arguments

Check failure on line 138 in OpenGSQ/Socket.cs

View workflow job for this annotation

GitHub Actions / build

No overload for method 'ConnectAsync' takes 3 arguments
}
}
catch (System.OperationCanceledException ex)
{
throw new TimeoutException($"The operation has timed out. {ex.Message}");
}
}

/// <summary>
/// Sends data to a connected TcpClient.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion OpenGSQTests/OpenGSQTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion OpenGSQTests/Protocols/KillingFloorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class KillingFloorTests : TestBase

public KillingFloorTests() : base(nameof(KillingFloorTests))
{
EnableSave = true;
// EnableSave = true;
}

[TestMethod()]
Expand Down
6 changes: 3 additions & 3 deletions OpenGSQTests/Results/KillingFloorTests/GetDetailsTest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"WaveCurrent": 5,
"WaveCurrent": 11,
"WaveTotal": 10,
"ServerId": 0,
"ServerIP": "",
"GamePort": 7707,
"QueryPort": 0,
"ServerName": "WS-GAMING.EU | NORMAL | 60LVL [#1]",
"MapName": "KF-DefenceNight-Betav1",
"MapName": "KF-No_MercyIII_Betav2Go4pfix",
"GameType": "UZGameType",
"NumPlayers": 12,
"NumPlayers": 14,
"MaxPlayers": 25,
"Ping": 0,
"Flags": 64,
Expand Down
Loading

0 comments on commit c4ad68f

Please sign in to comment.