Exemplo n.º 1
0
        public static CacheSyncMsg CreateStoreMsg(Guid sender, string key, object data, IDictionary <string, object> parameters)
        {
            var msg = new CacheSyncMsg()
            {
                CacheKey = key,
                Action   = CacheAction.Store,
                Sender   = sender
            };

            msg.Options.Add("Parameters", parameters);

            if (data != null)
            {
                var dataType = data.GetType().AssemblyQualifiedName;
                msg.Options.Add("DataType", dataType);
                var dataStr = Convert.ToBase64String(BitSerializer.Serialize(data));
                msg.Options.Add("Data", dataStr);
            }
            else
            {
                msg.Options.Add("Data", string.Empty);
            }

            return(msg);
        }
Exemplo n.º 2
0
        private void TestValueSeralize(ValueType value)
        {
            byte[] bs = BitSerializer.Seralize(value);
            Debug.WriteLine(value + "序列化后字节数" + bs.Length);
            object newvalue = BitSerializer.Deseralize(value.GetType(), bs);

            Assert.AreEqual(value, newvalue);
        }
Exemplo n.º 3
0
        private void TestStringSeralize(string value)
        {
            byte[] bs       = BitSerializer.Seralize(value);
            object obj      = BitSerializer.Deseralize(value.GetType(), bs);
            string newvalue = obj as string;

            Assert.AreEqual(value, newvalue);
        }
        public void Precalculated()
        {
            byte[] output = new byte[_DataSerializationSize];
            BitSerializer <Struct3> .Serialize(output, _Data);

            Struct3 value;

            BitSerializer <Struct3> .Deserialize(output, out value);
        }
        public void OnTheFly()
        {
            byte[] output = new byte[_DataSerializationSize];
            BitSerializer.Serialize(output, _Data);

            Struct3 value;

            BitSerializer.Deserialize(output, out value);
        }
Exemplo n.º 6
0
        private void TestList(IList list)
        {
            byte[] bs      = BitSerializer.Seralize(list);
            IList  newlist = BitSerializer.Deseralize(list.GetType(), bs) as IList;

            for (int i = 0; i < list.Count; i++)
            {
                Assert.AreEqual(list[i], newlist[i]);
            }
        }
Exemplo n.º 7
0
        private void CheckSerializers <T>(
            T value,
            T expectedDeserializeValue,
            int expectedSerializeSize,
            int?reportedSerializeSize,
            CalculateSizeFunc <T> calculateSizeFunc,
            SerializeFunc <T> serializeFunc,
            DeserializeFunc <T> deserializeFunc
            ) where
        T : struct
        {
            if (reportedSerializeSize != null)
            {
                Assert.Equal(expectedSerializeSize, reportedSerializeSize);
            }

            Assert.Equal(expectedSerializeSize, calculateSizeFunc(value));

            byte[]      output1    = new byte[expectedSerializeSize];
            Span <byte> outputItr1 = BitSerializer.Serialize(output1, value);

            Assert.Equal(0, outputItr1.Length);

            byte[]      output2    = new byte[expectedSerializeSize];
            Span <byte> outputItr2 = BitSerializer <T> .Serialize(output2, value);

            Assert.Equal(0, outputItr2.Length);

            byte[]      output3    = new byte[expectedSerializeSize];
            Span <byte> outputItr3 = serializeFunc(output3, value);

            Assert.Equal(0, outputItr3.Length);

            Assert.Equal(output1, output2);
            Assert.Equal(output1, output3);

            T value1;
            ReadOnlySpan <byte> inputItr1 = BitSerializer.Deserialize(output1, out value1);

            Assert.Equal(0, inputItr1.Length);

            T value2;
            ReadOnlySpan <byte> inputItr2 = BitSerializer <T> .Deserialize(output2, out value2);

            Assert.Equal(0, inputItr2.Length);

            T value3;
            ReadOnlySpan <byte> inputItr3 = deserializeFunc(output3, out value3);

            Assert.Equal(0, inputItr3.Length);

            expectedDeserializeValue.ShouldCompare(value1);
            expectedDeserializeValue.ShouldCompare(value2);
            expectedDeserializeValue.ShouldCompare(value3);
        }
Exemplo n.º 8
0
        private void TestDictionary(IDictionary dic)
        {
            byte[] bs = BitSerializer.Seralize(dic);
            Dictionary <int, string> obj = BitSerializer.Deseralize(dic.GetType(), bs) as Dictionary <int, string>;

            Assert.AreEqual(obj.Count, dic.Count);
            foreach (KeyValuePair <int, string> pair in obj)
            {
                Assert.AreEqual(dic[pair.Key], pair.Value);
            }
        }
Exemplo n.º 9
0
        private void TestArray(Array array)
        {
            byte[] bs       = BitSerializer.Seralize(array);
            object obj      = BitSerializer.Deseralize(array.GetType(), bs);
            Array  newarray = obj as Array;

            Assert.AreEqual(array.Length, newarray.Length);
            for (int i = 0; i < array.Length; i++)
            {
                Assert.AreEqual(array.GetValue(i), newarray.GetValue(i));
            }
        }
