当前位置: 首页>>代码示例>>C#>>正文


C# Unpacker.UnpackObject方法代码示例

本文整理汇总了C#中Unpacker.UnpackObject方法的典型用法代码示例。如果您正苦于以下问题:C# Unpacker.UnpackObject方法的具体用法?C# Unpacker.UnpackObject怎么用?C# Unpacker.UnpackObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Unpacker的用法示例。


在下文中一共展示了Unpacker.UnpackObject方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DeserializeCore

        private static void DeserializeCore( RequestMessageDeserializationContext context )
        {
            using ( var unpacker = new Unpacker( context.ReadBytes() ) )
            {
                var request = unpacker.UnpackObject();
                if ( request == null )
                {
                    if ( context.SerializationError.IsSuccess )
                    {
                        // Since entire stream was readed and its length was in quota, the stream may be coruppted.
                        context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Cannot deserialize message stream." ) );
                    }

                    return;
                }

                if ( !request.Value.IsTypeOf<IList<MessagePackObject>>().GetValueOrDefault() )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Request message is not array." ) );
                    return;
                }

                var requestFields = request.Value.AsList();
                if ( requestFields.Count > 4 || requestFields.Count < 3 )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Request message is not 3 nor 4 element array." ) );
                    return;
                }

                if ( !requestFields[ 0 ].IsTypeOf<int>().GetValueOrDefault() )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Message type of request message is not int 32." ) );
                    return;
                }

                int nextPosition = 1;
                switch ( ( MessageType )requestFields[ 0 ].AsInt32() )
                {
                    case MessageType.Request:
                    {
                        if ( !requestFields[ nextPosition ].IsTypeOf<uint>().GetValueOrDefault() )
                        {
                            context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Message ID of request message is not uint32." ) );
                            return;
                        }

                        // For CLS compliance store uint32 value as int32.
                        unchecked
                        {
                            context.MessageId = ( int )requestFields[ nextPosition ].AsUInt32();
                        }

                        nextPosition++;
                        break;
                    }
                    case MessageType.Notification:
                    {
                        break;
                    }
                    default:
                    {
                        context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Message type of request message is not Request(0) nor Notification(2)." ) );
                        return;
                    }
                }

                if ( !requestFields[ nextPosition ].IsTypeOf<string>().GetValueOrDefault() )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", String.Format( CultureInfo.CurrentCulture, "Method of request message (ID:{0}) is not raw. ", context.MessageId ) ) );
                    return;
                }

                try
                {
                    context.MethodName = MessagePackConvert.DecodeStringStrict( requestFields[ nextPosition ].AsBinary() );
                }
                catch ( InvalidOperationException ex )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", String.Format( CultureInfo.CurrentCulture, "Message ID:{0}: {1}", context.MessageId, ex.Message ) ) );
                    return;
                }

                nextPosition++;

                if ( !requestFields[ nextPosition ].IsTypeOf<IList<MessagePackObject>>().GetValueOrDefault() )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", String.Format( CultureInfo.CurrentCulture, "Arguments of request message (ID:{0}) is not array.", context.MessageId ) ) );
                    return;
                }

                context.Arguments = requestFields[ nextPosition ].AsList();
            }
        }
开发者ID:yfakariya,项目名称:msgpack-rpc,代码行数:93,代码来源:RequestMessageSerializer.cs

示例2: DeserializeCore

        private static void DeserializeCore( IEnumerable<byte> sequence, ResponseMessageDeserializationContext context )
        {
            using ( var unpacker = new Unpacker( sequence ) )
            {
                MessagePackObject? response = unpacker.UnpackObject();
                if ( response == null )
                {
                    if ( context.SerializationError.IsSuccess )
                    {
                        // Since entire stream was readed and its length was in quota, the stream may be coruppted.
                        context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Cannot deserialize message stream." ) );
                    }

                    return;
                }

                if ( !response.Value.IsTypeOf<IList<MessagePackObject>>().GetValueOrDefault() )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Response message is not array." ) );
                    return;
                }

                var requestFields = response.Value.AsList();
                if ( requestFields.Count != 4 )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Response message is not 4 element array." ) );
                    return;
                }

                if ( !requestFields[ 0 ].IsTypeOf<int>().GetValueOrDefault() )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Message type of response message is not int 32." ) );
                    return;
                }

                if ( requestFields[ 0 ].AsInt32() != ( int )MessageType.Response )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Message type of response message is not Response(2)." ) );
                    return;
                }

                if ( !requestFields[ 1 ].IsTypeOf<uint>().GetValueOrDefault() )
                {
                    context.SetSerializationError( new RpcErrorMessage( RpcError.MessageRefusedError, "Invalid message.", "Message ID of response message is not int32." ) );
                    return;
                }

                // For CLS compliance store uint32 value as int32.
                unchecked
                {
                    context.MessageId = ( int )requestFields[ 1 ].AsUInt32();
                }

                // Error is should be string identifier of error, but arbitary objects are supported.
                context.Error = requestFields[ 2 ];

                // If error is specified, this value should be nil by original spec, but currently should specify error information.
                context.DeserializedResult = requestFields[ 3 ];
            }
        }
开发者ID:yfakariya,项目名称:msgpack-rpc,代码行数:60,代码来源:ResponseMessageSerializer.cs


注:本文中的Unpacker.UnpackObject方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。