Ejemplo n.º 1
0
 /// <summary>
 /// 安装入口
 /// </summary>
 /// <param name="exportPathType">导出引导类型</param>
 /// <returns>是否安装成功</returns>
 public bool Run(Type exportPathType, string outputFileName)
 {
     AutoCSer.WebView.PathAttribute exportWebPath = exportPathType.customAttribute <AutoCSer.WebView.PathAttribute>();
     if (exportWebPath == null)
     {
         Messages.Message("没有找到 path 导出信息 " + exportPathType.fullName());
     }
     else if (exportWebPath.Flag == 0)
     {
         Messages.Message("缺少导出二进制位标识 " + exportPathType.fullName());
     }
     else
     {
         LeftArray <KeyValue <Type, AutoCSer.WebView.PathAttribute> > types = new LeftArray <KeyValue <Type, AutoCSer.WebView.PathAttribute> >();
         foreach (Type type in exportPathType.Assembly.GetTypes())
         {
             AutoCSer.WebView.PathAttribute webPath = type.customAttribute <AutoCSer.WebView.PathAttribute>();
             if (webPath != null && (webPath.Flag & exportWebPath.Flag) == exportWebPath.Flag)
             {
                 types.Add(new KeyValue <Type, AutoCSer.WebView.PathAttribute>(type, webPath));
             }
         }
         foreach (KeyValue <Type, AutoCSer.WebView.PathAttribute> type in types.Sort((left, right) => string.CompareOrdinal(left.Key.FullName, right.Key.FullName)))
         {
             this.Type = type.Key;
             Attribute = type.Value;
             nextCreate();
         }
         this.outputFileName = outputFileName + outputFileExtensionName;
         onCreated();
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 字段集合信息
        /// </summary>
        /// <param name="fixedFields">固定序列化字段</param>
        /// <param name="fields">非固定序列化字段</param>
        /// <param name="jsonFields">JSON 混合序列化字段</param>
        /// <param name="fixedSize">固定序列化字段字节数</param>
        /// <param name="isJson"></param>
        /// <param name="memberCountVerify">序列化成员数量</param>
        internal Fields(ref LeftArray <fieldType> fixedFields, ref LeftArray <fieldType> fields, ref LeftArray <FieldIndex> jsonFields, int fixedSize, bool isJson, out int memberCountVerify)
        {
            memberCountVerify = fixedFields.Length + fields.Length + 0x40000000;
            if (isJson || jsonFields.Length != 0)
            {
                memberCountVerify |= 0x20000000;
            }

            FixedFields = fixedFields.Sort(FieldSize.FixedSizeSort);
            FieldArray  = fields;
            JsonFields  = jsonFields;
            FixedSize   = fixedSize;
        }
Ejemplo n.º 3
0
        static TypeDeSerializer()
        {
            Type       type = typeof(valueType), attributeType;
            MethodInfo methodInfo = DeSerializer.GetDeSerializeMethod(type);

            attribute = type.customAttribute <SerializeAttribute>(out attributeType) ?? Serializer.DefaultAttribute;
            if (methodInfo != null)
            {
                DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                IsReferenceMember   = false;
                isValueType         = true;
                return;
            }
            if (type.IsArray)
            {
                isValueType = true;
                if (type.GetArrayRank() == 1)
                {
                    Type elementType = type.GetElementType();
                    if (!elementType.IsPointer && !typeof(Delegate).IsAssignableFrom(elementType))
                    {
                        if (elementType.IsValueType)
                        {
                            if (elementType.IsEnum)
                            {
                                Type enumType = System.Enum.GetUnderlyingType(elementType);
                                if (enumType == typeof(uint))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumUIntArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(byte))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumByteArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(ulong))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumULongArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(ushort))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumUShortArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(long))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumLongArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(short))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumShortArrayMethod.MakeGenericMethod(elementType);
                                }
                                else if (enumType == typeof(sbyte))
                                {
                                    methodInfo = DeSerializeMethodCache.EnumSByteArrayMethod.MakeGenericMethod(elementType);
                                }
                                else
                                {
                                    methodInfo = DeSerializeMethodCache.EnumIntArrayMethod.MakeGenericMethod(elementType);
                                }
                                IsReferenceMember = false;
                            }
                            else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                methodInfo        = DeSerializeMethodCache.NullableArrayMethod.MakeGenericMethod(elementType = elementType.GetGenericArguments()[0]);
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                            else
                            {
                                methodInfo        = DeSerializeMethodCache.StructArrayMethod.MakeGenericMethod(elementType);
                                IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                            }
                        }
                        else
                        {
                            methodInfo        = DeSerializeMethodCache.ArrayMethod.MakeGenericMethod(elementType);
                            IsReferenceMember = SerializeMethodCache.IsReferenceMember(elementType);
                        }
                        DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                        return;
                    }
                }
                DefaultDeSerializer = fromNull;
                IsReferenceMember   = false;
                return;
            }
            if (type.IsEnum)
            {
                Type enumType = System.Enum.GetUnderlyingType(type);
                if (enumType == typeof(uint))
                {
                    DefaultDeSerializer = enumUInt;
                }
                else if (enumType == typeof(byte))
                {
                    DefaultDeSerializer = enumByte;
                }
                else if (enumType == typeof(ulong))
                {
                    DefaultDeSerializer = enumULong;
                }
                else if (enumType == typeof(ushort))
                {
                    DefaultDeSerializer = enumUShort;
                }
                else if (enumType == typeof(long))
                {
                    DefaultDeSerializer = enumLong;
                }
                else if (enumType == typeof(short))
                {
                    DefaultDeSerializer = enumShort;
                }
                else if (enumType == typeof(sbyte))
                {
                    DefaultDeSerializer = enumSByte;
                }
                else
                {
                    DefaultDeSerializer = enumInt;
                }
                IsReferenceMember = false;
                isValueType       = true;
                return;
            }
            if (type.IsPointer || typeof(Delegate).IsAssignableFrom(type))
            {
                DefaultDeSerializer = fromNull;
                IsReferenceMember   = false;
                isValueType         = true;
                return;
            }
            if (type.IsGenericType)
            {
                Type   genericType    = type.GetGenericTypeDefinition();
                Type[] parameterTypes = type.GetGenericArguments();
                if (genericType == typeof(LeftArray <>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.LeftArrayDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
#if !Serialize
                if (genericType == typeof(SubArray <>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.SubArrayDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
#endif
                if (genericType == typeof(Dictionary <,>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.DictionaryDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(Nullable <>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.NullableDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(KeyValuePair <,>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.KeyValuePairDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(SortedDictionary <,>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.SortedDictionaryDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
                if (genericType == typeof(SortedList <,>))
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), DeSerializeMethodCache.SortedListDeSerializeMethod.MakeGenericMethod(type.GetGenericArguments()));
                    IsReferenceMember   = SerializeMethodCache.IsReferenceMember(parameterTypes[0]) || SerializeMethodCache.IsReferenceMember(parameterTypes[1]);
                    isValueType         = true;
                    return;
                }
            }
            if ((methodInfo = SerializeMethodCache.GetCustom(type, false)) != null)
            {
                if (type.IsValueType)
                {
#if NOJIT
                    DefaultDeSerializer = new CustomDeSerializer(methodInfo).DeSerialize;
#else
                    DynamicMethod dynamicMethod = new DynamicMethod("CustomDeSerializer", null, new Type[] { typeof(DeSerializer), type.MakeByRefType() }, type, true);
                    ILGenerator   generator     = dynamicMethod.GetILGenerator();
                    generator.Emit(OpCodes.Ldarg_1);
                    generator.Emit(OpCodes.Ldarg_0);
                    generator.call(methodInfo);
                    generator.Emit(OpCodes.Ret);
                    DefaultDeSerializer = (deSerialize)dynamicMethod.CreateDelegate(typeof(deSerialize));
#endif
                }
                else
                {
                    DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                }
                IsReferenceMember = attribute.IsReferenceMember;
                isValueType       = true;
                return;
            }
            if (type.IsAbstract || type.IsInterface || Emit.Constructor <valueType> .New == null)
            {
                DefaultDeSerializer = noConstructor;
                isValueType         = IsReferenceMember = true;
                return;
            }
            IsReferenceMember = attribute.IsReferenceMember;
            foreach (Type interfaceType in type.GetInterfaces())
            {
                if (interfaceType.IsGenericType)
                {
                    Type genericType = interfaceType.GetGenericTypeDefinition();
                    if (genericType == typeof(ICollection <>))
                    {
                        Type[] parameters   = interfaceType.GetGenericArguments();
                        Type   argumentType = parameters[0];
                        parameters[0] = argumentType.MakeArrayType();
                        ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            break;
                        }
                        parameters[0]   = typeof(IList <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            break;
                        }
                        parameters[0]   = typeof(ICollection <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            break;
                        }
                        parameters[0]   = typeof(IEnumerable <>).MakeGenericType(argumentType);
                        constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null);
                        if (constructorInfo != null)
                        {
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructCollectionMethod : DeSerializeMethodCache.ClassCollectionMethod).MakeGenericMethod(type, argumentType);
                            break;
                        }
                    }
                    else if (genericType == typeof(IDictionary <,>))
                    {
                        ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { interfaceType }, null);
                        if (constructorInfo != null)
                        {
                            Type[] parameters = interfaceType.GetGenericArguments();
                            methodInfo = (type.IsValueType ? DeSerializeMethodCache.StructDictionaryDeSerializeMethod : DeSerializeMethodCache.ClassDictionaryDeSerializeMethod).MakeGenericMethod(type, parameters[0], parameters[1]);
                            break;
                        }
                    }
                }
            }
            if (methodInfo != null)
            {
                DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                return;
            }
            if (type.IsValueType)
            {
                isValueType = true;
            }
            else if (attribute != Serializer.DefaultAttribute && attributeType != type)
            {
                for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType)
                {
                    SerializeAttribute baseAttribute = baseType.customAttribute <SerializeAttribute>();
                    if (baseAttribute != null)
                    {
                        if (baseAttribute.IsBaseType)
                        {
                            methodInfo          = DeSerializeMethodCache.BaseSerializeMethod.MakeGenericMethod(baseType, type);
                            DefaultDeSerializer = (deSerialize)Delegate.CreateDelegate(typeof(deSerialize), methodInfo);
                            return;
                        }
                        break;
                    }
                }
            }

            LeftArray <DeSerializeVersionField> attributeFields = new LeftArray <DeSerializeVersionField>();
            if ((attribute.MemberFilters & MemberFilters.PublicInstanceField) != 0)
            {
                appendField(ref attributeFields, MemberIndexGroup <valueType> .Group.PublicFields);
            }
            else
            {
                foreach (FieldIndex field in MemberIndexGroup <valueType> .Group.PublicFields)
                {
                    attributeFields.Add(new DeSerializeVersionField {
                        Field = field
                    });
                }
            }
            if ((attribute.MemberFilters & MemberFilters.NonPublicInstanceField) != 0)
            {
                appendField(ref attributeFields, MemberIndexGroup <valueType> .Group.NonPublicFields);
            }
            if (attribute.IsAnonymousFields)
            {
                appendField(ref attributeFields, MemberIndexGroup <valueType> .Group.AnonymousFields);
            }
            foreach (FieldIndex field in new MemberIndexGroup(type, true).NonPublicFields)
            {
                Type fieldType = field.Member.FieldType;
                if (!fieldType.IsPointer && (!fieldType.IsArray || fieldType.GetArrayRank() == 1) && !field.IsIgnore && !typeof(Delegate).IsAssignableFrom(fieldType))
                {
                    SerializeMemberAttribute memberAttribute = field.GetAttribute <SerializeMemberAttribute>(true);
                    if (memberAttribute != null && memberAttribute.IsSetup && memberAttribute.RemoveGlobalVersion != memberAttribute.GlobalVersion)
                    {
                        attributeFields.Add(new DeSerializeVersionField {
                            Field = field, Attribute = memberAttribute
                        });
                        if (memberAttribute.IsRemove)
                        {
                            attributeFields.Add(new DeSerializeVersionField {
                                Field = field, Attribute = memberAttribute, IsRemove = true
                            });
                        }
                    }
                }
            }

            isMemberMap = attribute.GetIsMemberMap;
            isJson      = attribute.GetIsJson;
            uint globalVersion = 0, removeMemberCount = 0;
            int  noSerializeMemberCount = 0;
            LeftArray <DeSerializeVersionFields <valueType> > deSerializeVersionFields = default(LeftArray <DeSerializeVersionFields <valueType> >);
            LeftArray <DeSerializeVersionField> attributeVersionFields = new LeftArray <DeSerializeVersionField>(attributeFields.Length);
            foreach (DeSerializeVersionField field in attributeFields.Sort(DeSerializeVersionField.GlobalVersionSort))
            {
                if (field.GlobalVersion != globalVersion)
                {
                    deSerializeVersionFields.Add(new DeSerializeVersionFields <valueType>(globalVersion, attributeVersionFields.GetArray(), removeMemberCount, noSerializeMemberCount));
                    globalVersion = field.GlobalVersion;
                }
                if (field.IsRemove)
                {
                    attributeVersionFields.RemoveToEnd(value => object.ReferenceEquals(value.Field, field.Field));
                    if (field.Attribute.IsRemove)
                    {
                        --removeMemberCount;
                    }
                }
                else
                {
                    attributeVersionFields.Add(field);
                    if (field.Attribute != null)
                    {
                        if (field.Attribute.IsRemove)
                        {
                            ++removeMemberCount;
                        }
                    }
                    else
                    {
                        ++noSerializeMemberCount;
                    }
                }
            }
            fieldDeSerializer = new DeSerializeVersionFields <valueType>(globalVersion, attributeVersionFields.GetArray(), removeMemberCount, noSerializeMemberCount).CreateOnly(attribute);
            if (deSerializeVersionFields.Length != 0)
            {
                int count = deSerializeVersionFields.Length;
                DeSerializeVersionFields <valueType>[] deSerializeVersionFieldsArray = new DeSerializeVersionFields <valueType> [count];
                foreach (DeSerializeVersionFields <valueType> value in deSerializeVersionFields)
                {
                    deSerializeVersionFieldsArray[--count]          = value;
                    deSerializeVersionFieldsArray[count].CreateLock = new object();
                }
                fieldDeSerializers = deSerializeVersionFieldsArray;
            }
        }
