Esempio n. 1
0
        // MessageFormat:
        // broadcast: [methodId, [argument]]
        // response:  [messageId, methodId, response]
        // error-response: [messageId, statusCode, detail, StringMessage]
        void ConsumeData(SynchronizationContext syncContext, byte[] data)
        {
            var messagePackReader = new MessagePackReader(data);
            var arrayLength       = messagePackReader.ReadArrayHeader();

            if (arrayLength == 3)
            {
                var    messageId = messagePackReader.ReadInt32();
                object future;
                if (responseFutures.TryRemove(messageId, out future))
                {
                    var methodId = messagePackReader.ReadInt32();
                    try
                    {
                        var offset = (int)messagePackReader.Consumed;
                        var rest   = new ArraySegment <byte>(data, offset, data.Length - offset);
                        OnResponseEvent(methodId, future, rest);
                    }
                    catch (Exception ex)
                    {
                        if (!(future as ITaskCompletion).TrySetException(ex))
                        {
                            throw;
                        }
                    }
                }
            }
            else if (arrayLength == 4)
            {
                var    messageId = messagePackReader.ReadInt32();
                object future;
                if (responseFutures.TryRemove(messageId, out future))
                {
                    var statusCode = messagePackReader.ReadInt32();
                    var detail     = messagePackReader.ReadString();
                    var offset     = (int)messagePackReader.Consumed;
                    var rest       = new ArraySegment <byte>(data, offset, data.Length - offset);
                    var error      = MessagePackSerializer.Deserialize <string>(rest, serializerOptions);
                    var ex         = default(RpcException);
                    if (string.IsNullOrWhiteSpace(error))
                    {
                        ex = new RpcException(new Status((StatusCode)statusCode, detail));
                    }
                    else
                    {
                        ex = new RpcException(new Status((StatusCode)statusCode, detail), detail + Environment.NewLine + error);
                    }

                    (future as ITaskCompletion).TrySetException(ex);
                }
            }
            else
            {
                var methodId = messagePackReader.ReadInt32();
                var offset   = (int)messagePackReader.Consumed;
                if (syncContext != null)
                {
                    var tuple = Tuple.Create(methodId, data, offset, data.Length - offset);
                    syncContext.Post(state =>
                    {
                        var t = (Tuple <int, byte[], int, int>)state;
                        OnBroadcastEvent(t.Item1, new ArraySegment <byte>(t.Item2, t.Item3, t.Item4));
                    }, tuple);
                }
                else
                {
                    OnBroadcastEvent(methodId, new ArraySegment <byte>(data, offset, data.Length - offset));
                }
            }
        }
 public virtual T LoadFromAsset(TextAsset textAsset)
 {
     return(MessagePackSerializer.Deserialize <T>(textAsset.bytes));
 }
Esempio n. 3
0
 public static MplsPacket FromBytes(byte[] bytes)
 {
     return(MessagePackSerializer.Deserialize <MplsPacket>(bytes));
 }
 T Convert <T>(T value)
 {
     return(MessagePackSerializer.Deserialize <T>(MessagePackSerializer.Serialize(value)));
 }
            public override void OnResponse(byte[] response)
            {
                var account = MessagePackSerializer.Deserialize <Account>(response);

                finishListener.OnFinish(account);
            }
Esempio n. 6
0
 public void FromBytes(ChatRuntime rt, byte[] bytes)
 {
     MessagePackSerializer.Deserialize <Snapshot>(bytes, ifr).Update(rt);
 }
Esempio n. 7
0
 public TOut Deserialize <TOut>(ReadOnlyMemory <byte> input)
 {
     return(MessagePackSerializer.Deserialize <TOut>(input, _options));
 }
Esempio n. 8
0
 T Convert1 <T>(T value)
 {
     return(MessagePackSerializer.Deserialize <T>(SequenceFactory.CreateSplit(MessagePackSerializer.Serialize(value), 1, 32)));
 }
 public SwimMessage Deserialize(ReadOnlySpan <byte> buffer)
 {
     return(MessagePackSerializer.Deserialize <SwimMessage>(buffer.ToArray(), this));
 }
Esempio n. 10
0
 public StaticBody(Entity entity, byte[] data) : this(entity)
 {
     List <colliderData> cols = MessagePackSerializer.Deserialize <List <colliderData> >(data);
     CreateColliders(cols);
 }
Esempio n. 11
0
 /// <summary>
 /// Loads an object asyncroniously.
 /// Handles Loading, decryption, deserialization and will invoke the given callback when done.
 /// </summary>
 /// <param name="path">Relative path to the data folder.</param>
 /// <param name="callback">Callback to execute when done.</param>
 /// <typeparam name="T">The type to serialize to.</typeparam>
 public void LoadObjectAsync <T>(string path, DynamicDataCallback <T> callback) where T : new()
 {
     LoadDataAsync(path, data => callback.Invoke(MessagePackSerializer.Deserialize <T>(data)));
 }
