Ejemplo n.º 1
0
        public TypeSerializer(TypeModel model, Type forType, int[] fieldNumbers, IProtoSerializer[] serializers, MethodInfo[] baseCtorCallbacks, bool isRootType, bool useConstructor, CallbackSet callbacks, Type constructType, MethodInfo factory)
        {
            Helpers.Sort(fieldNumbers, serializers);
            bool flag = false;

            for (int i = 1; i < fieldNumbers.Length; i++)
            {
                if (fieldNumbers[i] == fieldNumbers[i - 1])
                {
                    throw new InvalidOperationException("Duplicate field-number detected; " + fieldNumbers[i].ToString() + " on: " + forType.FullName);
                }
                if (!flag && serializers[i].ExpectedType != forType)
                {
                    flag = true;
                }
            }
            this.forType = forType;
            this.factory = factory;
            if (constructType == (Type)null)
            {
                constructType = forType;
            }
            else if (!forType.IsAssignableFrom(constructType))
            {
                throw new InvalidOperationException(forType.FullName + " cannot be assigned from " + constructType.FullName);
            }
            this.constructType  = constructType;
            this.serializers    = serializers;
            this.fieldNumbers   = fieldNumbers;
            this.callbacks      = callbacks;
            this.isRootType     = isRootType;
            this.useConstructor = useConstructor;
            if (baseCtorCallbacks != null && baseCtorCallbacks.Length == 0)
            {
                baseCtorCallbacks = null;
            }
            this.baseCtorCallbacks = baseCtorCallbacks;
            if (Helpers.GetUnderlyingType(forType) != (Type)null)
            {
                throw new ArgumentException("Cannot create a TypeSerializer for nullable types", "forType");
            }
            if (model.MapType(iextensible).IsAssignableFrom(forType))
            {
                if ((forType.IsValueType || !isRootType) | flag)
                {
                    throw new NotSupportedException("IExtensible is not supported in structs or classes with inheritance");
                }
                isExtensible = true;
            }
            hasConstructor = (!constructType.IsAbstract && Helpers.GetConstructor(constructType, Helpers.EmptyTypes, true) != (ConstructorInfo)null);
            if (!(constructType != forType & useConstructor))
            {
                return;
            }
            if (hasConstructor)
            {
                return;
            }
            throw new ArgumentException("The supplied default implementation cannot be created: " + constructType.FullName, "constructType");
        }
Ejemplo n.º 2
0
        private void Send(Frame frame, CallbackSet cbSet)
        {
            if (!port.IsOpen)
            {
                cbSet.ErrorHandler?.Invoke(TappyError.Hardware);
            }

            Callback flushAndSend = (byte[] data) =>
            {
                Flush();
                ackCb                = cbSet.AckHandler;
                nackCb               = cbSet.ErrorHandler;
                lcsErrorResponseCb   = cbSet.ErrorHandler;
                dcsErrorResponseCb   = cbSet.ErrorHandler;
                validResponseFrameCb = cbSet.SuccessHandler;
                appErrorCb           = cbSet.ErrorHandler;
                UnSafeSend(frame);
            };

            validResponseFrameCb = ackCb = flushAndSend;
            dcsErrorResponseCb   = lcsErrorResponseCb = nackCb = appErrorCb = (TappyError errorCode, byte[] data) =>
            {
                if (data.Length > 0 && data[0] == Driver.CMD.STOP)
                {
                    Debug.WriteLine("Unrecogized command for stop, this Tappy probably predates stop");
                }
                flushAndSend();
            };
            Stop();
        }
Ejemplo n.º 3
0
 private static void EmitInvokeCallback(CompilerContext ctx, MethodInfo method, bool copyValue, Type constructType, Type type)
 {
     if (method != null)
     {
         if (copyValue)
         {
             ctx.CopyValue();
         }
         ParameterInfo[] parameters = method.GetParameters();
         bool            flag       = true;
         for (int i = 0; i < parameters.Length; i++)
         {
             Type parameterType = parameters[0].ParameterType;
             if (parameterType == ctx.MapType(typeof(SerializationContext)))
             {
                 ctx.LoadSerializationContext();
             }
             else if (parameterType == ctx.MapType(typeof(Type)))
             {
                 Type type3 = constructType;
                 if (type3 == null)
                 {
                     type3 = type;
                 }
                 ctx.LoadValue(type3);
             }
             else if (parameterType == ctx.MapType(typeof(StreamingContext)))
             {
                 ctx.LoadSerializationContext();
                 MethodInfo info = ctx.MapType(typeof(SerializationContext)).GetMethod("op_Implicit", new Type[] { ctx.MapType(typeof(SerializationContext)) });
                 if (info != null)
                 {
                     ctx.EmitCall(info);
                     flag = true;
                 }
             }
             else
             {
                 flag = false;
             }
         }
         if (!flag)
         {
             throw CallbackSet.CreateInvalidCallbackSignature(method);
         }
         ctx.EmitCall(method);
         if ((constructType != null) && (method.ReturnType == ctx.MapType(typeof(object))))
         {
             ctx.CastFromObject(type);
         }
     }
 }