Exemplo n.º 10
0
        public void SerializeTest()
        {
            var src = new LevelOne()
            {
                Dim1 = "Val",
                Dim4 = Convert.ToDateTime("2000-01-01 01:02:03.789"),
                Dim5 = new LevelTwo()
                {
                    Dim1 = new List <string>()
                    {
                        "Val1", "Val2"
                    },
                    Dim2 = new int[] { 1, 10, 100, 1000 },
                    Dim3 = new Dictionary <string, object>(),
                    Dim4 = new LevelThree()
                    {
                        Dim1 = typeof(LevelTwo),
                        Dim2 = 2000,
                        Dim3 = Guid.NewGuid(),
                        Dim4 = Convert.FromBase64String("VmFs"),
                        Dim5 = 20,
                        Dim6 = LevelEnum.Enum2
                    }
                }
            };

            var bytes = BitSerializer.Serialize(src);

            var obj  = BitSerializer.Deserialize(typeof(LevelOne).AssemblyQualifiedName, bytes);
            var dest = (LevelOne)obj;

            Assert.Equal(src.Dim1, dest.Dim1);
            Assert.Equal(src.Dim4, dest.Dim4);
            Assert.Equal(111, LevelOne.Dim7);
            Assert.Equal(src.Dim10, dest.Dim10);
            Assert.Equal(src.Dim5.Dim1, dest.Dim5.Dim1);
            Assert.Equal(src.Dim5.Dim2, dest.Dim5.Dim2);
            Assert.Equal(src.Dim5.Dim3, dest.Dim5.Dim3);
            Assert.Equal(src.Dim5.Dim4.Dim1, dest.Dim5.Dim4.Dim1);
            Assert.Equal(10000, dest.Dim5.Dim4.Dim2);
            Assert.Equal(src.Dim5.Dim4.Dim3, dest.Dim5.Dim4.Dim3);
            Assert.Equal(src.Dim5.Dim4.Dim4, dest.Dim5.Dim4.Dim4);
            Assert.Equal(src.Dim5.Dim4.Dim5, dest.Dim5.Dim4.Dim5);
            Assert.Equal(src.Dim5.Dim4.Dim6, dest.Dim5.Dim4.Dim6);
        }
Exemplo n.º 11
0
        public void TestSeralize()
        {
            Class1 class1 = new Class1();

            class1.ArrayValue = new int[] { 1, 2, 3, 4, 5 };
            class1.Class2.s   = "classsssssss";
            // class1.Dictionary[1] = class1;
            class1.b             = true;
            class1.i             = 12312;
            class1.f             = 5783f;
            class1.d             = 231f;
            class1.Dictionary[1] = "dsasdasd";
            class1.Dictionary[2] = "ddscccxz";
            // class1.listObject.Add(class1);
            class1.listValue.Add(132);
            // class1.obj = class1;
            class1.s = "class111111";

            byte[] bytes   = BitSerializer.Seralize(class1);
            Class1 class11 = BitSerializer.Deseralize(typeof(Class1), bytes) as Class1;

            Assert.IsTrue(class1.Equals(class11));
        }
Exemplo n.º 12
0
 public BaseTypeResolver(TypeInfo typeInfo)
 {
     this.typeInfo      = typeInfo;
     this.bitSerializer = BitSerializer.GetInstanse(typeInfo.Type);
 }
 protected override ReadOnlySpan <byte> DeserializeItem(ReadOnlySpan <byte> itr, out T value)
 {
     return(BitSerializer <T> .Deserialize(itr, out value));
 }
Exemplo n.º 14
0
 static BinaryBufferTest()
 {
     BitSerializer.Start();
 }