Esempio n. 12
0
        public static bool TryConvert(object valueToConvert, Type targetType, out object?targetValue)
        {
            if (targetType.IsInstanceOfType(valueToConvert))
            {
                targetValue = valueToConvert;
                return(true);
            }

            if (targetType.IsEnum)
            {
                if (valueToConvert is string str)
                {
                    try
                    {
                        targetValue = Enum.Parse(targetType, str, true);
                        return(true);
                    }
                    catch
                    {
                        //Ignore
                    }
                }
                else
                {
                    try
                    {
                        targetValue = Enum.ToObject(targetType, valueToConvert);
                        return(true);
                    }
                    catch
                    {
                        //Ignore
                    }
                }
            }

            if (valueToConvert is string string2 && targetType == typeof(Guid))
            {
                if (Guid.TryParse(string2, out var result))
                {
                    targetValue = result;
                    return(true);
                }
            }
            try
            {
                targetValue = Convert.ChangeType(valueToConvert, targetType);
                return(true);
            }
            catch
            {
                //Ignore
            }

            try
            {
                targetValue = MessagePackSerializer.Deserialize(targetType, MessagePackSerializer.Serialize(valueToConvert));
                return(true);
            }
            catch
            {
                //Ignore
            }

            targetValue = null;
            return(false);
        }
Esempio n. 13
0
        public void AllowPrivate()
        {
            {
                var p = new HasPrivate {
                    PublicKey = 100, PublicKeyS = "foo"
                };
                p.SetPrivate(99, "bar");

                var bin  = MessagePackSerializer.Serialize(p, StandardResolverAllowPrivate.Options);
                var json = MessagePackSerializer.ConvertToJson(bin);

                json.Is("[99,100,\"bar\",\"foo\"]");

                HasPrivate r2 = MessagePackSerializer.Deserialize <HasPrivate>(bin, StandardResolverAllowPrivate.Options);
                r2.PublicKey.Is(100);
                r2.PublicKeyS.Is("foo");
                r2.GetPrivateInt().Is(99);
                r2.GetPrivateStr().Is("bar");
            }

            {
                var p = new HasPrivateStruct {
                    PublicKey = 100, PublicKeyS = "foo"
                };
                p.SetPrivate(99, "bar");

                var bin  = MessagePackSerializer.Serialize(p, StandardResolverAllowPrivate.Options);
                var json = MessagePackSerializer.ConvertToJson(bin);

                json.Is("[99,100,\"bar\",\"foo\"]");

                HasPrivate r2 = MessagePackSerializer.Deserialize <HasPrivate>(bin, StandardResolverAllowPrivate.Options);
                r2.PublicKey.Is(100);
                r2.PublicKeyS.Is("foo");
                r2.GetPrivateInt().Is(99);
                r2.GetPrivateStr().Is("bar");
            }

            {
                var p = new HasPrivateStringKey {
                    PublicKey = 100, PublicKeyS = "foo"
                };
                p.SetPrivate(99, "bar");

                var bin  = MessagePackSerializer.Serialize(p, StandardResolverAllowPrivate.Options);
                var json = MessagePackSerializer.ConvertToJson(bin);

                json.Is("{\"PublicKey\":100,\"privateKeyS\":\"bar\",\"PublicKeyS\":\"foo\",\"privateKey\":99}");

                HasPrivateStringKey r2 = MessagePackSerializer.Deserialize <HasPrivateStringKey>(bin, StandardResolverAllowPrivate.Options);
                r2.PublicKey.Is(100);
                r2.PublicKeyS.Is("foo");
                r2.GetPrivateInt().Is(99);
                r2.GetPrivateStr().Is("bar");
            }

            {
                var p = new HasPrivateContractless {
                    PublicKey = 100, PublicKeyS = "foo"
                };
                p.SetPrivate(99, "bar");

                var bin  = MessagePackSerializer.Serialize(p, ContractlessStandardResolverAllowPrivate.Options);
                var json = MessagePackSerializer.ConvertToJson(bin);

                json.Is("{\"PublicKey\":100,\"privateKeyS\":\"bar\",\"PublicKeyS\":\"foo\",\"privateKey\":99}");

                HasPrivateContractless r2 = MessagePackSerializer.Deserialize <HasPrivateContractless>(bin, ContractlessStandardResolverAllowPrivate.Options);
                r2.PublicKey.Is(100);
                r2.PublicKeyS.Is("foo");
                r2.GetPrivateInt().Is(99);
                r2.GetPrivateStr().Is("bar");
            }
        }