Ejemplo n.º 4
0
        private object InvokeCallback(MethodInfo method, object obj, SerializationContext context)
        {
            object obj2 = null;

            object[] objArray;
            bool     flag;

            if (method == null)
            {
                return(obj2);
            }
            ParameterInfo[] parameters = method.GetParameters();
            if (parameters.Length == 0)
            {
                objArray = null;
                flag     = true;
            }
            else
            {
                objArray = new object[parameters.Length];
                flag     = true;
                for (int i = 0; i < objArray.Length; i++)
                {
                    object constructType;
                    Type   parameterType = parameters[i].ParameterType;
                    if (parameterType == typeof(SerializationContext))
                    {
                        constructType = context;
                    }
                    else if (parameterType == typeof(Type))
                    {
                        constructType = this.constructType;
                    }
                    else if (parameterType == typeof(StreamingContext))
                    {
                        constructType = (StreamingContext)context;
                    }
                    else
                    {
                        constructType = null;
                        flag          = false;
                    }
                    objArray[i] = constructType;
                }
            }
            if (!flag)
            {
                throw CallbackSet.CreateInvalidCallbackSignature(method);
            }
            return(method.Invoke(obj, objArray));
        }
Ejemplo n.º 5
0
        public TypeSerializer(Type forType, int[] fieldNumbers, IProtoSerializer[] serializers, MethodInfo[] baseCtorCallbacks, bool isRootType, bool useConstructor, CallbackSet callbacks)
        {
            Helpers.DebugAssert(forType != null);
            Helpers.DebugAssert(fieldNumbers != null);
            Helpers.DebugAssert(serializers != null);
            Helpers.DebugAssert(fieldNumbers.Length == serializers.Length);

            Helpers.Sort(fieldNumbers, serializers);
            bool hasSubTypes = false;

            for (int i = 1; i < fieldNumbers.Length; i++)
            {
                if (fieldNumbers[i] == fieldNumbers[i - 1])
                {
                    throw new InvalidOperationException("Duplicate field-number detected; " +
                                                        fieldNumbers[i].ToString() + " on: " + forType.FullName);
                }
                if (!hasSubTypes && serializers[i].ExpectedType != forType)
                {
                    hasSubTypes = true;
                }
            }
            this.forType        = forType;
            this.serializers    = serializers;
            this.fieldNumbers   = fieldNumbers;
            this.callbacks      = callbacks;
            this.isRootType     = isRootType;
            this.useConstructor = useConstructor;

            if (baseCtorCallbacks != null && baseCtorCallbacks.Length == 0)
            {
                baseCtorCallbacks = null;
            }
            this.baseCtorCallbacks = baseCtorCallbacks;
#if !NO_GENERICS
            if (Nullable.GetUnderlyingType(forType) != null)
            {
                throw new ArgumentException("Cannot create a TypeSerializer for nullable types", "forType");
            }
#endif

            if (typeof(IExtensible).IsAssignableFrom(forType))
            {
                if (forType.IsValueType || !isRootType || hasSubTypes)
                {
                    throw new NotSupportedException("IExtensible is not supported in structs or classes with inheritance");
                }
                isExtensible = true;
            }
        }
Ejemplo n.º 6
0
        private object InvokeCallback(MethodInfo method, object obj, SerializationContext context)
        {
            object result = null;

            if (method != (MethodInfo)null)
            {
                ParameterInfo[] parameters = method.GetParameters();
                object[]        array;
                bool            flag;
                if (parameters.Length == 0)
                {
                    array = null;
                    flag  = true;
                }
                else
                {
                    array = new object[parameters.Length];
                    flag  = true;
                    for (int i = 0; i < array.Length; i++)
                    {
                        Type   parameterType = parameters[i].ParameterType;
                        object obj2;
                        if (parameterType == typeof(SerializationContext))
                        {
                            obj2 = context;
                        }
                        else if (parameterType == typeof(Type))
                        {
                            obj2 = constructType;
                        }
                        else
                        {
                            obj2 = null;
                            flag = false;
                        }
                        array[i] = obj2;
                    }
                }
                if (flag)
                {
                    result = method.Invoke(obj, array);
                    goto IL_00a1;
                }
                throw CallbackSet.CreateInvalidCallbackSignature(method);
            }
            goto IL_00a1;
IL_00a1:
            return(result);
        }
        static void ChangeCallbackSet(SerializableGuid clipGuid, Action <CallbackSet> modification)
        {
            if (!clipGuid.IsSet())
            {
                return;
            }

            CallbackSet callbackSet;

            if (!s_CallbackSetsPerClip.TryGetValue(clipGuid, out callbackSet))
            {
                callbackSet = new CallbackSet();
                s_CallbackSetsPerClip.Add(clipGuid, callbackSet);
            }

            modification?.Invoke(callbackSet);
        }
