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 VoxelFace ReadFace(MessagePackObject faceObj, out int faceI, List <Material> materials, List <Material> overlays) { VoxelFace face = new VoxelFace(); var faceList = faceObj.AsList(); if (faceList.Count >= 1) { faceI = faceList[0].AsInt32(); } else { faceI = -1; } if (faceList.Count >= 2 && faceList[1].AsInt32() != -1) { face.material = materials[faceList[1].AsInt32()]; } if (faceList.Count >= 3 && faceList[2].AsInt32() != -1) { face.overlay = overlays[faceList[2].AsInt32()]; } if (faceList.Count >= 4) { face.orientation = faceList[3].AsByte(); } return(face); }
public static Event Deserialize(List <byte[]> envelope, byte[] data) { MessagePackObject payloadData = Unpacking.UnpackObject(data).Value; IList <MessagePackObject> parts; MessagePackObjectDictionary header; if (!payloadData.IsArray || (parts = payloadData.AsList()).Count != 3) { throw new Exception("Expected array of size 3"); } if (!parts[0].IsMap || !(header = parts[0].AsDictionary()).ContainsKey("message_id")) { throw new Exception("Bad header"); } if (parts[1].AsString() == null) { throw new Exception("Bad name"); } var headerObj = new EventHeader { Version = header["v"].AsInt32(), MessageId = ProcessUuid(header, "message_id"), ResponseTo = ProcessUuid(header, "response_to") }; var msg = new Event { Envelope = envelope, Header = headerObj, Name = parts[1].AsString(), Args = parts[2].AsList() }; return(msg); }
private static void Dump(MessagePackObject obj, StringBuilder buffer, int indent) { if (obj.IsNil) { buffer.Append(' ', indent * 2).Append("(null)").AppendLine(); } else if (obj.IsTypeOf <IList <MessagePackObject> >().GetValueOrDefault()) { buffer.Append(' ', indent * 2).AppendLine("("); foreach (var child in obj.AsList()) { Dump(child, buffer, indent + 1); } buffer.Append(' ', indent * 2).AppendLine(")"); } else if (obj.IsTypeOf <IDictionary <MessagePackObject, MessagePackObject> >().GetValueOrDefault()) { buffer.Append(' ', indent * 2).AppendLine("{"); foreach (var child in obj.AsDictionary()) { Dump(child.Key, buffer, indent + 1); buffer.Append(' ', (indent + 1) * 2).AppendLine("= "); Dump(child.Value, buffer, indent + 2); } buffer.Append(' ', indent * 2).AppendLine("}"); } else { buffer.Append(' ', indent * 2).Append(obj).Append(" : ").Append(obj.UnderlyingType).AppendLine(); } }
private static object ParseResult(MessagePackObject obj) { if (obj.IsList) { List <object> data = new List <object>(); foreach (MessagePackObject objItem in obj.AsList()) { data.Add(ParseResult(objItem)); } return(data.ToArray()); } else if (obj.IsMap) { System.Collections.Hashtable data = new System.Collections.Hashtable(); foreach (var objItem in obj.AsDictionary()) { data.Add(ParseResult(objItem.Key), ParseResult(objItem.Value)); } return(data); } else { if (obj.UnderlyingType != null && resolver.ContainsKey(obj.UnderlyingType)) { return(resolver[obj.UnderlyingType](obj)); } } return(null); }
private static object MapClass(Type targetType, MessagePackObject source) { var sourceFields = source.AsList(); var target = Activator.CreateInstance(targetType); foreach (var property in targetType.GetRuntimeProperties()) { var attr = property.GetCustomAttribute <MessagePackMemberAttribute>(); if (attr != null && attr.Id < sourceFields.Count) { try { var msgPackValue = sourceFields[attr.Id]; var targetValue = Map(property.PropertyType, msgPackValue, property); property.SetValue(target, targetValue); } catch (Exception ex) { throw new MessagePackMapperException( $"Cannot map field [{property.Name}] from position [{attr.Id}]. See inner exception for details.", ex); } } } return(target); }
private static MessagePackObjectDictionary FirstDictionary(this MessagePackObject obj) { if (obj.IsList) { return(obj.AsList().First().FirstDictionary()); } return(obj.AsDictionary()); }
private static JToken CreateToken(MessagePackObject curObj) { if (curObj.IsDictionary) { JObject resultObj = new JObject(); Dictionary <string, MessagePackObject> curDict = curObj.AsDictionary("inputFile"); foreach (KeyValuePair <string, MessagePackObject> curProp in curDict) { resultObj[curProp.Key] = CreateToken(curProp.Value); } return(resultObj); } else if (curObj.IsArray) { JArray resultArray = new JArray(); IList <MessagePackObject> curArray = curObj.AsList(); foreach (MessagePackObject curElem in curArray) { resultArray.Add(CreateToken(curElem)); } return(resultArray); } else if (curObj.IsNil) { return(null); } else if (curObj.IsTypeOf <Int64>().HasValue&& curObj.IsTypeOf <Int64>().Value) { return(curObj.AsInt64()); } else if (curObj.IsTypeOf <double>().HasValue&& curObj.IsTypeOf <double>().Value) { return(curObj.AsDouble()); } else if (curObj.IsTypeOf <string>().HasValue&& curObj.IsTypeOf <string>().Value) { return(curObj.AsString()); } else if (curObj.IsTypeOf <bool>().HasValue&& curObj.IsTypeOf <bool>().Value) { return(curObj.AsBoolean()); } else { throw new Exception("Unknown Type!"); } }
private Color ReadColor(MessagePackObject o) { var l = o.AsList(); if (l.Count == 4) { return(new Color(l[0].AsSingle(), l[1].AsSingle(), l[2].AsSingle(), l[3].AsSingle())); } else { return(new Color(l[0].AsSingle(), l[1].AsSingle(), l[2].AsSingle())); } }
private void ReadEdge(MessagePackObject edgeObj, Voxel voxel) { var edgeList = edgeObj.AsList(); if (edgeList.Count == 0) { return; } int edgeI = edgeList[0].AsInt32(); if (edgeList.Count >= 2) { voxel.edges[edgeI].bevel = edgeList[1].AsByte(); } }
/// <inheritdoc /> public object Unpack(MessagePackObject obj) { if (obj.IsList) { IList <MessagePackObject> list = obj.AsList(); return(this.Unpack(list)); } if (obj.IsMap) { MessagePackObjectDictionary map = obj.AsDictionary(); return(map.ToDictionary(k => k.Key.ToString(), k => Unpack(k.Value))); } return(obj.ToObject()); }
public void TestNestedArray() { var output = new MemoryStream(); Packer.Create(output).Pack(new[] { new int[0], new[] { 0 }, new[] { 0, 1 } }); MessagePackObject obj = UnpackOne(output); var outer = obj.AsList(); Assert.AreEqual(3, outer.Count); Assert.AreEqual(0, outer[0].AsList().Count); Assert.AreEqual(1, outer[1].AsList().Count); Assert.That(outer[1].AsList()[0].AsInt32(), Is.EqualTo(0).With.TypeOf <int>()); Assert.AreEqual(2, outer[2].AsList().Count); Assert.That(outer[2].AsList()[0].AsInt32(), Is.EqualTo(0).With.TypeOf <int>()); Assert.AreEqual(1, outer[2].AsList()[1].AsInt32()); }
private void ReadVoxel(MessagePackObject voxelObj, VoxelArray voxelArray, List <Material> materials, List <Material> overlays, List <Substance> substances) { var voxelList = voxelObj.AsList(); if (voxelList.Count == 0) { return; } Vector3 position = ReadVector3(voxelList[0]); Voxel voxel = null; if (!editor) { // slightly faster -- doesn't add to octree voxel = voxelArray.InstantiateVoxel(position); } else { voxel = voxelArray.VoxelAt(position, true); } if (voxelList.Count >= 2) { foreach (var faceObj in voxelList[1].AsList()) { ReadFace(faceObj, voxel, materials, overlays); } } if (voxelList.Count >= 3 && voxelList[2].AsInt32() != -1) { voxel.substance = substances[voxelList[2].AsInt32()]; } if (voxelList.Count >= 4) { foreach (var edgeObj in voxelList[3].AsList()) { ReadEdge(edgeObj, voxel); } } voxel.UpdateVoxel(); }
private static void FormatMsgPackArray(MessagePackObject obj, IndentedStringBuilder builder) { var arr = obj.AsList(); builder.Append("[ "); foreach (var item in arr) { FormatMsgPackObj(item, builder); builder.Append(", "); } // Remove trailing comma builder.Remove(builder.Length - 2, 2); builder.Append(" ]"); }
private void ReadVoxel(MessagePackObject voxelObj, VoxelArray voxelArray, List <Material> materials, List <Material> overlays, List <Substance> substances) { var voxelList = voxelObj.AsList(); if (voxelList.Count == 0) { return; } Vector3Int position = ReadVector3Int(voxelList[0]); Voxel voxel = null; voxel = voxelArray.VoxelAt(position, true); if (voxelList.Count >= 2) { foreach (var faceObj in voxelList[1].AsList()) { VoxelFace face = ReadFace(faceObj, out int faceI, materials, overlays); if (faceI != -1) { voxel.faces[faceI] = face; } } } if (voxelList.Count >= 3 && voxelList[2].AsInt32() != -1) { voxel.substance = substances[voxelList[2].AsInt32()]; } if (voxelList.Count >= 4) { foreach (var edgeObj in voxelList[3].AsList()) { ReadEdge(edgeObj, voxel); } } voxel.UpdateVoxel(); }
public void UnpackFromMessage( MessagePackObject messagePackObject ) { if (! messagePackObject.IsTypeOf<IList<MessagePackObject>>().GetValueOrDefault() ) { throw new ArgumentException(messagePackObject.UnderlyingType.ToString()); } var asList = messagePackObject.AsList(); if ( asList.Count != 5 ) { throw new ArgumentException(); } uri = asList[ 0 ].AsString(); title = asList[ 1].AsString(); width = asList[ 2 ].AsInt32(); height = asList[ 3 ].AsInt32(); size = asList[ 4 ].AsInt32(); }
private void ReadFace(MessagePackObject faceObj, Voxel voxel, List <Material> materials, List <Material> overlays) { var faceList = faceObj.AsList(); if (faceList.Count == 0) { return; } int faceI = faceList[0].AsInt32(); if (faceList.Count >= 2 && faceList[1].AsInt32() != -1) { voxel.faces[faceI].material = materials[faceList[1].AsInt32()]; } if (faceList.Count >= 3 && faceList[2].AsInt32() != -1) { voxel.faces[faceI].overlay = overlays[faceList[2].AsInt32()]; } if (faceList.Count >= 4) { voxel.faces[faceI].orientation = faceList[3].AsByte(); } }
public static ulong TraceId(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["trace_id"].AsUInt64()); }
public void TestHeteroArray() { var heteroList = new List <MessagePackObject>() { true, false, MessagePackObject.Nil, 0, "", 123456, -123456, new String('a', 40000), new String('a', 80000), new MessagePackObject( new MessagePackObjectDictionary() { { "1", "foo" }, { 2, MessagePackObject.Nil }, { 3333333, -1 } } ), new MessagePackObject(new MessagePackObject[] { 1, 2, 3 }) }; var output = new MemoryStream(); Packer.Create(output).Pack(heteroList); MessagePackObject obj = UnpackOne(output); bool isSuccess = false; try { var list = obj.AsList(); Assert.AreEqual(heteroList[0], list[0]); Assert.AreEqual(heteroList[1], list[1]); Assert.AreEqual(heteroList[2], list[2]); Assert.AreEqual(heteroList[3], list[3]); Assert.AreEqual(heteroList[4], list[4]); Assert.AreEqual(heteroList[5], list[5]); Assert.AreEqual(heteroList[6], list[6]); Assert.AreEqual(heteroList[7], list[7]); Assert.AreEqual(heteroList[8], list[8]); Assert.AreEqual( heteroList[9].AsDictionary()["1"], list[9].AsDictionary()["1"] ); Assert.IsTrue(list[9].AsDictionary()[2].IsNil); Assert.AreEqual( heteroList[9].AsDictionary()[3333333], list[9].AsDictionary()[3333333] ); Assert.AreEqual(heteroList[10].AsList()[0], list[10].AsList()[0]); Assert.AreEqual(heteroList[10].AsList()[1], list[10].AsList()[1]); Assert.AreEqual(heteroList[10].AsList()[2], list[10].AsList()[2]); isSuccess = true; } finally { if (!isSuccess) { Console.WriteLine(Dump(obj)); } } }
public static string Error(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["error"].AsString()); }
public static Dictionary <string, string> Tags(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["meta"].AsDictionary().ToDictionary(kv => kv.Key.AsString(), kv => kv.Value.AsString())); }
private Vector3Int ReadVector3Int(MessagePackObject o) { var l = o.AsList(); return(new Vector3Int(l[0].AsInt32(), l[1].AsInt32(), l[2].AsInt32())); }
public static ulong ParentId(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["parent_id"].AsUInt64()); }
public static long StartTime(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["start"].AsInt64()); }
public static string ServiceName(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["service"].AsString()); }
public static string Type(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["type"].AsString()); }
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}."); }
private static void TestDeserializeDeviedCore( int id, object returnValue, bool isVoid, RpcException error ) { if ( isVoid ) { Assert.IsNull( returnValue, "Return value should not be specified in void." ); } if ( error != null ) { Assert.IsNull( returnValue, "Return value shoud not be specified in error." ); Assert.IsFalse( isVoid, "isVoid should be false in error test." ); } // TODO: Mock filters var objectTracingFilter = new DeserializedResponseTracingFilterProvider(); var binaryTracingFilter = new DeserializingResponseTracingFilterProvider(); var target = SerializerFactory.CreateResponseMessageSerializerWithTracer( null, null, binaryTracingFilter, objectTracingFilter ); try { var expected = new MessagePackObject( new MessagePackObject[] { new MessagePackObject( ( int )MessageType.Response ), new MessagePackObject( ( uint )id ), error == null ? MessagePackObject.Nil : error.RpcError.Identifier, returnValue == null ? ( error == null ? MessagePackObject.Nil : error.GetExceptionMessage( false ) ) : MessagePackObject.FromObject( returnValue ) } ); var stream = new MemoryStream(); Packer.Create( stream ).Pack( expected ); var serialized = stream.ToArray(); var packets = Segmentate( serialized, 10 ).ToArray(); int indexOfPackets = 0; using ( var underlying = ChunkBuffer.CreateDefault() ) { underlying.Feed( new ArraySegment<byte>( packets[ 0 ] ) ); using ( var buffer = new RpcInputBuffer<object, object>( underlying, ( _0, _1, _2 ) => ChunkBuffer.CreateDefault(), ( item, _0, _1 ) => { indexOfPackets++; if ( indexOfPackets >= packets.Length ) { return default( BufferFeeding ); //Assert.Fail( "Over requesting." ); } item.Reset(); item.Feed( new ArraySegment<byte>( packets[ indexOfPackets ] ) ); return new BufferFeeding( packets[ indexOfPackets ].Length ); }, null ) ) { ResponseMessage actual; var result = target.Deserialize( buffer, out actual ); Assert.IsTrue( result.IsSuccess, result.ToString() ); Assert.AreEqual( id, actual.MessageId ); if ( isVoid || returnValue == null ) { Assert.IsTrue( actual.ReturnValue.IsNil ); } else { Assert.AreEqual( returnValue.ToString(), actual.ReturnValue.AsString() ); } if ( error == null ) { Assert.IsNull( actual.Error ); } else { Assert.AreEqual( expected.AsList()[ 2 ].AsString(), actual.Error.RpcError.Identifier ); Assert.AreEqual( expected.AsList()[ 3 ].AsDictionary()[ MessagePackConvert.EncodeString( "ErrorCode" ) ].AsInt32(), actual.Error.RpcError.ErrorCode ); Assert.AreEqual( expected.AsList()[ 3 ].AsDictionary()[ MessagePackConvert.EncodeString( "Message" ) ].AsString(), actual.Error.Message ); if ( expected.AsList()[ 3 ].AsDictionary().ContainsKey( MessagePackConvert.EncodeString( "DebugInformation" ) ) ) { Assert.AreEqual( expected.AsList()[ 3 ].AsDictionary()[ MessagePackConvert.EncodeString( "DebugInformation" ) ].AsString(), actual.Error.DebugInformation ); } else { Assert.IsNull( actual.Error.DebugInformation ); } } } } } finally { Console.WriteLine( "BINARY TRACE:{0}", binaryTracingFilter.GetTrace() ); Console.WriteLine( "OBJECT TRACE:{0}", objectTracingFilter.GetTrace() ); } }
private static void Dump( MessagePackObject obj, StringBuilder buffer, int indent ) { if ( obj.IsNil ) { buffer.Append( ' ', indent * 2 ).Append( "(null)" ).AppendLine(); } else if ( obj.IsTypeOf<IList<MessagePackObject>>().GetValueOrDefault() ) { buffer.Append( ' ', indent * 2 ).AppendLine( "(" ); foreach ( var child in obj.AsList() ) { Dump( child, buffer, indent + 1 ); } buffer.Append( ' ', indent * 2 ).AppendLine( ")" ); } else if ( obj.IsTypeOf<IDictionary<MessagePackObject, MessagePackObject>>().GetValueOrDefault() ) { buffer.Append( ' ', indent * 2 ).AppendLine( "{" ); foreach ( var child in obj.AsDictionary() ) { Dump( child.Key, buffer, indent + 1 ); buffer.Append( ' ', ( indent + 1 ) * 2 ).AppendLine( "= " ); Dump( child.Value, buffer, indent + 2 ); } buffer.Append( ' ', indent * 2 ).AppendLine( "}" ); } else { buffer.Append( ' ', indent * 2 ).Append( obj ).Append( " : " ).Append( obj.UnderlyingType ).AppendLine(); } }
private Vector3 ReadVector3(MessagePackObject o) { var l = o.AsList(); return(new Vector3(l[0].AsSingle(), l[1].AsSingle(), l[2].AsSingle())); }
public static long Duration(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["duration"].AsInt64()); }
public static string OperationName(this MessagePackObject obj) { return(obj.AsList().First().AsDictionary()["name"].AsString()); }