Esempio n. 14
0
        public void Union()
        {
            {
                var data = new MySubUnion1 {
                    One = 23
                };
                var data2 = new MySubUnion1 {
                    One = 23
                };

                var unionData1 = MessagePackSerializer.Serialize <IUnionChecker>(data, MsgPackUnsafeDefaultResolver.Options);
                var unionData2 = MessagePackSerializer.Serialize <IUnionChecker2>(data2, MsgPackUnsafeDefaultResolver.Options);

                var reData1 = MessagePackSerializer.Deserialize <IUnionChecker>(unionData1, MsgPackUnsafeDefaultResolver.Options);
                var reData2 = MessagePackSerializer.Deserialize <IUnionChecker>(unionData1, MsgPackUnsafeDefaultResolver.Options);

                reData1.IsInstanceOf <IUnionChecker>();
                reData2.IsInstanceOf <IUnionChecker2>();

                var null1 = MessagePackSerializer.Serialize <IUnionChecker>(null, MsgPackUnsafeDefaultResolver.Options);

                var null2 = MessagePackSerializer.Serialize <IUnionChecker2>(null, MsgPackUnsafeDefaultResolver.Options);

                MessagePackSerializer.Deserialize <IUnionChecker>(null1, MsgPackUnsafeDefaultResolver.Options).IsNull();
                MessagePackSerializer.Deserialize <IUnionChecker2>(null1, MsgPackUnsafeDefaultResolver.Options).IsNull();

                var hoge = MessagePackSerializer.Serialize <IIVersioningUnion>(new VersioningUnion {
                    FV = 0
                }, MsgPackUnsafeDefaultResolver.Options);
                MessagePackSerializer.Deserialize <IUnionChecker>(hoge, MsgPackUnsafeDefaultResolver.Options).IsNull();
            }

            {
                var data = new MySubUnion2 {
                    Two = 23
                };
                var data2 = new MySubUnion2 {
                    Two = 23
                };

                var unionData1 = MessagePackSerializer.Serialize <IUnionChecker>(data, MsgPackUnsafeDefaultResolver.Options);
                var unionData2 = MessagePackSerializer.Serialize <IUnionChecker2>(data2, MsgPackUnsafeDefaultResolver.Options);

                var reData1 = MessagePackSerializer.Deserialize <IUnionChecker>(unionData1, MsgPackUnsafeDefaultResolver.Options);
                var reData2 = MessagePackSerializer.Deserialize <IUnionChecker>(unionData1, MsgPackUnsafeDefaultResolver.Options);

                reData1.IsInstanceOf <IUnionChecker>();
                reData2.IsInstanceOf <IUnionChecker2>();

                var null1 = MessagePackSerializer.Serialize <IUnionChecker>(null, MsgPackUnsafeDefaultResolver.Options);
                var null2 = MessagePackSerializer.Serialize <IUnionChecker2>(null, MsgPackUnsafeDefaultResolver.Options);

                MessagePackSerializer.Deserialize <IUnionChecker>(null1, MsgPackUnsafeDefaultResolver.Options).IsNull();
                MessagePackSerializer.Deserialize <IUnionChecker2>(null1, MsgPackUnsafeDefaultResolver.Options).IsNull();

                var hoge = MessagePackSerializer.Serialize <IIVersioningUnion>(new VersioningUnion {
                    FV = 0
                }, MsgPackUnsafeDefaultResolver.Options);
                MessagePackSerializer.Deserialize <IUnionChecker>(hoge, MsgPackUnsafeDefaultResolver.Options).IsNull();
            }
        }
        public static void SceneInfoImportHook(string path, BinaryReader br, Version version)
        {
            //Reading useless data
            br.ReadInt32();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadInt32();
            br.ReadBoolean();
            br.ReadInt32();
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadSingle();
            }
            if (version.CompareTo(new Version(0, 0, 1)) <= 0)
            {
                br.ReadBoolean();
                br.ReadSingle();
                br.ReadString();
            }

            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadBoolean();
                br.ReadString();
                br.ReadSingle();
            }

            br.ReadBoolean();
            br.ReadSingle();
            br.ReadSingle();
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadSingle();
            }
            if (version.CompareTo(new Version(0, 0, 1)) <= 0)
            {
                br.ReadBoolean();
            }
            br.ReadBoolean();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadBoolean();
            if (version.CompareTo(new Version(0, 0, 1)) <= 0)
            {
                br.ReadSingle();
            }
            br.ReadBoolean();
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadString();
                br.ReadSingle();
                br.ReadSingle();
            }

            br.ReadBoolean();
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadString();
                br.ReadString();
            }

            if (version.CompareTo(new Version(0, 0, 4)) >= 0)
            {
                br.ReadInt32();
            }
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadBoolean();
            }
            if (version.CompareTo(new Version(0, 0, 4)) >= 0)
            {
                br.ReadBoolean();
                br.ReadBoolean();
                br.ReadSingle();
                br.ReadString();
            }

            if (version.CompareTo(new Version(0, 0, 5)) >= 0)
            {
                br.ReadSingle();
                br.ReadInt32();
                br.ReadSingle();
            }

            int num = br.ReadInt32();

            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            if (num == 1)
            {
                br.ReadSingle();
            }
            else
            {
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
            }

            br.ReadSingle();
            for (int j = 0; j < 10; j++)
            {
                num = br.ReadInt32();
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
                if (num == 1)
                {
                    br.ReadSingle();
                }
                else
                {
                    br.ReadSingle();
                    br.ReadSingle();
                    br.ReadSingle();
                }

                br.ReadSingle();
            }

            br.ReadString();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadBoolean();

            br.ReadString();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadBoolean();

            br.ReadInt32();
            br.ReadInt32();
            br.ReadBoolean();

            br.ReadInt32();
            br.ReadInt32();
            br.ReadBoolean();

            br.ReadInt32();
            br.ReadString();
            br.ReadBoolean();
            br.ReadString();
            br.ReadString();
            br.ReadString();
            br.ReadBytes(16);

            ExtendedSave.internalSceneDictionary.Clear();

            try
            {
                string marker = br.ReadString();
                int    ver    = br.ReadInt32();

                int length = br.ReadInt32();

                if (marker.Equals(Marker) && length > 0)
                {
                    byte[] bytes = br.ReadBytes(length);
                    ExtendedSave.internalSceneDictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(bytes);
                }
            }
            catch (EndOfStreamException)
            {
                /* Incomplete/non-existant data */
            }
            catch (InvalidOperationException)
            {
                /* Invalid/unexpected deserialized data */
            }

            ExtendedSave.SceneImportEvent(path);
        }
        public T Deserialize <T>(byte[] content)
        {
            Ensure.Argument.NotNull(content, nameof(content));

            return(MessagePackSerializer.Deserialize <T>(content));
        }
Esempio n. 17
0
 /// <summary>
 /// Reads an <see cref="Invocation"/> from a byte array.
 /// </summary>
 /// <param name="message"></param>
 /// <returns></returns>
 public static Invocation Read(byte[] message)
 {
     return(MessagePackSerializer.Deserialize <Invocation>(message));
 }
