示例#1
0
        private uint ChecksumCallback(Native.ENetBuffer *buffers, UIntPtr buffersCount)
        {
            Checksum.Begin();
            byte[] input = null;

            for (int i = 0; i < buffersCount.ToUInt32(); i++)
            {
                var buffer = buffers[i];

                if (Checksum.Method == ENetChecksum.ChecksumMethod.Pointer)
                {
                    Checksum.Sum((byte *)buffer.Data, (int)buffer.DataLength);
                    continue;
                }

                if (Checksum.Method == ENetChecksum.ChecksumMethod.Array)
                {
                    var count = (int)buffer.DataLength.ToUInt32();
                    if (input == null)
                    {
                        input = ByteArrayPool.Shared.Rent(count);
                    }
                    else if (input.Length < count)
                    {
                        ByteArrayPool.Shared.Return(input);
                        input = ByteArrayPool.Shared.Rent(count);
                    }

                    fixed(byte *dest = input)
                    {
                        Platform.Current.MemoryCopy((IntPtr)dest, buffer.Data, buffer.DataLength);
                    }

                    Checksum.Sum(input, count);
                    continue;
                }

                throw new NotImplementedException(Checksum.Method.ToString());
            }

            if (input != null)
            {
                ByteArrayPool.Shared.Return(input);
            }

            return(Checksum.End());
        }