Пример #1
0
        public void TestJsonSetOptResponseWriteRead()
        {
            // Arrange
            var response = Message.Create(null, null, null, SetOptResponse.Create());
            var stream   = new MemoryStream();

            // Act
            response.Encode(stream, CodecId.Json);
            byte[] buf      = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length);
            var    returned = Serializable.Decode <Message>(new MemoryStream(buf), CodecId.Json);

            // Assert
            Assert.IsTrue(response.Equals(returned));
        }
        public override async Task <Message> ReadAsync(Reader reader,
                                                       SerializerContext context, CancellationToken ct)
        {
            var message = Message.Get();
            var members = await reader.ReadObjectHeaderAsync(ct).ConfigureAwait(false);

            if (members != 9)
            {
                throw new FormatException($"Unexpected number of properties {members}");
            }

            message.Version = await reader.ReadUInt32Async(ct).ConfigureAwait(false);

            if ((message.Version >> 16) != (VersionEx.Assembly.ToUInt() >> 16))
            {
                throw new FormatException($"Bad message version {message.Version}");
            }

            message.Source = await context.Get <Reference>().ReadAsync(
                reader, context, ct).ConfigureAwait(false);

            message.Proxy = await context.Get <Reference>().ReadAsync(
                reader, context, ct).ConfigureAwait(false);

            message.Target = await context.Get <Reference>().ReadAsync(
                reader, context, ct).ConfigureAwait(false);

            message.SequenceId = await reader.ReadUInt32Async(ct).ConfigureAwait(false);

            message.Error = await reader.ReadInt32Async(ct).ConfigureAwait(false);

            message.IsResponse = await reader.ReadBoolAsync(ct).ConfigureAwait(false);

            message.TypeId = await reader.ReadUInt32Async(ct).ConfigureAwait(false);

            if (message.IsResponse)
            {
                /**/ if (message.TypeId == MessageContent.Data)
                {
                    message.Content = await context.Get <DataMessage>().ReadAsync(
                        reader, context, ct).ConfigureAwait(false);
                }
                else if (message.TypeId == MessageContent.Ping)
                {
                    message.Content = await context.Get <PingResponse>().ReadAsync(
                        reader, context, ct).ConfigureAwait(false);
                }
                else if (message.TypeId == MessageContent.Link)
                {
                    message.Content = await context.Get <LinkResponse>().ReadAsync(
                        reader, context, ct).ConfigureAwait(false);
                }
                else if (message.TypeId == MessageContent.GetOpt)
                {
                    message.Content = await context.Get <GetOptResponse>().ReadAsync(
                        reader, context, ct).ConfigureAwait(false);
                }
                else if (message.TypeId == MessageContent.Close)
                {
                    message.Content = await context.Get <CloseResponse>().ReadAsync(
                        reader, context, ct).ConfigureAwait(false);
                }
                else
                {
                    await reader.ReadAsync(ct).ConfigureAwait(false);

                    /**/ if (message.TypeId == MessageContent.Open)
                    {
                        message.Content = OpenResponse.Create();
                    }
                    else if (message.TypeId == MessageContent.SetOpt)
                    {
                        message.Content = SetOptResponse.Create();
                    }
                    else if (message.TypeId == MessageContent.Poll)
                    {
                        message.Content = PollResponse.Create();
                    }
                    else
                    {
                        message.Content = null;
                    }
                }
            }