public void Serialize(NetOutgoingMessage lidgrenMsg)
        {
            lidgrenMsg.Write(Module);

            CachedQlz.Compress(ref Data, ref NumBytes);
            lidgrenMsg.Write(NumBytes);
            lidgrenMsg.Write(Data, 0, NumBytes);
        }
Beispiel #2
0
        public void Serialize(NetOutgoingMessage lidgrenMsg)
        {
            GuidUtil.Serialize(ContractGuid, lidgrenMsg);

            CachedQlz.Compress(ref Data, ref NumBytes);
            lidgrenMsg.Write(NumBytes);
            lidgrenMsg.Write(Data, 0, NumBytes);
        }
Beispiel #3
0
        public void Serialize(NetOutgoingMessage lidgrenMsg)
        {
            lidgrenMsg.Write(KerbalName);

            CachedQlz.Compress(ref KerbalData, ref NumBytes);
            lidgrenMsg.Write(NumBytes);
            lidgrenMsg.Write(KerbalData, 0, NumBytes);
        }
Beispiel #4
0
        internal override void InternalSerialize(NetOutgoingMessage lidgrenMsg)
        {
            base.InternalSerialize(lidgrenMsg);

            CachedQlz.Compress(ref Data, ref NumBytes);
            lidgrenMsg.Write(NumBytes);
            lidgrenMsg.Write(Data, 0, NumBytes);
        }
        public void TestCheckArrayIsNotCompressed()
        {
            var data = new byte[100];

            new Random().NextBytes(data);

            Assert.IsFalse(CachedQlz.IsCompressed(data, 100));
        }
Beispiel #6
0
 public static void ThreadSafeCompress(object lockObj, ref byte[] data, ref int numBytes)
 {
     lock (lockObj)
     {
         if (!CachedQlz.IsCompressed(data, numBytes))
         {
             CachedQlz.Compress(ref data, ref numBytes);
         }
     }
 }
Beispiel #7
0
        public void Serialize(NetOutgoingMessage lidgrenMsg)
        {
            lidgrenMsg.Write(FolderName);
            lidgrenMsg.Write(CraftName);
            lidgrenMsg.Write((int)CraftType);

            CachedQlz.Compress(ref Data, ref NumBytes);
            lidgrenMsg.Write(NumBytes);
            lidgrenMsg.Write(Data, 0, NumBytes);
        }
        public void TestCheckArrayIsCompressed_NoIssues()
        {
            var numBytes = 5000;

            var text = Encoding.ASCII.GetBytes(TestCommon.RandomString(numBytes));

            CachedQlz.Compress(ref text, ref numBytes);

            Assert.IsTrue(CachedQlz.IsCompressed(text, numBytes));
        }
        public void CompressData_NoIssues()
        {
            var originalLength = 5000;
            var numBytes       = originalLength;

            var text = Encoding.ASCII.GetBytes(TestCommon.RandomString(numBytes));

            CachedQlz.Compress(ref text, ref numBytes);

            Assert.IsTrue(originalLength > numBytes);
        }
        public void TestCheckArrayIsCompressed_ImpossibleToCompress()
        {
            var numBytes = 100;

            var data = new byte[numBytes];

            new Random().NextBytes(data);
            CachedQlz.Compress(ref data, ref numBytes);

            Assert.IsTrue(CachedQlz.IsCompressed(data, numBytes));
        }
        public void CompressData_ImpossibleToCompress()
        {
            var originalLength = 100;
            var numBytes       = originalLength;

            var data = new byte[numBytes];

            new Random().NextBytes(data);
            CachedQlz.Compress(ref data, ref numBytes);

            Assert.IsTrue(originalLength <= numBytes);
        }
        internal override void InternalSerialize(NetOutgoingMessage lidgrenMsg)
        {
            base.InternalSerialize(lidgrenMsg);

            lidgrenMsg.Write(SubspaceId);
            GuidUtil.Serialize(DominantVesselId, lidgrenMsg);
            GuidUtil.Serialize(WeakVesselId, lidgrenMsg);

            CachedQlz.Compress(ref FinalVesselData, ref NumBytes);
            lidgrenMsg.Write(NumBytes);
            lidgrenMsg.Write(FinalVesselData, 0, NumBytes);
        }
