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);
        }
示例#3
0
        public object Desirialize(int offset, DynamicBuffer buffer, Dictionary <int, object> referenceMaping)
        {
            object result = bitSerializer.Deserialize(buffer.CurrentBuffer, offset);

            if (typeInfo.IsEnum)
            {
                return(Enum.ToObject(typeInfo.Type, (int)result));
            }
            return(result);
        }
        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);
        }
示例#5
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);
        }
 protected override ReadOnlySpan <byte> DeserializeItem(ReadOnlySpan <byte> itr, out T value)
 {
     return(BitSerializer <T> .Deserialize(itr, out value));
 }
示例#7
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);
                }
            }
        }