mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-28 05:58:25 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
5 changed files with 784842 additions and 0 deletions
40
Brutal.RakNet/Manual/APIPartials/AddressOrGuid.cs
Normal file
40
Brutal.RakNet/Manual/APIPartials/AddressOrGuid.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
namespace Brutal.RakNetApi;
|
||||
|
||||
public partial struct AddressOrGUID
|
||||
{
|
||||
public static AddressOrGUID UnassignedGuid => new(RakNetGUID.UNASSIGNED_RAKNET_GUID);
|
||||
|
||||
public static AddressOrGUID From(RakNetGUID guid) => new(guid);
|
||||
|
||||
public static AddressOrGUID From(SystemAddress address) => new(address);
|
||||
|
||||
public AddressOrGUID(RakNetGUID guid)
|
||||
{
|
||||
RakNetGuid = guid;
|
||||
SystemAddress = SystemAddress.UNASSIGNED_SYSTEM_ADDRESS;
|
||||
}
|
||||
|
||||
public AddressOrGUID(SystemAddress systemAddress)
|
||||
{
|
||||
SystemAddress = systemAddress;
|
||||
RakNetGuid = RakNetGUID.UNASSIGNED_RAKNET_GUID;
|
||||
}
|
||||
|
||||
public string ToString(bool appendPort = false, char delimiter = ':')
|
||||
{
|
||||
return RakNetGuid != RakNetGUID.UNASSIGNED_RAKNET_GUID
|
||||
? RakNetGuid.ToString()
|
||||
: SystemAddress.ToString(appendPort, delimiter);
|
||||
}
|
||||
}
|
||||
|
||||
/*public readonly unsafe partial struct RakPeerInstance : IDisposable
|
||||
{
|
||||
public void Dispose()
|
||||
{
|
||||
if (impl != 0)
|
||||
{
|
||||
RakNetLibrary.DestroyInstance(this);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
70
Brutal.RakNet/Manual/APIPartials/RakNetGuid.cs
Normal file
70
Brutal.RakNet/Manual/APIPartials/RakNetGuid.cs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
namespace Brutal.RakNetApi;
|
||||
|
||||
public partial struct RakNetGUID : IEquatable<RakNetGUID>
|
||||
{
|
||||
/// <summary>
|
||||
/// A special RakNetGUID used in the API.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// UNASSIGNED_RAKNET_GUID is often means for concepts like "MY GUID" or "ANY GUID"
|
||||
/// </remarks>
|
||||
public static readonly RakNetGUID UNASSIGNED_RAKNET_GUID = new()
|
||||
{
|
||||
G = ulong.MaxValue,
|
||||
SystemIndex = ushort.MaxValue,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current <see cref="RakNetGUID"/> instance to its string representation.
|
||||
/// </summary>
|
||||
/// <returns>A string representation of the <see cref="RakNetGUID"/> instance. Returns "UNASSIGNED_RAKNET_GUID" if the instance is equal to <see cref="UNASSIGNED_RAKNET_GUID"/>, otherwise returns the value of the G field as a string.</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return this == UNASSIGNED_RAKNET_GUID ? "UNASSIGNED_RAKNET_GUID" : $"{G}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines an equality operator for comparing two instances of <see cref="RakNetGUID"/>.
|
||||
/// </summary>
|
||||
/// <param name="left">The first <see cref="RakNetGUID"/> instance to compare.</param>
|
||||
/// <param name="right">The second <see cref="RakNetGUID"/> instance to compare.</param>
|
||||
/// <returns>True if the two instances are equal; otherwise, false.</returns>
|
||||
public static bool operator ==(RakNetGUID left, RakNetGUID right) => left.G == right.G;
|
||||
|
||||
/// <summary>
|
||||
/// Defines an equality operator for comparing two instances of <see cref="RakNetGUID"/>.
|
||||
/// </summary>
|
||||
/// <param name="left">The first <see cref="RakNetGUID"/> instance to compare.</param>
|
||||
/// <param name="right">The second <see cref="RakNetGUID"/> instance to compare.</param>
|
||||
/// <returns>True if the two instances are equal; otherwise, false.</returns>
|
||||
public static bool operator !=(RakNetGUID left, RakNetGUID right) => !(left == right);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the current instance is equal to another instance of <see cref="RakNetGUID"/>.
|
||||
/// </summary>
|
||||
/// <param name="other">The <see cref="RakNetGUID"/> instance to compare with the current instance.</param>
|
||||
/// <returns>True if the current instance is equal to the specified instance; otherwise, false.</returns>
|
||||
public bool Equals(RakNetGUID other)
|
||||
{
|
||||
return G == other.G;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the current instance is equal to a specified object.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current instance.</param>
|
||||
/// <returns>True if the specified object is equal to the current instance; otherwise, false.</returns>
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is RakNetGUID other && Equals(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a hash code for the current instance of <see cref="RakNetGUID"/> based on its field values.
|
||||
/// </summary>
|
||||
/// <returns>A hash code representing the current instance.</returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return G.GetHashCode();
|
||||
}
|
||||
}
|
||||
42
Brutal.RakNet/Manual/APIPartials/SockaddrIn.cs
Normal file
42
Brutal.RakNet/Manual/APIPartials/SockaddrIn.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Brutal.RakNetApi
|
||||
{
|
||||
public partial struct SockaddrIn
|
||||
{
|
||||
[FieldOffset(1)]
|
||||
private byte SinFamilyBsd;
|
||||
|
||||
public ushort SinFamily
|
||||
{
|
||||
get => SockAddrUtility.Layout == SockAddrUtility.SockAddrLayout.Bsd ? SinFamilyBsd : SinFamilyNonBsd;
|
||||
set
|
||||
{
|
||||
if (SockAddrUtility.Layout == SockAddrUtility.SockAddrLayout.Bsd)
|
||||
SinFamilyBsd = (byte)value;
|
||||
else
|
||||
SinFamilyNonBsd = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static partial class SockAddrUtility
|
||||
{
|
||||
static SockAddrUtility()
|
||||
{
|
||||
Layout = IsNonBsdSockAddrLayout() ? SockAddrLayout.NonBsd : SockAddrLayout.Bsd;
|
||||
}
|
||||
|
||||
public enum SockAddrLayout : byte
|
||||
{
|
||||
Bsd = 0,
|
||||
NonBsd = 1,
|
||||
};
|
||||
|
||||
[LibraryImport("RakNetDLL", EntryPoint = "IsNonBsdSockAddrLayout")]
|
||||
[return: MarshalAs(UnmanagedType.U1)]
|
||||
public static partial bool IsNonBsdSockAddrLayout();
|
||||
|
||||
public static SockAddrLayout Layout { get; }
|
||||
}
|
||||
}
|
||||
115
Brutal.RakNet/Manual/APIPartials/SocketDescriptor.cs
Normal file
115
Brutal.RakNet/Manual/APIPartials/SocketDescriptor.cs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Brutal.Strings;
|
||||
|
||||
namespace Brutal.RakNetApi;
|
||||
|
||||
/// <summary>
|
||||
/// Partial struct for SocketDescriptor that provides useful functionality.
|
||||
/// </summary>
|
||||
public unsafe partial struct SocketDescriptor
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a SocketDescriptor with an IPV4 address family.
|
||||
/// </summary>
|
||||
public SocketDescriptor()
|
||||
: this(AddressFamily.InterNetwork) { }
|
||||
|
||||
/// <summary>
|
||||
/// Create a socket descriptor for IPV4 with port and AddressFamily
|
||||
/// </summary>
|
||||
/// <param name="port">Port to use</param>
|
||||
/// <param name="family">Address family to use</param>
|
||||
public SocketDescriptor(int port, AddressFamily family = AddressFamily.InterNetwork)
|
||||
: this("", port, family) { }
|
||||
|
||||
/// <summary>
|
||||
/// Create a SocketDescriptor with an AddressFamily
|
||||
/// </summary>
|
||||
public SocketDescriptor(AddressFamily addressFamily)
|
||||
: this(0, addressFamily) { }
|
||||
|
||||
/// <summary>
|
||||
/// Create a SocketDescriptor with a host, port and optional AddressFamily.
|
||||
/// </summary>
|
||||
/// <param name="host">Host address or name</param>
|
||||
/// <param name="port">Port to use</param>
|
||||
/// <param name="family">The Address Family to use, defaults to IPV4</param>
|
||||
public SocketDescriptor(string host, int port, AddressFamily family = AddressFamily.InterNetwork)
|
||||
{
|
||||
SetHostAddress(host);
|
||||
Port = (ushort)port;
|
||||
SocketFamily = (short)family;
|
||||
BlockingSocket = true;
|
||||
RemotePortRakNetWasStartedOnPS3PSP2 = 0;
|
||||
ExtraSocketOptions = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the host address for the socket descriptor. The address must encode to at most
|
||||
/// <see cref="HostAddressLength"/> - 1 UTF-8 bytes so the buffer stays null-terminated;
|
||||
/// any remaining buffer space is cleared. On failure the existing address is left unchanged.
|
||||
/// </summary>
|
||||
/// <param name="host">The host address as a string.</param>
|
||||
/// <exception cref="ArgumentException">The encoded address does not fit.</exception>
|
||||
public void SetHostAddress(string host)
|
||||
{
|
||||
Span<byte> encoded = stackalloc byte[HostAddressLength - 1];
|
||||
if (!Utf8.TryGetBytes(host, encoded, out var bytesWritten))
|
||||
throw new ArgumentException(
|
||||
$"Host address must encode to at most {HostAddressLength - 1} UTF-8 bytes to leave room for the null terminator.",
|
||||
nameof(host)
|
||||
);
|
||||
|
||||
fixed (byte* ptr = HostAddress)
|
||||
{
|
||||
var buffer = new Span<byte>(ptr, HostAddressLength);
|
||||
encoded[..bytesWritten].CopyTo(buffer);
|
||||
buffer[bytesWritten..].Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the host address for the socket descriptor using a Utf8String instance.
|
||||
/// Trailing null bytes in <paramref name="host"/> are ignored; the content must be at most
|
||||
/// <see cref="HostAddressLength"/> - 1 bytes so the buffer stays null-terminated, and any
|
||||
/// remaining buffer space is cleared. On failure the existing address is left unchanged.
|
||||
/// </summary>
|
||||
/// <param name="host">The new host address</param>
|
||||
/// <exception cref="ArgumentException">The address content does not fit.</exception>
|
||||
public void SetHostAddress(ReadOnlySpan<byte> host)
|
||||
{
|
||||
var content = host.TrimEnd((byte)0);
|
||||
if (content.Length >= HostAddressLength)
|
||||
throw new ArgumentException(
|
||||
$"Host address must be at most {HostAddressLength - 1} bytes to leave room for the null terminator.",
|
||||
nameof(host)
|
||||
);
|
||||
|
||||
fixed (byte* ptr = HostAddress)
|
||||
{
|
||||
var buffer = new Span<byte>(ptr, HostAddressLength);
|
||||
content.CopyTo(buffer);
|
||||
buffer[content.Length..].Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (HostAddress[0] != 0)
|
||||
{
|
||||
fixed (byte* ptr = HostAddress)
|
||||
{
|
||||
var length = 0;
|
||||
while (length < HostAddressLength && ptr[length] != 0)
|
||||
length++;
|
||||
var s = Encoding.UTF8.GetString(ptr, length);
|
||||
return $"{s}:{Port}";
|
||||
}
|
||||
}
|
||||
return $"{Port}";
|
||||
}
|
||||
|
||||
// Convenience value useful when you're copying into or out of the HostAddress array.
|
||||
public const int HostAddressLength = 32;
|
||||
}
|
||||
34
Brutal.RakNet/Manual/APIPartials/SystemAddress.cs
Normal file
34
Brutal.RakNet/Manual/APIPartials/SystemAddress.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using System.Buffers.Binary;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Brutal.RakNetApi;
|
||||
|
||||
public partial struct SystemAddress
|
||||
{
|
||||
public SystemAddress()
|
||||
{
|
||||
// All other fields should be initialised to their default values, which will be 0.
|
||||
Address.Addr4.SinFamily = (ushort)AddressFamily.InterNetwork;
|
||||
SystemIndex = unchecked((ushort)(-1));
|
||||
DebugPort = 0;
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public string ToString(bool includePort = false, char portDelimiter = ':')
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* b = &Address.Addr4.SinAddr.SUn.SUnB.SB1)
|
||||
{
|
||||
var ip = new IPAddress(new ReadOnlySpan<byte>(b, sizeof(SUnBSUnInAddr)));
|
||||
var port = BinaryPrimitives.ReverseEndianness(Address.Addr4.SinPort);
|
||||
return ip + (includePort ? $"{portDelimiter}{port}" : "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly SystemAddress UNASSIGNED_SYSTEM_ADDRESS = new();
|
||||
}
|
||||
Loading…
Reference in a new issue