Ejemplo n.º 8
0
 internal void VerifyFactory(MethodInfo factory, Type type)
 {
     if (factory != null)
     {
         if (type != null && Helpers.IsValueType(type))
         {
             throw new InvalidOperationException();
         }
         if (!factory.IsStatic)
         {
             throw new ArgumentException("A factory-method must be static", "factory");
         }
         if (type != null && factory.ReturnType != type && factory.ReturnType != base.MapType(typeof(object)))
         {
             throw new ArgumentException("The factory-method must return object" + ((type == null) ? "" : (" or " + type.FullName)), "factory");
         }
         if (!CallbackSet.CheckCallbackParameters(this, factory))
         {
             throw new ArgumentException("Invalid factory signature in " + factory.DeclaringType.FullName + "." + factory.Name, "factory");
         }
     }
 }
Ejemplo n.º 9
0
        public TypeSerializer(TypeModel model, Type forType, int[] fieldNumbers, IProtoSerializer[] serializers, MethodInfo[] baseCtorCallbacks, bool isRootType, bool useConstructor, CallbackSet callbacks, Type constructType, MethodInfo factory)
        {
            Helpers.DebugAssert(forType != null);
            Helpers.DebugAssert(fieldNumbers != null);
            Helpers.DebugAssert(serializers != null);
            Helpers.DebugAssert(fieldNumbers.Length == serializers.Length);

            Helpers.Sort(fieldNumbers, serializers);
            bool hasSubTypes = false;

            for (int i = 1; i < fieldNumbers.Length; i++)
            {
                if (fieldNumbers[i] == fieldNumbers[i - 1])
                {
                    throw new InvalidOperationException("Duplicate field-number detected; " +
                                                        fieldNumbers[i].ToString() + " on: " + forType.FullName);
                }
                if (!hasSubTypes && serializers[i].ExpectedType != forType)
                {
                    hasSubTypes = true;
                }
            }
            this.forType = forType;
            this.factory = factory;
#if WINRT
            this.typeInfo = forType.GetTypeInfo();
#endif
            if (constructType == null)
            {
                constructType = forType;
            }
            else
            {
#if WINRT
                if (!typeInfo.IsAssignableFrom(constructType.GetTypeInfo()))
#else
                if (!forType.IsAssignableFrom(constructType))
#endif
                {
                    throw new InvalidOperationException(forType.FullName + " cannot be assigned from " + constructType.FullName);
                }
            }
            this.constructType  = constructType;
            this.serializers    = serializers;
            this.fieldNumbers   = fieldNumbers;
            this.callbacks      = callbacks;
            this.isRootType     = isRootType;
            this.useConstructor = useConstructor;

            if (baseCtorCallbacks != null && baseCtorCallbacks.Length == 0)
            {
                baseCtorCallbacks = null;
            }
            this.baseCtorCallbacks = baseCtorCallbacks;
#if !NO_GENERICS
            if (Helpers.GetUnderlyingType(forType) != null)
            {
                throw new ArgumentException("Cannot create a TypeSerializer for nullable types", "forType");
            }
#endif

#if WINRT
            if (iextensible.IsAssignableFrom(typeInfo))
            {
                if (typeInfo.IsValueType || !isRootType || hasSubTypes)
#else
            if (model.MapType(iextensible).IsAssignableFrom(forType))
            {
                if (forType.IsValueType || !isRootType || hasSubTypes)
#endif
                {
                    throw new NotSupportedException("IExtensible is not supported in structs or classes with inheritance");
                }
                isExtensible = true;
            }
#if WINRT
            TypeInfo constructTypeInfo = constructType.GetTypeInfo();
            hasConstructor = !constructTypeInfo.IsAbstract && Helpers.GetConstructor(constructTypeInfo, Helpers.EmptyTypes, true) != null;
#else
            hasConstructor = !constructType.IsAbstract && Helpers.GetConstructor(constructType, Helpers.EmptyTypes, true) != null;
#endif
            if (constructType != forType && useConstructor && !hasConstructor)
            {
                throw new ArgumentException("The supplied default implementation cannot be created: " + constructType.FullName, "constructType");
            }
        }
Ejemplo n.º 10
0
        public TypeSerializer(Type forType, int[] fieldNumbers, IProtoSerializer[] serializers, MethodInfo[] baseCtorCallbacks, bool isRootType, bool useConstructor, CallbackSet callbacks, Type constructType, MethodInfo factory)
        {
            Helpers.DebugAssert(forType != null);
            Helpers.DebugAssert(fieldNumbers != null);
            Helpers.DebugAssert(serializers != null);
            Helpers.DebugAssert(fieldNumbers.Length == serializers.Length);

            Helpers.Sort(fieldNumbers, serializers);
            bool hasSubTypes = false;

            for (int i = 0; i < fieldNumbers.Length; i++)
            {
                if (i != 0 && fieldNumbers[i] == fieldNumbers[i - 1])
                {
                    throw new InvalidOperationException("Duplicate field-number detected; " +
                                                        fieldNumbers[i].ToString() + " on: " + forType.FullName);
                }
                if (!hasSubTypes && serializers[i].ExpectedType != forType)
                {
                    hasSubTypes = true;
                }
            }
            ExpectedType = forType;
            this.factory = factory;

            if (constructType == null)
            {
                constructType = forType;
            }
            else
            {
                if (!forType.IsAssignableFrom(constructType))
                {
                    throw new InvalidOperationException(forType.FullName + " cannot be assigned from " + constructType.FullName);
                }
            }
            this.constructType  = constructType;
            this.serializers    = serializers;
            this.fieldNumbers   = fieldNumbers;
            this.callbacks      = callbacks;
            this.isRootType     = isRootType;
            this.useConstructor = useConstructor;

            if (baseCtorCallbacks != null)
            {
                foreach (var cb in baseCtorCallbacks)
                {
                    if (!cb.ReflectedType.IsAssignableFrom(forType))
                    {
                        throw new InvalidOperationException("Trying to assign incompatible callback to " + forType.FullName);
                    }
                }
                if (baseCtorCallbacks.Length == 0)
                {
                    baseCtorCallbacks = null;
                }
            }

            this.baseCtorCallbacks = baseCtorCallbacks;

            if (Helpers.GetUnderlyingType(forType) != null)
            {
                throw new ArgumentException("Cannot create a TypeSerializer for nullable types", nameof(forType));
            }
            if (iextensible.IsAssignableFrom(forType))
            {
                if (forType.IsValueType || !isRootType || hasSubTypes)
                {
                    throw new NotSupportedException("IExtensible is not supported in structs or classes with inheritance");
                }
                isExtensible = true;
            }
            hasConstructor = !constructType.IsAbstract && Helpers.GetConstructor(constructType, Helpers.EmptyTypes, true) != null;
            if (constructType != forType && useConstructor && !hasConstructor)
            {
                throw new ArgumentException("The supplied default implementation cannot be created: " + constructType.FullName, nameof(constructType));
            }
        }
Ejemplo n.º 11
0
 public TypeSerializer(TypeModel model, Type forType, long[] fieldNumbers, IProtoSerializer[] serializers, MethodInfo[] baseCtorCallbacks, bool isRootType, bool useConstructor, CallbackSet callbacks, Type constructType, MethodInfo factory)
Ejemplo n.º 12
0
 abstract internal void Init(int[] fieldNumbers, IRuntimeProtoSerializerNode[] serializers, MethodInfo[] baseCtorCallbacks, bool isRootType, bool useConstructor, bool assertKnownType, CallbackSet callbacks, Type constructType, MethodInfo factory, SerializerFeatures features);
Ejemplo n.º 13
0
        public static IProtoTypeSerializer Create(Type forType, int[] fieldNumbers, IRuntimeProtoSerializerNode[] serializers, MethodInfo[] baseCtorCallbacks, bool isRootType, bool useConstructor, bool assertKnownType, CallbackSet callbacks, Type constructType, MethodInfo factory, Type rootType, SerializerFeatures features)
        {
            var obj = (TypeSerializer)(rootType != null
                ? Activator.CreateInstance(typeof(InheritanceTypeSerializer <,>).MakeGenericType(rootType, forType), nonPublic: true)
                : Activator.CreateInstance(typeof(TypeSerializer <>).MakeGenericType(forType), nonPublic: true));

            obj.Init(fieldNumbers, serializers, baseCtorCallbacks, isRootType, useConstructor, assertKnownType, callbacks, constructType, factory, features);
            return((IProtoTypeSerializer)obj);
        }