Beispiel #13
0
        internal override void InternalDeserialize(NetIncomingMessage lidgrenMsg)
        {
            base.InternalDeserialize(lidgrenMsg);

            NumBytes = lidgrenMsg.ReadInt32();
            if (Data.Length < NumBytes)
            {
                Data = new byte[NumBytes];
            }

            lidgrenMsg.ReadBytes(Data, 0, NumBytes);
            CachedQlz.Decompress(ref Data, out NumBytes);
        }
Beispiel #14
0
        public void Deserialize(NetIncomingMessage lidgrenMsg)
        {
            ContractGuid = GuidUtil.Deserialize(lidgrenMsg);

            NumBytes = lidgrenMsg.ReadInt32();
            if (Data.Length < NumBytes)
            {
                Data = new byte[NumBytes];
            }

            lidgrenMsg.ReadBytes(Data, 0, NumBytes);
            CachedQlz.Decompress(ref Data, out NumBytes);
        }
Beispiel #15
0
        public void Deserialize(NetIncomingMessage lidgrenMsg)
        {
            Module = lidgrenMsg.ReadString();

            NumBytes = lidgrenMsg.ReadInt32();
            if (Data.Length < NumBytes)
            {
                Data = new byte[NumBytes];
            }

            lidgrenMsg.ReadBytes(Data, 0, NumBytes);
            CachedQlz.Decompress(ref Data, out NumBytes);
        }
Beispiel #16
0
 public static void ThreadSafeDecompress(object lockObj, ref byte[] data, int length, out int numBytes)
 {
     lock (lockObj)
     {
         if (CachedQlz.IsCompressed(data, length))
         {
             CachedQlz.Decompress(ref data, out numBytes);
         }
         else
         {
             numBytes = length;
         }
     }
 }
Beispiel #17
0
        public void DecompressThreadSafe()
        {
            const int iterations = 1000;
            var       length     = 100000;

            var data = Encoding.ASCII.GetBytes(TestCommon.RandomString(length));

            CachedQlz.Compress(ref data, ref length);

            var task1Ok = true;
            var task1   = Task.Run(() =>
            {
                try
                {
                    for (var i = 0; i < iterations; i++)
                    {
                        var clone = TestCommon.CloneArray(data);
                        CachedQlz.Decompress(ref clone, out _);
                    }
                }
                catch (Exception)
                {
                    task1Ok = false;
                }
            });

            var task2Ok = true;
            var task2   = Task.Run(() =>
            {
                try
                {
                    for (var i = 0; i < iterations; i++)
                    {
                        var clone = TestCommon.CloneArray(data);
                        CachedQlz.Decompress(ref clone, out _);
                    }
                }
                catch (Exception)
                {
                    task2Ok = false;
                }
            });

            task1.Wait();
            task2.Wait();

            Assert.IsTrue(task1Ok);
            Assert.IsTrue(task2Ok);
        }
        public void CompressThreadSafe()
        {
            const int iterations     = 1000;
            const int originalLength = 100000;

            var task1Ok = true;
            var task1   = Task.Run(() =>
            {
                try
                {
                    for (var i = 0; i < iterations; i++)
                    {
                        var numBytes = originalLength;
                        var text     = Encoding.ASCII.GetBytes(TestCommon.RandomString(numBytes));
                        CachedQlz.Compress(ref text, ref numBytes);
                    }
                }
                catch (Exception)
                {
                    task1Ok = false;
                }
            });

            var task2Ok = true;
            var task2   = Task.Run(() =>
            {
                try
                {
                    for (var i = 0; i < iterations; i++)
                    {
                        var numBytes = originalLength;
                        var text     = Encoding.ASCII.GetBytes(TestCommon.RandomString(numBytes));
                        CachedQlz.Compress(ref text, ref numBytes);
                    }
                }
                catch (Exception)
                {
                    task2Ok = false;
                }
            });

            task1.Wait();
            task2.Wait();

            Assert.IsTrue(task1Ok);
            Assert.IsTrue(task2Ok);
        }
        internal override void InternalDeserialize(NetIncomingMessage lidgrenMsg)
        {
            base.InternalDeserialize(lidgrenMsg);

            SubspaceId       = lidgrenMsg.ReadInt32();
            DominantVesselId = GuidUtil.Deserialize(lidgrenMsg);
            WeakVesselId     = GuidUtil.Deserialize(lidgrenMsg);

            NumBytes = lidgrenMsg.ReadInt32();
            if (FinalVesselData.Length < NumBytes)
            {
                FinalVesselData = new byte[NumBytes];
            }

            lidgrenMsg.ReadBytes(FinalVesselData, 0, NumBytes);
            CachedQlz.Decompress(ref FinalVesselData, out NumBytes);
        }