Esempio n. 18
0
        private static void Benchmark <T>(T target)
        {
            const int Iteration = 10000;

            Console.WriteLine("Running {0} iterations...", Iteration);

            var jsonSerializer = new JsonSerializer();

            MsgPack.Serialization.SerializationContext msgpack = MsgPack.Serialization.SerializationContext.Default;
            msgpack.GetSerializer <T>().PackSingleObject(target);
            MessagePackSerializer.Serialize(target);
            MessagePackSerializer.Serialize(target, MessagePackSerializerOptions.LZ4Standard);
            ZeroFormatter.ZeroFormatterSerializer.Serialize(target);
            ProtoBuf.Serializer.Serialize(new MemoryStream(), target);
            jsonSerializer.Serialize(new JsonTextWriter(new StringWriter()), target);

            Console.WriteLine(typeof(T).Name + " serialization test");
            Console.WriteLine();

            Console.WriteLine("Serialize::");
            Deserializing = false;

            byte[] data  = null;
            var    data0 = new Nerdbank.Streams.Sequence <byte>();

            byte[] data1        = null;
            byte[] data2        = null;
            byte[] data3        = null;
            byte[] dataJson     = null;
            byte[] dataGzipJson = null;

            using (new Measure("MessagePack for C#"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    data0.Reset();
                    MessagePackSerializer.Serialize(data0, target);
                }
            }

            using (new Measure("MessagePack for C# (LZ4)"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    data3 = MessagePackSerializer.Serialize(target, MessagePackSerializerOptions.LZ4Standard);
                }
            }

            using (new Measure("MsgPack-Cli"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    data = msgpack.GetSerializer <T>().PackSingleObject(target);
                }
            }

            using (new Measure("protobuf-net"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream())
                    {
                        ProtoBuf.Serializer.Serialize(ms, target);
                    }
                }
            }

            using (new Measure("ZeroFormatter"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    data1 = ZeroFormatter.ZeroFormatterSerializer.Serialize(target);
                }
            }

            using (new Measure("Json.NET"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream())
                        using (var sw = new StreamWriter(ms, Encoding.UTF8, 1024, true))
                            using (var jw = new JsonTextWriter(sw))
                            {
                                jsonSerializer.Serialize(jw, target);
                            }
                }
            }

            using (new Measure("Json.NET(+GZip)"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream())
                        using (var gzip = new GZipStream(ms, CompressionLevel.Fastest))
                            using (var sw = new StreamWriter(gzip, Encoding.UTF8, 1024, true))
                                using (var jw = new JsonTextWriter(sw))
                                {
                                    jsonSerializer.Serialize(jw, target);
                                }
                }
            }

            using (var ms = new MemoryStream())
            {
                ProtoBuf.Serializer.Serialize(ms, target);
                data2 = ms.ToArray();
            }

            using (var ms = new MemoryStream())
            {
                using (var sw = new StreamWriter(ms, Encoding.UTF8, 1024, true))
                    using (var jw = new JsonTextWriter(sw))
                    {
                        jsonSerializer.Serialize(jw, target);
                    }

                dataJson = ms.ToArray();
            }

            using (var ms = new MemoryStream())
            {
                using (var gzip = new GZipStream(ms, CompressionLevel.Fastest))
                    using (var sw = new StreamWriter(gzip, Encoding.UTF8, 1024, true))
                        using (var jw = new JsonTextWriter(sw))
                        {
                            jsonSerializer.Serialize(jw, target);
                        }

                dataGzipJson = ms.ToArray();
            }

            msgpack.GetSerializer <T>().UnpackSingleObject(data);
            MessagePackSerializer.Deserialize <T>(data0);
            ////ZeroFormatterSerializer.Deserialize<T>(data1);
            ProtoBuf.Serializer.Deserialize <T>(new MemoryStream(data2));
            MessagePackSerializer.Deserialize <T>(data3, MessagePackSerializerOptions.LZ4Standard);
            jsonSerializer.Deserialize <T>(new JsonTextReader(new StreamReader(new MemoryStream(dataJson))));

            Console.WriteLine();
            Console.WriteLine("Deserialize::");
            Deserializing = true;

            using (new Measure("MessagePack for C#"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    MessagePackSerializer.Deserialize <T>(data0);
                }
            }

            using (new Measure("MessagePack for C# (LZ4)"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    MessagePackSerializer.Deserialize <T>(data3, MessagePackSerializerOptions.LZ4Standard);
                }
            }

            using (new Measure("MsgPack-Cli"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    msgpack.GetSerializer <T>().UnpackSingleObject(data);
                }
            }

            using (new Measure("protobuf-net"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream(data2))
                    {
                        ProtoBuf.Serializer.Deserialize <T>(ms);
                    }
                }
            }

            using (new Measure("ZeroFormatter"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    ZeroFormatterSerializer.Deserialize <T>(data1);
                }
            }

            using (new Measure("Json.NET"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream(dataJson))
                        using (var sr = new StreamReader(ms, Encoding.UTF8))
                            using (var jr = new JsonTextReader(sr))
                            {
                                jsonSerializer.Deserialize <T>(jr);
                            }
                }
            }

            using (new Measure("Json.NET(+GZip)"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream(dataGzipJson))
                        using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
                            using (var sr = new StreamReader(gzip, Encoding.UTF8))
                                using (var jr = new JsonTextReader(sr))
                                {
                                    jsonSerializer.Deserialize <T>(jr);
                                }
                }
            }

            Console.WriteLine();
            Console.WriteLine("FileSize::");
            var label = string.Empty;

            label = "MessagePack for C#";
            Console.WriteLine($"{label,-25} {data0.Length,14} byte");
            label = "MessagePack for C# (LZ4)";
            Console.WriteLine($"{label,-25} {data3.Length,14} byte");
            label = "MsgPack-Cli";
            Console.WriteLine($"{label,-25} {data.Length,14} byte");
            label = "protobuf-net";
            Console.WriteLine($"{label,-25} {data2.Length,14} byte");
            label = "ZeroFormatter";
            Console.WriteLine($"{label,-25} {data1.Length,14} byte");
            label = "Json.NET";
            Console.WriteLine($"{label,-25} {dataJson.Length,14} byte");
            label = "Json.NET(+GZip)";
            Console.WriteLine($"{label,-25} {dataGzipJson.Length,14} byte");

            Console.WriteLine();
            Console.WriteLine();
        }
