/// <summary> /// Decode a serialized value /// </summary> object Decode(ProcedureSignature procedure, int i, Type type, ByteString value) { if (TypeUtils.IsAClassType(type)) { return(ObjectStore.Instance.GetInstance((ulong)ProtocolBuffers.ReadValue(value, typeof(ulong)))); } else if (TypeUtils.IsACollectionType(type)) { return(DecodeCollection(procedure, i, type, value)); } else if (ProtocolBuffers.IsAMessageType(type)) { return(ProtocolBuffers.ParseFrom(type, value)); } else if (TypeUtils.IsAnEnumType(type)) { // TODO: Assumes it's underlying type is int var enumValue = ProtocolBuffers.ReadValue(value, typeof(int)); if (!Enum.IsDefined(type, enumValue)) { throw new RPCException(procedure, "Failed to convert value " + enumValue + " to enumeration type " + type); } return(Enum.ToObject(type, enumValue)); } else { return(ProtocolBuffers.ReadValue(value, type)); } }
public ParameterSignature(string fullProcedureName, ProcedureParameter parameter) { Name = parameter.Name; Type = parameter.Type; // Check the parameter type is valid if (!TypeUtils.IsAValidType(Type)) { throw new ServiceException(Type + " is not a valid Procedure parameter type, in " + fullProcedureName); } // Encode the default value as a ByteString if (parameter.HasDefaultValue) { var value = parameter.DefaultValue; if (TypeUtils.IsAClassType(Type)) { DefaultArgument = ProtocolBuffers.WriteValue(ObjectStore.Instance.AddInstance(value), typeof(ulong)); } else if (TypeUtils.IsAnEnumType(Type)) { // TODO: Assumes it's underlying type is int DefaultArgument = ProtocolBuffers.WriteValue((int)value, typeof(int)); } else if (ProtocolBuffers.IsAMessageType(Type)) { DefaultArgument = ProtocolBuffers.WriteMessage(value as IMessage); } else { DefaultArgument = ProtocolBuffers.WriteValue(value, Type); } } }
public void IsAMessageType() { Assert.IsTrue(ProtocolBuffers.IsAMessageType(typeof(KRPC.Schema.KRPC.Request))); Assert.IsTrue(ProtocolBuffers.IsAMessageType(typeof(KRPC.Schema.KRPC.Response))); Assert.IsFalse(ProtocolBuffers.IsAMessageType(null)); Assert.IsFalse(ProtocolBuffers.IsAMessageType(typeof(string))); Assert.IsFalse(ProtocolBuffers.IsAMessageType(typeof(ProtocolBuffersTest))); }
/// <summary> /// Encode a value /// </summary> ByteString Encode(Type type, object value) { if (TypeUtils.IsAClassType(type)) { return(ProtocolBuffers.WriteValue(ObjectStore.Instance.AddInstance(value), typeof(ulong))); } else if (TypeUtils.IsACollectionType(type)) { return(EncodeCollection(type, value)); } else if (ProtocolBuffers.IsAMessageType(type)) { return(ProtocolBuffers.WriteMessage(value as IMessage)); } else if (TypeUtils.IsAnEnumType(type)) { // TODO: Assumes it's underlying type is int return(ProtocolBuffers.WriteValue((int)value, typeof(int))); } else { return(ProtocolBuffers.WriteValue(value, type)); } }