using System.Net.Sockets; using System.Text; using Brutal.Strings; namespace Brutal.RakNetApi; /// /// Partial struct for SocketDescriptor that provides useful functionality. /// public unsafe partial struct SocketDescriptor { /// /// Create a SocketDescriptor with an IPV4 address family. /// public SocketDescriptor() : this(AddressFamily.InterNetwork) { } /// /// Create a socket descriptor for IPV4 with port and AddressFamily /// /// Port to use /// Address family to use public SocketDescriptor(int port, AddressFamily family = AddressFamily.InterNetwork) : this("", port, family) { } /// /// Create a SocketDescriptor with an AddressFamily /// public SocketDescriptor(AddressFamily addressFamily) : this(0, addressFamily) { } /// /// Create a SocketDescriptor with a host, port and optional AddressFamily. /// /// Host address or name /// Port to use /// The Address Family to use, defaults to IPV4 public SocketDescriptor(string host, int port, AddressFamily family = AddressFamily.InterNetwork) { SetHostAddress(host); Port = (ushort)port; SocketFamily = (short)family; BlockingSocket = true; RemotePortRakNetWasStartedOnPS3PSP2 = 0; ExtraSocketOptions = 0; } /// /// Sets the host address for the socket descriptor. The address must encode to at most /// - 1 UTF-8 bytes so the buffer stays null-terminated; /// any remaining buffer space is cleared. On failure the existing address is left unchanged. /// /// The host address as a string. /// The encoded address does not fit. public void SetHostAddress(string host) { Span 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(ptr, HostAddressLength); encoded[..bytesWritten].CopyTo(buffer); buffer[bytesWritten..].Clear(); } } /// /// Sets the host address for the socket descriptor using a Utf8String instance. /// Trailing null bytes in are ignored; the content must be at most /// - 1 bytes so the buffer stays null-terminated, and any /// remaining buffer space is cleared. On failure the existing address is left unchanged. /// /// The new host address /// The address content does not fit. public void SetHostAddress(ReadOnlySpan 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(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; }