Beispiel #20
0
        public void DecompressDataReuseArrays()
        {
            var numBytes = 4500;

            var text = Encoding.ASCII.GetBytes(TestCommon.RandomString(numBytes));

            CachedQlz.Compress(ref text, ref numBytes);
            CachedQlz.Decompress(ref text, out _);

            numBytes = 5500;

            text = Encoding.ASCII.GetBytes(TestCommon.RandomString(numBytes));
            CachedQlz.Compress(ref text, ref numBytes);

            var memBefore = GC.GetTotalMemory(true);

            CachedQlz.Decompress(ref text, out _);
            var memAfter = GC.GetTotalMemory(true);

            Assert.IsTrue(memAfter <= memBefore);
        }
        public void CompressDataReuseArrays()
        {
            var numBytes = 4500;

            //Compress a text that uses 4500 bytes
            var text = Encoding.ASCII.GetBytes(TestCommon.RandomString(numBytes));

            CachedQlz.Compress(ref text, ref numBytes);

            numBytes = 5500;

            //Now compress another text that uses 5500 bytes. As it has the same
            //"next exponential value of 2", it should reuse the array
            text = Encoding.ASCII.GetBytes(TestCommon.RandomString(numBytes));

            var memBefore = GC.GetTotalMemory(true);

            CachedQlz.Compress(ref text, ref numBytes);
            var memAfter = GC.GetTotalMemory(true);

            Assert.IsTrue(memAfter <= memBefore);
        }
Beispiel #22
0
        public void DecompressData_NoIssues()
        {
            const int originalLength = 5000;
            var       numBytes       = originalLength;

            var text = TestCommon.RandomString(numBytes);
            var data = Encoding.ASCII.GetBytes(text);

            var dataBackup = TestCommon.CloneArray(data);

            CachedQlz.Compress(ref data, ref numBytes);
            CachedQlz.Decompress(ref data, out var decompressedLength);

            Assert.AreEqual(originalLength, decompressedLength);

            var sequenceEqual = true;

            for (var i = 0; i < originalLength; i++)
            {
                sequenceEqual &= data[i] == dataBackup[i];
            }
            Assert.IsTrue(sequenceEqual);
        }
Beispiel #23
0
        public void DecompressData_ImpossibleToCompress()
        {
            const int originalLength = 100;
            var       numBytes       = originalLength;

            var data = new byte[numBytes];

            new Random().NextBytes(data);

            var dataBackup = TestCommon.CloneArray(data);

            CachedQlz.Compress(ref data, ref numBytes);
            CachedQlz.Decompress(ref data, out var decompressedLength);

            Assert.AreEqual(originalLength, decompressedLength);

            var sequenceEqual = true;

            for (var i = 0; i < originalLength; i++)
            {
                sequenceEqual &= data[i] == dataBackup[i];
            }
            Assert.IsTrue(sequenceEqual);
        }