Esempio n. 1
0
        private static int SerializeAddrInfos(ServiceCtx context, ulong responseBufferPosition, ulong responseBufferSize, IPHostEntry hostEntry, int port)
        {
            ulong originalBufferPosition = responseBufferPosition;
            ulong bufferPosition         = originalBufferPosition;

            byte[] hostName = Encoding.ASCII.GetBytes(hostEntry.HostName + '\0');

            using (WritableRegion region = context.Memory.GetWritableRegion(responseBufferPosition, (int)responseBufferSize))
            {
                Span <byte> data = region.Memory.Span;

                for (int i = 0; i < hostEntry.AddressList.Length; i++)
                {
                    IPAddress ip = hostEntry.AddressList[i];

                    if (ip.AddressFamily != AddressFamily.InterNetwork)
                    {
                        continue;
                    }

                    // NOTE: 0 = Any
                    AddrInfoSerializedHeader header = new AddrInfoSerializedHeader(ip, 0);
                    AddrInfo4          addr         = new AddrInfo4(ip, (short)port);
                    AddrInfoSerialized info         = new AddrInfoSerialized(header, addr, null, hostEntry.HostName);

                    data = info.Write(data);
                }

                uint sentinel = 0;
                MemoryMarshal.Write(data, ref sentinel);
                data = data[sizeof(uint)..];