Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR, Palworld Support (Tested on Minecraft and PalWorld) #57

Merged
merged 7 commits into from Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/CoreRCON/Constants.cs
Expand Up @@ -12,11 +12,12 @@ internal class Constants
/// Minimum size of a rcon packet
/// </summary>
internal const int MIN_PACKET_SIZE = 14;



/// <summary>
/// The size of the header of an RCON packet.
/// </summary>
internal const int PACKET_HEADER_SIZE = 8;
internal const int PACKET_HEADER_SIZE = 12;

/// <summary>
/// The size of the header of an RCON packet.
xobust marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -28,5 +29,6 @@ internal class Constants
/// Used to finde the end of a multi packet response.
/// </summary>
internal const string MULTI_PACKET_END_RESPONSE = "\0\u0001\0\0";

}
}
15 changes: 15 additions & 0 deletions src/CoreRCON/Exceptions.cs
Expand Up @@ -19,4 +19,19 @@ public AuthenticationException(string message, Exception innerException) : base(
{
}
}

public class AuthenticationFailedException : AuthenticationException
{
public AuthenticationFailedException()
{
}

public AuthenticationFailedException(string message) : base(message)
{
}

public AuthenticationFailedException(string message, Exception innerException) : base(message, innerException)
{
}
}
}
10 changes: 4 additions & 6 deletions src/CoreRCON/Extensions.cs
Expand Up @@ -134,18 +134,16 @@ public static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan? timeout
{
var delayTask = Task.Delay(timeout.Value, cts.Token);

var resultTask = await Task.WhenAny(task, delayTask);
var resultTask = await Task.WhenAny(task, delayTask).ConfigureAwait(false);
if (resultTask == delayTask)
{
// Operation cancelled
throw new TimeoutException();
}
else
{
cts.Cancel();
}

return await task;
ExusAltimus marked this conversation as resolved.
Show resolved Hide resolved
cts.Cancel();

return await task.ConfigureAwait(false);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/CoreRCON/PacketFormats/RCONPacket.cs
Expand Up @@ -69,14 +69,16 @@ internal static RCONPacket FromBytes(byte[] buffer)
internal byte[] ToBytes()
{
int bodyLength = Encoding.UTF8.GetByteCount(Body);
int size = Constants.PACKET_HEADER_SIZE + Constants.PACKET_PADDING_SIZE + bodyLength;
//
int packetSize = Constants.PACKET_HEADER_SIZE + Constants.PACKET_PADDING_SIZE + bodyLength;

byte[] packetBytes = new byte[size+4];
byte[] packetBytes = new byte[packetSize];
Span<byte> packetSpan = packetBytes;

// Write packet size
// Packet size parameter does not include the size of the size parameter itself
BinaryPrimitives.WriteInt32LittleEndian(packetSpan, size);
var normalizedPacketSize = packetSize - 4;
BinaryPrimitives.WriteInt32LittleEndian(packetSpan, normalizedPacketSize);
packetSpan = packetSpan.Slice(4);

// Write ID
Expand All @@ -87,7 +89,7 @@ internal byte[] ToBytes()
BinaryPrimitives.WriteInt32LittleEndian(packetSpan, (int)Type);

// Write body
Encoding.UTF8.GetBytes(Body, 0, Body.Length, packetBytes, 12);
Encoding.UTF8.GetBytes(Body, 0, Body.Length, packetBytes, Constants.PACKET_HEADER_SIZE);

packetBytes[packetBytes.Length - 2] = 0; // Null terminator for the body
packetBytes[packetBytes.Length - 1] = 0; // Null terminator for the package
Expand Down