Ejemplo n.º 4
0
            /// <summary>
            /// 获取 TCP 服务函数信息
            /// </summary>
            /// <param name="type"></param>
            /// <returns></returns>
            internal bool Get(Type type)
            {
                if (type.IsInterface)
                {
                    if (type.IsGenericTypeDefinition)
                    {
                        ErrorString = "不支持泛型接口 " + type.fullName();
                        return(false);
                    }
                    //if (type.GetInterfaces().length() != 0)
                    //{
                    //    ErrorString = "不支持接口继承 " + type.fullName();
                    //    return false;
                    //}
                    foreach (PropertyInfo property in type.GetProperties())
                    {
                        ErrorString = "不支持属性 " + type.fullName();
                        return(false);
                    }
                    attributeType serverAttribute = null;
                    foreach (attributeType attribute in type.GetCustomAttributes(typeof(attributeType), false))
                    {
                        serverAttribute = attribute;
                        break;
                    }
                    if (serverAttribute == null)
                    {
                        serverAttribute = AutoCSer.Emit.Constructor <attributeType> .New();
                    }
                    methodAttributeType methodAttribute = null;
                    foreach (methodAttributeType attribute in type.GetCustomAttributes(typeof(methodAttributeType), false))
                    {
                        methodAttribute = attribute;
                        break;
                    }
                    if (methodAttribute == null)
                    {
                        methodAttribute = AutoCSer.Emit.Constructor <methodAttributeType> .New();
                    }

                    LeftArray <methodType> tcpMethods = new LeftArray <methodType>();
                    if (!get(type, serverAttribute, methodAttribute, true, ref tcpMethods))
                    {
                        return(false);
                    }
                    foreach (Type interfaceType in type.GetInterfaces().notNull())
                    {
                        if (!get(interfaceType, serverAttribute, methodAttribute, true, ref tcpMethods))
                        {
                            return(false);
                        }
                    }
                    //MethodInfo[] typeMethods = type.GetMethods();
                    //Methods = new methodType[typeMethods.Length];
                    //int tcpMethodIndex = 0;
                    //foreach (MethodInfo method in typeMethods)
                    //{
                    //    if (method.IsGenericMethod)
                    //    {
                    //        ErrorString = "不支持泛型函数 " + type.fullName() + "." + method.Name;
                    //        return false;
                    //    }
                    //    Methods[tcpMethodIndex++] = new methodType(method, serverAttribute, servermethodAttributeType, isClient);
                    //}
                    //LeftArray<methodType> tcpMethods = new LeftArray<methodType> { Array = Methods, Length = tcpMethodIndex };
                    //foreach (PropertyInfo property in type.GetProperties())
                    //{
                    //    if (property.CanRead)
                    //    {
                    //        methodType getMethod = new methodType(property, serverAttribute, servermethodAttributeType);
                    //        tcpMethods.Add(getMethod);
                    //        if (getMethod.PropertySetMethod != null) tcpMethods.Add(getMethod.PropertySetMethod);
                    //    }
                    //    else tcpMethods.Add(new methodType(property, serverAttribute, servermethodAttributeType, true));
                    //}
                    if (tcpMethods.Length == 0)
                    {
                        ErrorString = type.fullName() + " 没有找到函数定义";
                        return(false);
                    }
                    foreach (methodType method in tcpMethods)
                    {
                        if (!method.CheckRef(ref ErrorString))
                        {
                            return(false);
                        }
                    }
                    tcpMethods.Sort(Compare);
                    if ((Methods = CheckIdentity(ref tcpMethods, ref ErrorString)) == null)
                    {
                        return(false);
                    }
                    bool isVerifyMethod = false;
                    foreach (methodType method in Methods)
                    {
                        if (method != null)
                        {
                            if (isVerifyMethod)
                            {
                                method.Attribute.IsVerifyMethod = false;
                            }
                            else if (method.CheckIsVerifyMethod(ref ErrorString))
                            {
                                isVerifyMethod = true;
                                if (!method.IsVerifyMethodAsynchronous)
                                {
                                    method.Attribute.ServerTaskType = Net.TcpServer.ServerTaskType.Synchronous;
                                }
                            }
                            else if (ErrorString != null)
                            {
                                return(false);
                            }
                        }
                    }
                    foreach (methodType method in Methods)
                    {
                        if (method != null)
                        {
                            method.SetParameterType();
                        }
                    }
                    string serviceName = serverAttribute.ServerName ?? type.fullName();
                    DefaultServerAttribute = (attributeType)AutoCSer.Config.Loader.GetObject(typeof(attributeType), serviceName) ?? AutoCSer.MemberCopy.Copyer <attributeType> .MemberwiseClone(serverAttribute);

                    if (DefaultServerAttribute.Name == null)
                    {
                        DefaultServerAttribute.Name = serviceName;
                    }
                    return(true);
                }
                ErrorString = type.fullName() + " 不是接口类型";
                return(false);
            }
