static unsafe UIntPtr CompressCallback(IntPtr context, Native.ENetBuffer *inBuffers, UIntPtr inBufferCount, UIntPtr inLimit, IntPtr outData, UIntPtr outLimit) { if (context == IntPtr.Zero || inBufferCount == UIntPtr.Zero || inLimit == UIntPtr.Zero) { return(UIntPtr.Zero); } var handle = GCHandle.FromIntPtr(context); var compressor = handle.Target as ENetCompressor; byte[] input = null; compressor.BeginCompress(); for (int i = 0; i < inBufferCount.ToUInt32(); i++) { var inBuffer = inBuffers[i]; var count = (int)inBuffer.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, inBuffer.Data, inBuffer.DataLength); } compressor.Compress(input, count); } if (input != null) { ByteArrayPool.Shared.Return(input); } var result = compressor.EndCompress(); if (result.Length > outLimit.ToUInt32()) { return(UIntPtr.Zero); } var resultLen = (UIntPtr)result.Length; fixed(byte *src = result) { Platform.Current.MemoryCopy(outData, (IntPtr)src, resultLen); } return(resultLen); }
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()); }