Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/CoreRCON/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ internal class Constants
internal const int PACKET_HEADER_SIZE = 12;

/// <summary>
/// The size of the padding of an RCON packet.
/// The packet is padded with two null bytes.
/// One to terminate the string and one to terminate the packet.
/// The size of the header of an RCON packet.
/// </summary>
internal const int PACKET_PADDING_SIZE = 2;

Expand Down
15 changes: 15 additions & 0 deletions src/CoreRCON/Exceptions.cs
Original file line number Diff line number Diff line change
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)
{
}
}
}
18 changes: 7 additions & 11 deletions src/CoreRCON/Extensions.cs
Original file line number Diff line number Diff line change
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;
cts.Cancel();

return await task.ConfigureAwait(false);
}
}

Expand All @@ -166,11 +164,9 @@ public static async Task TimeoutAfter(this Task task, TimeSpan? timeout)
{
throw new TimeoutException();
}
else
{
// Cancel the timer task so that it does not fire
cts.Cancel();
}

// Cancel the timer task so that it does not fire
cts.Cancel();
await task;
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/CoreRCON/PacketFormats/RCONPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ internal static RCONPacket FromBytes(byte[] buffer)
internal byte[] ToBytes()
{
int bodyLength = Encoding.UTF8.GetByteCount(Body);
int totalLength = Constants.PACKET_HEADER_SIZE + bodyLength + Constants.PACKET_PADDING_SIZE;
byte[] packetBytes = new byte[totalLength];
//
int packetSize = Constants.PACKET_HEADER_SIZE + Constants.PACKET_PADDING_SIZE + bodyLength;

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, totalLength - 4);
var normalizedPacketSize = packetSize - 4;
BinaryPrimitives.WriteInt32LittleEndian(packetSpan, normalizedPacketSize);
packetSpan = packetSpan.Slice(4);

// Write ID
Expand All @@ -84,12 +87,12 @@ internal byte[] ToBytes()

// Write type
BinaryPrimitives.WriteInt32LittleEndian(packetSpan, (int)Type);
packetSpan = packetSpan.Slice(4);

// Write body
Encoding.UTF8.GetBytes(Body, 0, Body.Length, packetBytes, Constants.PACKET_HEADER_SIZE);
packetSpan[bodyLength] = 0; // Null terminator for the body
packetBytes[totalLength - 1] = 0; // Null terminator for the package

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

return packetBytes;
}
Expand Down
Loading