Exemplo n.º 15
0
        public void TestSeralizePerformance()
        {
            int dataCount = 1000;
            int iteration = 1000;


            Class1 class1 = new Class1();

            class1.ArrayValue = new int[] { 1, 2, 3, 4, 5 };
            class1.Class2.s   = "classsedwqdsavdddddddddddddddddsdasdasddddddddddddddddd"
                                + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
                                + "sdasddddddddddddddddd" + "asddddddddddddddd"
                                + "dasddddddddddddddddddddddxcvxcvxcvxcv"
                                + "cxvcxxxxxxxxxxxxxxxxxxxxxxxxczxcsadasdasdasdssssss";
            class1.b = true;
            class1.i = 12312;
            for (int i = 0; i < dataCount; i++)
            {
                class1.listValue.Add(i);
            }

            /*     class1.Dictionary[1] = "dsasdasd";
             *   class1.Dictionary[2] = "ddscccxz";*/
            class1.s = "192.168.1.37";

            Iterations iterations = new Iterations();

            for (int i = 0; i < iteration; i++)
            {
                iterations.class1s.Add(class1);
            }

            #region 序列化反序列化性能

            Stopwatch watch = new Stopwatch();
            byte[]    bytes = null; Iterations iterations1;
            byte[]    CompBbytes;
            {
                watch.Restart();
                bytes = BitSerializer.Seralize(iterations);
                watch.Stop();
                UnityEngine.Debug.Log("Bit seralizeTime:" + watch.ElapsedMilliseconds);
                // string s = ProtocolSerialize.DebugSeralizeStr(bytes);
                watch.Restart();
                iterations1 = BitSerializer.Deseralize(typeof(Iterations), bytes) as Iterations;
                watch.Stop();
                UnityEngine.Debug.Log("Bit DeseralizeTime:" + watch.ElapsedMilliseconds);


                #region 大小

                UnityEngine.Debug.Log("Protocol序列化:" + bytes.Length);
                CompBbytes = CompressBase.CompressBytes(bytes);
                UnityEngine.Debug.Log("压缩后:" + CompBbytes.Length);

                #endregion
            }
            {
                watch.Restart();
                ExtendProtocolSerialize.Enable();
                bytes = ProtocolSerialize.Seralize(iterations);
                watch.Stop();
                UnityEngine.Debug.Log("seralizeTime:" + watch.ElapsedMilliseconds);
                // string s = ProtocolSerialize.DebugSeralizeStr(bytes);
                watch.Restart();
                iterations1 = ProtocolSerialize.Deseralize(typeof(Iterations), bytes) as Iterations;
                watch.Stop();
                UnityEngine.Debug.Log("DeseralizeTime:" + watch.ElapsedMilliseconds);


                #region 大小

                UnityEngine.Debug.Log("Protocol序列化:" + bytes.Length);
                CompBbytes = CompressBase.CompressBytes(bytes);
                UnityEngine.Debug.Log("压缩后:" + CompBbytes.Length);

                #endregion
            }
            {
                watch.Restart();
                var json = UnityEngine.JsonUtility.ToJson(iterations);

                watch.Stop();
                UnityEngine.Debug.Log("json seralizeTime:" + watch.ElapsedMilliseconds);
                // string s = ProtocolSerialize.DebugSeralizeStr(bytes);
                watch.Restart();
                iterations1 = UnityEngine.JsonUtility.FromJson <Iterations>(json);
                watch.Stop();
                UnityEngine.Debug.Log("json DeseralizeTime:" + watch.ElapsedMilliseconds);
            }
            #endregion
        }
Exemplo n.º 16
0
        private static void Queue_Proc()
        {
            while (true)
            {
                if (_queue.TryDequeue(out var msg))
                {
                    switch (msg.Action)
                    {
                    case CacheAction.Remove:
                        if (msg.CreatedAt >= DateTime.Now.AddSeconds(-600))
                        {
                            if (msg.IsBatch)
                            {
                                if (string.IsNullOrWhiteSpace(msg.CacheKey))
                                {
                                    //全部清空
                                    CacheProvider.Provider.Clear();
                                    Debug.WriteLine("CacheRunner已全部清空缓存#" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                                }
                                else
                                {
                                    //批量清理
                                    var keys = CacheProvider.Provider.Keys(k => k.StartsWith(msg.CacheKey));
                                    Parallel.ForEach(keys, (key) => { CacheProvider.Provider.Remove(key); });

                                    Debug.WriteLine("CacheRunner批量清理:Key=" + msg.CacheKey + "#" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                                }
                            }
                            else
                            {
                                //清理单个key
                                if (CacheProvider.Provider.Contains(msg.CacheKey))
                                {
                                    CacheProvider.Provider.Remove(msg.CacheKey);
                                }

                                Debug.WriteLine("CacheRunner已删除缓存" + msg.CacheKey);
                            }
                        }
                        break;

                    case CacheAction.UpdateExpire:
                        //用于更新某个缓存的失效时间
                        var data = CacheProvider.Provider.Retrieve <object>(msg.CacheKey);
                        CacheProvider.Provider.Remove(msg.CacheKey);
                        var parms = new Dictionary <string, object>();
                        parms.Add("Duration", msg.Duration);
                        parms.Add("Preset", null);
                        CacheProvider.Provider.Store(msg.CacheKey, data, parms);

                        break;

                    case CacheAction.Store:
                        //用于同步缓存数据
                        if (msg.Options.ContainsKey("Data") &&
                            msg.Options.ContainsKey("Parameters"))
                        {
                            var parameters = msg.Options["Parameters"] as IDictionary <string, object>;
                            var dataStr    = msg.Options["Data"] as string;

                            object dataStore = null;
                            if (!string.IsNullOrEmpty(dataStr))
                            {
                                var type           = msg.Options["DataType"] as string;
                                var dataSerialized = Convert.FromBase64String(dataStr);
                                dataStore = BitSerializer.Deserialize(type, dataSerialized);
                            }

                            CacheProvider.Provider.Store(msg.CacheKey, dataStore, parameters);
                        }

                        break;

                    default:
                        Debug.WriteLine($"CacheRunner接收到未知的消息类别:{msg.Action}");
                        break;
                    }
                }
                else
                {
                    Thread.Sleep(0);
                }
            }
        }