private void ParseIProtoResponse(int code, MessagePackObject value) { switch (code) { case (int)Key.CODE: Code = value.AsInt32(); break; case (int)Key.SYNC: Sync = value.AsInt32(); break; case (int)Key.SCHEMA_ID: SchemaId = value.AsInt32(); break; case (int)Key.DATA: Body = value.AsList().Select(i => new Tuple(i.AsList().Select(a => a.ToObject()).ToList())).ToList(); break; case (int)Key.ERROR: Error = value.AsString(); IsError = true; break; } }
private static object MapEnum(Type targetType, MessagePackObject source) { if (source.IsTypeOf <int>() ?? false) { return(source.AsInt32()); } if (source.IsTypeOf <string>() ?? false) { var strVal = source.AsString(); return(Enum.Parse(targetType, strVal, true)); } throw new MessagePackMapperException($"Cannot map value to enum {targetType.FullName}."); }
public static object Map(Type targetType, MessagePackObject source, PropertyInfo property = null) { if (source.IsNil) { return(null); } if (targetType == typeof(string)) { return(source.AsString()); } if (targetType == typeof(int) || targetType == typeof(int?)) { return(source.AsInt32()); } if (targetType == typeof(uint) || targetType == typeof(uint?)) { return(source.AsUInt32()); } if (targetType == typeof(long) || targetType == typeof(long?)) { return(source.AsInt64()); } if (targetType == typeof(ulong) || targetType == typeof(ulong?)) { return(source.AsUInt64()); } if (targetType == typeof(float) || targetType == typeof(float?)) { return(source.AsSingle()); } if (targetType == typeof(double) || targetType == typeof(double?)) { return(source.AsDouble()); } if (targetType == typeof(bool) || targetType == typeof(bool?)) { return(source.AsBoolean()); } if (targetType == typeof(byte[])) { return(source.AsBinary()); } if (targetType == typeof(byte) || targetType == typeof(byte?)) { return(source.AsByte()); } if (targetType == typeof(sbyte) || targetType == typeof(sbyte?)) { return(source.AsSByte()); } if (targetType == typeof(char[])) { return(source.AsCharArray()); } if (targetType == typeof(short) || targetType == typeof(short?)) { return(source.AsInt16()); } if (targetType == typeof(ushort) || targetType == typeof(ushort?)) { return(source.AsUInt16()); } if (targetType == typeof(DateTime) || targetType == typeof(DateTime?)) { return(MapDateTime(property, source)); } if (targetType == typeof(IList <MessagePackObject>)) { return(source.AsList()); } if (targetType == typeof(IEnumerable <MessagePackObject>)) { return(source.AsEnumerable()); } var ti = targetType.GetTypeInfo(); if (targetType == typeof(MessagePackObject)) { return(source); } if (ti.IsGenericType && (targetType.GetGenericTypeDefinition() == typeof(List <>) || targetType.GetGenericTypeDefinition() == typeof(IList <>))) { return(MapList(targetType, source.AsList())); } if (ti.IsClass && source.IsList) { return(MapClass(targetType, source)); } if (ti.IsClass && source.IsMap) { return(MapDictionary(targetType, source.AsDictionary())); } if (ti.IsEnum) { return(MapEnum(targetType, source)); } throw new MessagePackMapperException( $"Cannot find MsgPackObject converter for type {targetType.FullName}."); }
/// <summary> /// Unpacks <see cref="RpcErrorMessage"/> from stream in the specified context. /// </summary> /// <param name="context"><see cref="ClientResponseContext"/> which stores serialized error.</param> /// <returns>An unpacked <see cref="RpcErrorMessage"/>.</returns> internal static RpcErrorMessage UnpackError( ClientResponseContext context ) { Contract.Assert( context != null ); Contract.Assert( context.ErrorBuffer != null ); Contract.Assert( context.ErrorBuffer.Length > 0 ); Contract.Assert( context.ResultBuffer != null ); Contract.Assert( context.ResultBuffer.Length > 0 ); MessagePackObject error; try { error = Unpacking.UnpackObject( context.ErrorBuffer ); } catch ( UnpackException ) { error = new MessagePackObject( context.ErrorBuffer.GetBuffer().SelectMany( segment => segment.AsEnumerable() ).ToArray() ); } if ( error.IsNil ) { return RpcErrorMessage.Success; } bool isUnknown = false; RpcError errorIdentifier; if ( error.IsTypeOf<string>().GetValueOrDefault() ) { var asString = error.AsString(); errorIdentifier = RpcError.FromIdentifier( asString, null ); // Check if the error is truely Unexpected error. isUnknown = errorIdentifier.ErrorCode == RpcError.Unexpected.ErrorCode && asString != RpcError.Unexpected.Identifier; } else if ( error.IsTypeOf<int>().GetValueOrDefault() ) { errorIdentifier = RpcError.FromIdentifier( null, error.AsInt32() ); } else { errorIdentifier = RpcError.Unexpected; isUnknown = true; } MessagePackObject detail; if ( context.ResultBuffer.Length == 0 ) { detail = MessagePackObject.Nil; } else { try { detail = Unpacking.UnpackObject( context.ResultBuffer ); } catch ( UnpackException ) { detail = new MessagePackObject( context.ResultBuffer.GetBuffer().SelectMany( segment => segment.AsEnumerable() ).ToArray() ); } } if ( isUnknown ) { // Unknown error, the error should contain original Error field as message. if ( detail.IsNil ) { return new RpcErrorMessage( errorIdentifier, error.AsString(), null ); } else { var details = new MessagePackObjectDictionary( 2 ); details[ RpcException.MessageKeyUtf8 ] = error; details[ RpcException.DebugInformationKeyUtf8 ] = detail; return new RpcErrorMessage( errorIdentifier, new MessagePackObject( details, true ) ); } } else { return new RpcErrorMessage( errorIdentifier, detail ); } }
/// <summary> /// Unpacks <see cref="RpcErrorMessage"/> from stream in the specified context. /// </summary> /// <param name="context"><see cref="ClientResponseContext"/> which stores serialized error.</param> /// <returns>An unpacked <see cref="RpcErrorMessage"/>.</returns> internal static RpcErrorMessage UnpackError(ClientResponseContext context) { Contract.Assert(context != null); Contract.Assert(context.ErrorBuffer != null); Contract.Assert(context.ErrorBuffer.Length > 0); Contract.Assert(context.ResultBuffer != null); Contract.Assert(context.ResultBuffer.Length > 0); MessagePackObject error; try { error = Unpacking.UnpackObject(context.ErrorBuffer); } catch (UnpackException) { error = new MessagePackObject(context.ErrorBuffer.GetBuffer().SelectMany(segment => segment.AsEnumerable()).ToArray()); } if (error.IsNil) { return(RpcErrorMessage.Success); } bool isUnknown = false; RpcError errorIdentifier; if (error.IsTypeOf <string>().GetValueOrDefault()) { var asString = error.AsString(); errorIdentifier = RpcError.FromIdentifier(asString, null); // Check if the error is truely Unexpected error. isUnknown = errorIdentifier.ErrorCode == RpcError.Unexpected.ErrorCode && asString != RpcError.Unexpected.Identifier; } else if (error.IsTypeOf <int>().GetValueOrDefault()) { errorIdentifier = RpcError.FromIdentifier(null, error.AsInt32()); } else { errorIdentifier = RpcError.Unexpected; isUnknown = true; } MessagePackObject detail; if (context.ResultBuffer.Length == 0) { detail = MessagePackObject.Nil; } else { try { detail = Unpacking.UnpackObject(context.ResultBuffer); } catch (UnpackException) { detail = new MessagePackObject(context.ResultBuffer.GetBuffer().SelectMany(segment => segment.AsEnumerable()).ToArray()); } } if (isUnknown) { // Unknown error, the error should contain original Error field as message. if (detail.IsNil) { return(new RpcErrorMessage(errorIdentifier, error.AsString(), null)); } else { var details = new MessagePackObjectDictionary(2); details[RpcException.MessageKeyUtf8] = error; details[RpcException.DebugInformationKeyUtf8] = detail; return(new RpcErrorMessage(errorIdentifier, new MessagePackObject(details, true))); } } else { return(new RpcErrorMessage(errorIdentifier, detail)); } }