Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="prevFrame"></param>
        /// <returns></returns>
        public byte[] Compress(ref uint prevFrame, out int size)
        {
                        #if DONT_USE_DELTA
            size = queue.Last().Data.Length;
            return(NetworkEngine.Compress(queue.Last().Data));
                        #endif

            var lastSnapshot = queue.Last();
            size = lastSnapshot.Data.Length;

            var prevFrameLocal = prevFrame;

            var prevSnapshot = queue.SingleOrDefault(s => s.Frame == prevFrameLocal);

            if (prevSnapshot == null)
            {
                prevFrame = 0;
                return(NetDeflate.Compress(lastSnapshot.Data));
            }


            var delta   = new byte[lastSnapshot.Data.Length];
            var minSize = Math.Min(delta.Length, prevSnapshot.Data.Length);

            for (int i = 0; i < minSize; i++)
            {
                delta[i] = (byte)(lastSnapshot.Data[i] ^ prevSnapshot.Data[i]);
            }

            if (delta.Length > prevSnapshot.Data.Length)
            {
                for (int i = minSize; i < delta.Length; i++)
                {
                    delta[i] = lastSnapshot.Data[i];
                }
            }

            return(NetDeflate.Compress(delta));
        }