Esempio n. 19
0

        
Esempio n. 20
0
#pragma warning restore DF0000 // Marks undisposed anonymous objects from object creations.
#pragma warning restore DF0001 // Marks undisposed anonymous objects from method invocations.
        // ReSharper restore ExplicitCallerInfoArgument

        /// <summary>
        /// Records/playbacks the <see cref="HttpResponseMessage" /> to specified <see cref="HttpRequestMessage" />.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="getResponseAsync">The function to call if a recording is needed.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="callerFilePath">The caller file path; set automatically.</param>
        /// <param name="callerMemberName">Name of the caller member; set automatically.</param>
        /// <param name="callerLineNumber">The caller line number; set automatically.</param>
        /// <returns>
        /// The <see cref="HttpResponseMessage" />.
        /// </returns>
        /// <exception cref="ArgumentNullException">request
        /// or
        /// request</exception>
        /// <exception cref="ArgumentOutOfRangeException">mode - null</exception>
        public async Task <HttpResponseMessage> RecordAsync(
            HttpRequestMessage request,
            GetResponseAsync getResponseAsync,
            CassetteOptions options                    = null,
            CancellationToken cancellationToken        = default(CancellationToken),
            [CallerFilePath] string callerFilePath     = "",
            [CallerMemberName] string callerMemberName = "",
            [CallerLineNumber] int callerLineNumber    = 0)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (getResponseAsync is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // Overwrite defaults with options
            options = DefaultOptions & options;
            // ReSharper disable once PossibleInvalidOperationException
            RecordMode mode = options.Mode.Value;

            // If we're in 'none' mode skip recording.
            if (mode == RecordMode.None)
            {
                return(await getResponseAsync(request, cancellationToken).ConfigureAwait(false));
            }

            // Ensure request has been completed before serialization attempts
            if (!(request.Content is null))
            {
                await request.Content.LoadIntoBufferAsync().ConfigureAwait(false);
            }

            // Get key data.
            byte[] key = await KeyGenerator.Generate(request, cancellationToken);

            // Get key data hash.
            string hash = key.GetKeyHash();

            /*
             * Lock based on hash - so only one operation is allowed for the same hash at the same time.
             */
            IDisposable @lock = await _keyedSemaphoreSlim.WaitAsync(hash, cancellationToken);

            try
            {
                // Try to get recording from the store.
                Recording           recording;
                bool                found         = false;
                HttpResponseMessage response      = null;
                byte[]              recordingData = null;

                /*
                 * Unless we're in overwrite mode, try to get a response from the store.
                 */
                if (mode != RecordMode.Overwrite)
                {
                    // Logs an error
                    // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
                    void Error(string message, Exception exception = null, bool @throw = false)
                    {
                        CassetteException cassetteException = new CassetteException(
                            message,
                            Store.Name,
                            callerFilePath,
                            callerMemberName,
                            callerLineNumber,
                            exception);

                        if (@throw)
                        {
                            Logger.LogCritical(cassetteException);
                            throw cassetteException;
                        }

                        Logger.LogError(cassetteException);
                        recording = null;
                        found     = false;
                    }

                    try
                    {
                        recordingData = await Store.GetAsync(hash, cancellationToken);
                    }
                    catch (Exception e)
                    {
                        Error("The underlying store threw an exception when attempting to retrieve a recording.", e);
                    }

                    // If we got a response and it has more than 0 bytes consider it fond!
                    if (!(recordingData is null) && recordingData.Length > 0)
                    {
                        found = true;

                        // If we're in recording mode don't bother to deserialize it as we're not going to use it
                        if (mode != RecordMode.Record)
                        {
                            // Deserialize recording
                            try
                            {
                                recording = MessagePackSerializer.Deserialize <Recording>(recordingData,
                                                                                          RecorderResolver.Instance);
                            }
                            catch (Exception e)
                            {
                                Error("The recording could not be deserialized.", e);
                            }

                            // Validate key
                            if (found && !string.Equals(hash, recording.Hash))
                            {
                                Error("The recording's hash did not match, ignoring.");
                            }
                            if (found && !string.Equals(KeyGenerator.Name, recording.KeyGeneratorName))
                            {
                                Error("The recording's key generator name did not match, ignoring.");
                            }

                            /*
                             * If we're in playback or auto mode we need to replay response
                             */
                            if (found && (mode == RecordMode.Playback || mode == RecordMode.Auto))
                            {
                                if (recording.ResponseData is null)
                                {
                                    Error("No response data found in recording, ignoring.");
                                }

                                // Deserialize response
                                if (found)
                                {
                                    try
                                    {
                                        response = MessagePackSerializer.Deserialize <HttpResponseMessage>(
                                            recording.ResponseData, RecorderResolver.Instance);
                                    }
                                    catch (Exception e)
                                    {
                                        Error("Failed to deserialize the response from the store, ignoring.", e);
                                    }
                                }

                                // ReSharper disable once PossibleInvalidOperationException
                                RequestPlaybackMode requestPlaybackMode = options.RequestPlaybackMode.Value;
                                if (found)
                                {
                                    if (recording.RequestData is null ||
                                        requestPlaybackMode == RequestPlaybackMode.IgnoreRecorded)
                                    {
                                        if (requestPlaybackMode == RequestPlaybackMode.UseRecorded)
                                        {
                                            Error(
                                                "No request found in the recording, and in RequestPlaybackMode UseRecorded.",
                                                null,
                                                true);
                                        }

                                        // ReSharper disable once PossibleNullReferenceException
                                        response.RequestMessage = request;
                                    }
                                    else
                                    {
                                        // Deserialize request
                                        try
                                        {
#pragma warning disable DF0023 // Marks undisposed objects assinged to a property, originated from a method invocation.
                                            // ReSharper disable once PossibleNullReferenceException
                                            response.RequestMessage =
                                                MessagePackSerializer.Deserialize <HttpRequestMessage>(
                                                    recording.RequestData, RecorderResolver.Instance);
#pragma warning restore DF0023 // Marks undisposed objects assinged to a property, originated from a method invocation.
                                        }
                                        catch (Exception e)
                                        {
                                            if (requestPlaybackMode == RequestPlaybackMode.UseRecorded)
                                            {
                                                Error(
                                                    "Failed to deserialize the request from the store, and in RequestPlaybackMode UseRecorded.",
                                                    e,
                                                    true);
                                            }
                                            else
                                            {
                                                Error("Failed to deserialize the request from the store, ignoring.", e);
                                            }
                                        }
                                    }
                                }

                                // ReSharper disable once PossibleInvalidOperationException
                                if (found)
                                {
                                    TimeSpan simulateDelay = options.SimulateDelay.Value;
                                    if (simulateDelay != default(TimeSpan))
                                    {
                                        int delay = simulateDelay < TimeSpan.Zero
                                            ? recording.DurationMs
                                            : (int)simulateDelay.TotalMilliseconds;

                                        Logger.LogInformation(
                                            $"Responding with matching recording from '{recording.RecordedUtc.ToLocalTime()}' after {delay}ms simulated delay.",
                                            Store.Name,
                                            callerFilePath,
                                            callerMemberName,
                                            callerLineNumber);

                                        await Task.Delay(delay, cancellationToken);
                                    }
                                    else
                                    {
                                        Logger.LogInformation(
                                            $"Responding with matching recording from '{recording.RecordedUtc.ToLocalTime()}'.",
                                            Store.Name,
                                            callerFilePath,
                                            callerMemberName,
                                            callerLineNumber);
                                    }

                                    return(response);
                                }
                            }
                        }
                    }
                }

                // If we're in playback mode we've failed to get a recording so error
                if (mode == RecordMode.Playback)
                {
                    // Recording not found so error in playback mode.
                    CassetteNotFoundException exception = new CassetteNotFoundException(
                        Store.Name,
                        callerFilePath,
                        callerMemberName,
                        callerLineNumber);
                    Logger.LogError(exception);
                    throw exception;
                }

                /*
                 * Record original request to detect changes if options set to RequestRecordMode.RecordIfChanged
                 */
                byte[] requestData;
                // ReSharper disable once PossibleInvalidOperationException
                RequestRecordMode requestRecordMode = options.RequestRecordMode.Value;
                if (!found && requestRecordMode == RequestRecordMode.RecordIfChanged)
                {
                    // If the key was generated with the FullRequestKeyGenerator.Instance then the request is already serialized.
                    if (ReferenceEquals(KeyGenerator, FullRequestKeyGenerator.Instance))
                    {
                        requestData = key;
                    }
                    else
                    {
                        try
                        {
                            requestData = MessagePackSerializer.Serialize(request, RecorderResolver.Instance);
                        }
                        catch (Exception e)
                        {
                            CassetteException ce = new CassetteException(
                                "Failed to serialize the request.",
                                Store.Name,
                                callerFilePath,
                                callerMemberName,
                                callerLineNumber,
                                e);
                            Logger.LogCritical(ce);
                            throw ce;
                        }
                    }
                }
                else
                {
                    requestData = null;
                }

                /*
                 * Retrieve response from endpoint.
                 */
                int      durationMs;
                DateTime recordedUtc;
                try
                {
                    // Use stopwatch to record how long it takes to get a response.
                    Stopwatch stopwatch = Stopwatch.StartNew();
#pragma warning disable DF0010 // Marks undisposed local variables.
                    response = await getResponseAsync(request, cancellationToken).ConfigureAwait(false);

#pragma warning restore DF0010 // Marks undisposed local variables.
                    durationMs  = (int)stopwatch.ElapsedMilliseconds;
                    recordedUtc = DateTime.UtcNow;
                }
                catch (Exception e)
                {
                    // TODO We could save the exception an repeat on playback, useful for testing handlers
                    // Unfortunately MessagePack-CSharp doesn't support exception serialization normally so would need to be
                    // handled in a custom way.
                    CassetteException re = new CassetteException("Fatal error occured retrieving the response.",
                                                                 Store.Name,
                                                                 callerFilePath,
                                                                 callerMemberName,
                                                                 callerLineNumber,
                                                                 e);
                    Logger.LogError(re);
                    throw re;
                }

                // If we have a recording, don't overwrite just return the new response here.
                if (found)
                {
                    Logger.LogInformation(
                        "Existing recording found so not overwriting it.",
                        Store.Name,
                        callerFilePath,
                        callerMemberName,
                        callerLineNumber);
                    return(response);
                }


                // Serialize response
                byte[] responseData;
                try
                {
                    responseData = MessagePackSerializer.Serialize(response, RecorderResolver.Instance);
                }
                catch (Exception e)
                {
                    CassetteException re = new CassetteException(
                        "Failed to serialize response, not storing.",
                        Store.Name,
                        callerFilePath,
                        callerMemberName,
                        callerLineNumber,
                        e);
                    Logger.LogError(re);
                    return(response);
                }


                if (requestRecordMode != RequestRecordMode.Ignore)
                {
                    byte[] oldRequestData = requestData;
                    // Serialize the request
                    try
                    {
                        requestData =
                            MessagePackSerializer.Serialize(response.RequestMessage, RecorderResolver.Instance);

                        // If we're only recording requests on change, check for changes
                        if (requestRecordMode == RequestRecordMode.RecordIfChanged &&
                            // ReSharper disable once AssignNullToNotNullAttribute
                            requestData.SequenceEqual(oldRequestData))
                        {
                            requestData = null;
                        }
                    }
                    catch (Exception e)
                    {
                        CassetteException re = new CassetteException(
                            "Failed to serialize response's request message, so ignoring check.",
                            Store.Name,
                            callerFilePath,
                            callerMemberName,
                            callerLineNumber,
                            e);
                        Logger.LogError(re);
                        requestData = null;
                    }
                }

                // Create new recording
                recording = new Recording(
                    hash,
                    KeyGenerator.Name,
                    recordedUtc,
                    durationMs,
                    responseData,
                    requestData);

                // Finally serialize the recording
                try
                {
                    recordingData = MessagePackSerializer.Serialize(recording, RecorderResolver.Instance);
                }
                catch (Exception e)
                {
                    CassetteException re = new CassetteException(
                        "Failed to serialize recording, not storing.",
                        Store.Name,
                        callerFilePath,
                        callerMemberName,
                        callerLineNumber,
                        e);
                    Logger.LogError(re);
                    return(response);
                }

                // Set the response
                Logger.LogInformation(
                    $"Recording response at '{recordedUtc.ToLocalTime()}' (took {durationMs} ms).",
                    Store.Name,
                    callerFilePath,
                    callerMemberName,
                    callerLineNumber);

                if (options.WaitForSave == true)
                {
                    try
                    {
                        await Store.StoreAsync(hash, recordingData);
                    }
                    catch (Exception e)
                    {
                        // Just log the error.
                        CassetteException re = new CassetteException(
                            "Failed to store recording.",
                            Store.Name,
                            callerFilePath,
                            callerMemberName,
                            callerLineNumber,
                            e);
                        Logger.LogError(re);
                    }

                    // We can now dispose the lock safely.
                    @lock.Dispose();
                }
                else
                {
                    // Store the recording asynchronously, and don't wait the result (errors will be logged and suppressed).
                    StoreAsync(hash, recordingData, @lock, callerFilePath, callerMemberName, callerLineNumber);
                }

                // Return the response.
                return(response);
            }
Esempio n. 21
0
 public TOut Deserialize <TOut>(string input)
 {
     return(MessagePackSerializer.Deserialize <TOut>(Encoding.UTF8.GetBytes(input), _options));
 }
Esempio n. 22
0
 private static T Deserialize <T>(byte[] buf)
 {
     return(MessagePackSerializer.Deserialize <T>(buf));
 }
Esempio n. 23
0
 /// <inheritdoc/>
 public JsonRpcMessage Deserialize(ReadOnlySequence <byte> contentBuffer) => (JsonRpcMessage)MessagePackSerializer.Deserialize <object>(contentBuffer.AsStream(), this.options);
Esempio n. 24
0
 private List <T> ReadBytes(byte[] bytes) => MessagePackSerializer.Deserialize <List <T> >(bytes);
Esempio n. 25
0
        public static async Task ReqRepTest(int LoopNum)
        {
            string requestTopic = "mytopic/A/request";
            var    sw           = new System.Diagnostics.Stopwatch();

            sw.Start();
            using (var cts = new CancellationTokenSource())
                using (var startedev = new ManualResetEventSlim())
                    using (var connectedev = new ManualResetEventSlim())
                        using (var recvev = new ManualResetEventSlim())
                        {
                            var fac = new MqttFactory();
                            await Task.WhenAll(
                                Task.Run(async() =>
                            {
                                var svr        = fac.CreateMqttServer();
                                var msgbuilder = new MqttApplicationMessageBuilder().WithPayload("req");
                                svr.ApplicationMessageReceived += async(sender, ev) =>
                                {
                                    if (ev.ApplicationMessage.Topic.Equals(requestTopic, StringComparison.Ordinal))
                                    {
                                        var reqmsg = MessagePackSerializer.Deserialize <ReqMsg>(ev.ApplicationMessage.Payload);
                                        var msg    = msgbuilder.WithTopic(reqmsg.ReplyTopic).Build();
                                        await svr.PublishAsync(msg);
                                    }
                                };
                                svr.Started += (sender, ev) =>
                                {
                                    startedev.Set();
                                };
                                var opt = new MqttServerOptionsBuilder()
                                          .WithDefaultEndpoint()
                                          .WithDefaultEndpointBoundIPAddress(IPAddress.Loopback)
                                          .WithDefaultEndpointPort(10012)
                                          .Build();
                                await svr.StartAsync(opt).ConfigureAwait(false);
                                cts.Token.WaitHandle.WaitOne();
                                await svr.StopAsync().ConfigureAwait(false);
                            })
                                ,
                                Task.Run(async() =>
                            {
                                var client = fac.CreateMqttClient();
                                string replyTopic = "mytopic/A/reply";
                                var queue = new ConcurrentQueue <TaskCompletionSource <byte[]> >();
                                client.ApplicationMessageReceived += (sender, ev) =>
                                {
                                    if (queue.TryDequeue(out var tcs))
                                    {
                                        tcs.TrySetResult(ev.ApplicationMessage.Payload);
                                    }
                                };
                                client.Connected += (sender, ev) =>
                                {
                                    connectedev.Set();
                                };
                                var clientopt = new MqttClientOptionsBuilder()
                                                .WithClientId("clid")
                                                .WithTcpServer("localhost", 10012)
                                                .Build()
                                ;
                                await client.ConnectAsync(clientopt).ConfigureAwait(false);
                                connectedev.Wait();
                                var topicFilter = new TopicFilterBuilder()
                                                  .WithTopic(replyTopic)
                                                  .WithAtLeastOnceQoS()
                                                  .Build()
                                ;
                                await client.SubscribeAsync(topicFilter).ConfigureAwait(false);
                                Console.WriteLine($"client task loop started:{sw.Elapsed}");
                                var beginTime = sw.Elapsed;
                                var msgbuilder = new MqttApplicationMessageBuilder().WithTopic(requestTopic);
                                for (int i = 0; i < LoopNum; i++)
                                {
                                    var reqpayload = MessagePackSerializer.Serialize(new ReqMsg(replyTopic, "hoge"));
                                    var msg = msgbuilder
                                              .WithPayload(reqpayload)
                                              .Build();
                                    ;
                                    var reqtcs = new TaskCompletionSource <byte[]>();
                                    queue.Enqueue(reqtcs);
                                    await client.PublishAsync(msg).ConfigureAwait(false);
                                    await reqtcs.Task;
                                }
                                var endTime = sw.Elapsed;
                                Console.WriteLine($"elapsed(mqtt):{sw.Elapsed},rps={LoopNum/(endTime.Subtract(beginTime).TotalSeconds)}");
                            }).ContinueWith(t =>
                            {
                                Console.WriteLine($"all client task done:{sw.Elapsed}");
                                cts.Cancel();
                                if (t.IsCanceled)
                                {
                                    throw new TaskCanceledException("server task cancelled", t.Exception);
                                }
                                else if (t.IsFaulted)
                                {
                                    throw new AggregateException(t.Exception);
                                }
                            })
                                ).ConfigureAwait(false);
                        }
        }
Esempio n. 26
0
        private static void Main()
        {
            var builder = new ConnectionListenerBuilder()
                          .ListenOn(new UdpEndPoint("127.0.0.1", 45685))
                          .ConfigureChannels(builder => builder.AddChannel <ReliableOrderedChannel>(0))
                          .UseLogger(PegiLogging.Configure("Server"))
            ;

            using (var listener = builder.Build())
                using (var resetEvent = new ManualResetEventSlim(false))
                {
                    Console.CancelKeyPress += (_, e) =>
                    {
                        e.Cancel = true;
                        Log.Information("Ctrl+C");
                        resetEvent.Set();
                    };

                    var connections = new ConcurrentDictionary <UdpEndPoint, Connection>();

                    listener.NewConnection += (_, connection) =>
                    {
                        Log.Information("[{ConnectionEndPoint}] New connection", connection.RemoteEndPoint);
                        connection.Disconnected += (_) =>
                        {
                            Log.Information("[{ConnectionEndPoint}] Disconnected", connection.RemoteEndPoint);
                            connections.TryRemove(connection.RemoteEndPoint, out _);
                        };
                        connection.MessageReceived += (_, data) =>
                        {
                            var message = MessagePackSerializer.Deserialize <DebugMessage>(data);
                            Log.Information("[{ConnectionEndPoint}] Message: {Message}", connection.RemoteEndPoint, message);
                            connection.SendMessage(data);
                        };
                        connections.TryAdd(connection.RemoteEndPoint, connection);
                    };
                    listener.Run();

                    var updateTime = 60;
                    while (!resetEvent.IsSet)
                    {
                        foreach (var connection in connections.Values)
                        {
                            try
                            {
                                connection.Update();
                            }
                            catch (Exception e)
                            {
                                Log.Error(e, "Update error");
                                connection.Dispose();
                            }
                        }
                        Thread.Sleep(1000 / updateTime);
                    }

                    Log.Information("Disconnecting...");
                }
            Log.Information("Disconnected");

            Thread.Sleep(1000);
        }
Esempio n. 27
0
 public static ResolveInfo Deserialize(byte[] data) => MessagePackSerializer.Deserialize <ResolveInfo>(data);
Esempio n. 28
0
        /// <summary>
        /// Read a packet after the type has been resolved
        /// </summary>
        /// <typeparam name="T">Type of packet</typeparam>
        /// <param name="reader">NetDataReader to read from</param>
        /// <returns>Packet Object</returns>
        public T read_packet <T>(NetDataReader reader)
        {
            var data = reader.GetBytesWithLength();

            return(MessagePackSerializer.Deserialize <T>(data));
        }
 public static AnimationControllerInfo Unserialize(byte[] data)
 {
     return(MessagePackSerializer.Deserialize <AnimationControllerInfo>(data));
 }
Esempio n. 30
0
 public IMessage Read(ArraySegment <byte> data)
 => MessagePackSerializer.Deserialize <T>(data);