Ejemplo n.º 5
0
            /// <summary>
            /// 获取 TCP 服务函数信息
            /// </summary>
            /// <param name="type"></param>
            /// <returns></returns>
            internal bool Get(Type type)
            {
                if (type.IsInterface)
                {
                    if (type.IsGenericTypeDefinition)
                    {
                        ErrorString = "不支持泛型接口 " + type.fullName();
                        return(false);
                    }
                    //if (type.GetInterfaces().length() != 0)
                    //{
                    //    ErrorString = "不支持接口继承 " + type.fullName();
                    //    return false;
                    //}
                    foreach (PropertyInfo property in type.GetProperties())
                    {
                        ErrorString = "不支持属性 " + type.fullName();
                        return(false);
                    }
                    attributeType serverAttribute = null;
                    foreach (attributeType attribute in type.GetCustomAttributes(typeof(attributeType), false))
                    {
                        serverAttribute = attribute;
                        break;
                    }
                    if (serverAttribute == null)
                    {
                        serverAttribute = AutoCSer.Metadata.DefaultConstructor <attributeType> .Constructor();
                    }
                    methodAttributeType methodAttribute = null;
                    foreach (methodAttributeType attribute in type.GetCustomAttributes(typeof(methodAttributeType), false))
                    {
                        methodAttribute = attribute;
                        break;
                    }
                    if (methodAttribute == null)
                    {
                        methodAttribute = AutoCSer.Metadata.DefaultConstructor <methodAttributeType> .Constructor();

                        methodAttribute.IsDefault = true;
                    }

                    LeftArray <methodType> tcpMethods = new LeftArray <methodType>(0);
                    if (!get(type, serverAttribute, methodAttribute, true, ref tcpMethods))
                    {
                        return(false);
                    }
                    foreach (Type interfaceType in type.GetInterfaces().notNull())
                    {
                        if (!get(interfaceType, serverAttribute, methodAttribute, true, ref tcpMethods))
                        {
                            return(false);
                        }
                    }
                    //MethodInfo[] typeMethods = type.GetMethods();
                    //Methods = new methodType[typeMethods.Length];
                    //int tcpMethodIndex = 0;
                    //foreach (MethodInfo method in typeMethods)
                    //{
                    //    if (method.IsGenericMethod)
                    //    {
                    //        ErrorString = "不支持泛型函数 " + type.fullName() + "." + method.Name;
                    //        return false;
                    //    }
                    //    Methods[tcpMethodIndex++] = new methodType(method, serverAttribute, servermethodAttributeType, isClient);
                    //}
                    //LeftArray<methodType> tcpMethods = new LeftArray<methodType> { Array = Methods, Length = tcpMethodIndex };
                    //foreach (PropertyInfo property in type.GetProperties())
                    //{
                    //    if (property.CanRead)
                    //    {
                    //        methodType getMethod = new methodType(property, serverAttribute, servermethodAttributeType);
                    //        tcpMethods.Add(getMethod);
                    //        if (getMethod.PropertySetMethod != null) tcpMethods.Add(getMethod.PropertySetMethod);
                    //    }
                    //    else tcpMethods.Add(new methodType(property, serverAttribute, servermethodAttributeType, true));
                    //}
                    if (tcpMethods.Length == 0)
                    {
                        ErrorString = type.fullName() + " 没有找到函数定义";
                        return(false);
                    }
                    foreach (methodType method in tcpMethods)
                    {
                        if (!method.CheckRef(ref ErrorString))
                        {
                            return(false);
                        }
                    }
                    tcpMethods.Sort(Compare);
                    if ((Methods = CheckIdentity(ref tcpMethods, ref ErrorString)) == null)
                    {
                        return(false);
                    }
                    bool isVerifyMethod = false;
                    foreach (methodType method in Methods)
                    {
                        if (method != null)
                        {
                            if (isVerifyMethod)
                            {
                                method.Attribute.IsVerifyMethod = false;
                            }
                            else if (method.CheckIsVerifyMethod(ref ErrorString))
                            {
                                isVerifyMethod = true;
                                //if (!method.IsVerifyMethodAsynchronous) method.Attribute.ServerTaskType = Net.TcpServer.ServerTaskType.Synchronous;
                            }
                            else if (ErrorString != null)
                            {
                                return(false);
                            }
                        }
                    }
                    Dictionary <Type, ServerCallQueue> queueTypes = null;
                    foreach (methodType method in Methods)
                    {
                        if (method != null)
                        {
                            method.SetParameterType();

                            if (method.ServerCallQueueKeyParameter != null)
                            {
                                if (queueTypes == null)
                                {
                                    queueTypes = DictionaryCreator.CreateOnly <Type, ServerCallQueue>();
                                }
                                Type            parameterType = method.ServerCallQueueKeyParameter.ParameterType;
                                ServerCallQueue queueType;
                                if (!queueTypes.TryGetValue(parameterType, out queueType))
                                {
                                    queueTypes.Add(parameterType, queueType = new ServerCallQueue(queueTypes.Count, parameterType));
                                }
                                method.ServerCallQueue = queueType;
                            }
                        }
                    }
                    Queues = queueTypes == null ? EmptyArray <ServerCallQueue> .Array : queueTypes.Values.getArray();
                    string serviceName = serverAttribute.ServerName ?? type.fullName();
                    DefaultServerAttribute = (attributeType)AutoCSer.Configuration.Common.Get(typeof(attributeType), serviceName) ?? AutoCSer.MemberCopy.Copyer <attributeType> .MemberwiseClone(serverAttribute);

                    if (DefaultServerAttribute.Name == null)
                    {
                        DefaultServerAttribute.Name = serviceName;
                    }
                    if (DefaultServerAttribute.GetServerCallQueueType != null && !typeof(AutoCSer.Net.TcpServer.IServerCallQueueSet).IsAssignableFrom(DefaultServerAttribute.GetServerCallQueueType))
                    {
                        ErrorString = DefaultServerAttribute.GetServerCallQueueType.fullName() + @" 没有继承实现 " + typeof(AutoCSer.Net.TcpServer.IServerCallQueueSet).fullName();
                        return(false);
                    }
                    return(true);
                }
                ErrorString = type.fullName() + " 不是接口类型";
                return(false);
            }