コード例 #1
0
ファイル: Common.cs プロジェクト: Shadoku/DarkMultiPlayer
        public static ByteArray PrependNetworkFrame(int messageType, ByteArray messageData)
        {
            ByteArray returnBytes;

            //Get type bytes
            byte[] typeBytes = BitConverter.GetBytes(messageType);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(typeBytes);
            }
            if (messageData == null || messageData.Length == 0)
            {
                returnBytes = ByteRecycler.GetObject(8);
                typeBytes.CopyTo(returnBytes.data, 0);
                returnBytes.data[4] = 0;
                returnBytes.data[5] = 0;
                returnBytes.data[6] = 0;
                returnBytes.data[7] = 0;
            }
            else
            {
                //Get length bytes if we have a payload
                byte[] lengthBytes = BitConverter.GetBytes(messageData.Length);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(lengthBytes);
                }

                returnBytes = ByteRecycler.GetObject(8 + messageData.Length);
                typeBytes.CopyTo(returnBytes.data, 0);
                lengthBytes.CopyTo(returnBytes.data, 4);
                Array.Copy(messageData.data, 0, returnBytes.data, 8, messageData.Length);
            }
            return(returnBytes);
        }
コード例 #2
0
        public static ByteArray RemoveDecompressedHeader(ByteArray inputBytes)
        {
            if (inputBytes == null)
            {
                throw new Exception("Input bytes are null");
            }
            ByteArray returnBytes = ByteRecycler.GetObject(inputBytes.Length - 1);

            Array.Copy(inputBytes.data, 1, returnBytes.data, 0, inputBytes.Length - 1);
            return(returnBytes);
        }
コード例 #3
0
        /// <summary>
        /// Appends the decompressed header.
        /// </summary>
        /// <returns>The message with the prepended header</returns>
        /// <param name="inputBytes">Input bytes.</param>
        public static ByteArray AddCompressionHeader(ByteArray inputBytes, bool value)
        {
            if (inputBytes == null)
            {
                throw new Exception("Input bytes are null");
            }
            ByteArray returnBytes = ByteRecycler.GetObject(inputBytes.Length + 1);

            BitConverter.GetBytes(value).CopyTo(returnBytes.data, 0);
            Array.Copy(inputBytes.data, 0, returnBytes.data, 1, inputBytes.Length);
            return(returnBytes);
        }
コード例 #4
0
        public static ByteArray CompressIfNeeded(ByteArray inputBytes)
        {
            if (inputBytes == null)
            {
                throw new Exception("Input bytes are null");
            }
            if (inputBytes.Length < COMPRESSION_THRESHOLD || !compressionEnabled)
            {
                return(AddCompressionHeader(inputBytes, false));
            }
            ByteArray compressedBytes           = Compress(inputBytes);
            ByteArray compressedBytesWithHeader = AddCompressionHeader(compressedBytes, true);

            ByteRecycler.ReleaseObject(compressedBytes);
            return(compressedBytesWithHeader);
        }
コード例 #5
0
        public static ByteArray Compress(ByteArray inputBytes)
        {
            int compressSize = 0;

            lock (CompressionBuffer)
            {
                using (MemoryStream ms = new MemoryStream(CompressionBuffer))
                {
                    using (GZipStream gs = new GZipStream(ms, CompressionMode.Compress, true))
                    {
                        gs.Write(inputBytes.data, 0, inputBytes.Length);
                    }
                    compressSize = (int)ms.Position;
                }
            }
            ByteArray returnBytes = ByteRecycler.GetObject(compressSize);

            Array.Copy(CompressionBuffer, 0, returnBytes.data, 0, compressSize);
            return(returnBytes);
        }
コード例 #6
0
        public static ByteArray Decompress(ByteArray inputBytes)
        {
            ByteArray returnBytes = null;

            lock (CompressionBuffer)
            {
                int totalRead = 0;
                using (MemoryStream ms = new MemoryStream(inputBytes.data, 0, inputBytes.Length))
                {
                    using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
                    {
                        int thisRead;
                        while ((thisRead = gs.Read(CompressionBuffer, totalRead, 4096)) > 0)
                        {
                            totalRead += thisRead;
                        }
                    }
                }
                returnBytes = ByteRecycler.GetObject(totalRead);
                Array.Copy(CompressionBuffer, 0, returnBytes.data, 0, totalRead);
            }
            return(returnBytes);
        }