本文整理汇总了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();
}
}
示例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 ];
}
}