Exemple #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)..];
Exemple #2
0
        private static List <AddrInfoSerialized> DeserializeAddrInfos(IVirtualMemoryManager memory, ulong address, ulong size)
        {
            List <AddrInfoSerialized> result = new List <AddrInfoSerialized>();

            ReadOnlySpan <byte> data = memory.GetSpan(address, (int)size);

            while (!data.IsEmpty)
            {
                AddrInfoSerialized info = AddrInfoSerialized.Read(data, out data);

                if (info == null)
                {
                    break;
                }

                result.Add(info);
            }

            return(result);
        }