Exemple #1
0
        protected VT GetVT(Type type)
        {
            Type innerType;

            if (type.GetTypeInfo().IsEnum)
            {
                return(FixedTypeManager.IsVT(type.GetTypeInfo().GetEnumUnderlyingType()));
            }
            if ((innerType = Nullable.GetUnderlyingType(type)) != null)
            {
                return(FixedTypeManager.IsVT(innerType));
            }
            return(FixedTypeManager.IsVT(type));
        }
        /// <summary>
        ///     初始化属于指定服务角色的系统工作者
        /// </summary>
        /// <param name="role">服务角色</param>
        /// <param name="useRemoteConfig">是否使用远程配置的标示</param>
        /// <param name="setting">远程配置设置</param>
        /// <param name="notificationHandler">异常通知处理器</param>
        /// <param name="csnAddress">远程CSN的IP地址</param>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        public void Initialize(string role, bool useRemoteConfig, RemoteConfigurationSetting setting, ITracingNotificationHandler notificationHandler = null, string csnAddress = null)
        {
            if (string.IsNullOrEmpty(role))
            {
                throw new ArgumentNullException("role");
            }
            if (useRemoteConfig && setting == null)
            {
                throw new ArgumentNullException("setting");
            }
            if (IsInitialized)
            {
                return;
            }
            _configurationProxy = (!useRemoteConfig ? (IRemoteConfigurationProxy) new RemoteConfigurationProxy(csnAddress) : new SolitaryRemoteConfigurationProxy(csnAddress));
            //Regist("LGS", new LGSProtocolStack());
            //TracingManager.NotificationHandler = notificationHandler ?? new RemoteLogProxy();
            Role = role;
            FixedTypeManager.Add(typeof(Color), 6);
            FixedTypeManager.Add(typeof(MessageIdentity), 5);
            FixedTypeManager.Add(typeof(TransactionIdentity), 18);
            IntellectTypeProcessorMapping.Instance.Regist(new MessageIdentityProcessor());
            IntellectTypeProcessorMapping.Instance.Regist(new TransactionIdentityProcessor());
            IntellectTypeProcessorMapping.Instance.Regist(new ColorIntellectPropertyProcessor());
            ExtensionTypeMapping.Regist(typeof(MessageIdentityValueStored));
            ExtensionTypeMapping.Regist(typeof(TransactionIdentityValueStored));
            //config remote configuration loader.
            if (useRemoteConfig)
            {
                KJFramework.Configurations.Configurations.RemoteConfigLoader = new RemoteConfigurationLoader(setting);
            }
            //initialize long...long memory buffer for tcp layer.
            ChannelConst.Initialize();
            CommonCounter.Instance.Initialize();
            // Init CSN
            Dictionary <string, int> maxRanges = new Dictionary <string, int>();
            Dictionary <string, ServiceCoreConfig[]> addresses = new Dictionary <string, ServiceCoreConfig[]>();

            InitCSN(addresses, maxRanges);
            _transactionManager         = new MessageTransactionManager(new TransactionIdentityComparer());
            _metadataTransactionManager = new MetadataTransactionManager(new TransactionIdentityComparer());
            _clsuter         = HashNetworkCluster.Create(_transactionManager, new IntellectObjectSystemConnectionPool(), addresses, maxRanges);
            _metadataCluster = MetadataContainerHashNetworkCluster.Create(_metadataTransactionManager,
                                                                          new MetadataSystemConnectionPool(),
                                                                          addresses, maxRanges);
            _syncProxy     = new RemoteDataSyncProxy();
            _isInitialized = true;
        }
Exemple #3
0
 /// <summary>
 ///    初始化内部通用数据
 /// </summary>
 private static void InitializeCore(string role)
 {
     Role = role;
     FixedTypeManager.Add(typeof(MessageIdentity), 5);
     IntellectTypeProcessorMapping.Instance.Regist(new MessageIdentityProcessor());
     IntellectTypeProcessorMapping.Instance.Regist(new TransactionIdentityProcessor());
     ExtensionTypeMapping.Regist(typeof(MessageIdentityValueStored));
     ExtensionTypeMapping.Regist(typeof(TransactionIdentityValueStored));
     MemoryAllotter.Instance.Initialize();
     _transactionManager         = new MessageTransactionManager(new TransactionIdentityComparer());
     _metadataTransactionManager = new MetadataTransactionManager(new TransactionIdentityComparer());
     _protocolStackContainer     = new ProtocolStackContainer();
     _baseMessageConnectionPool  = new IntellectObjectSystemConnectionPool();
     _metadataConnectionPool     = new MetadataSystemConnectionPool();
     _clsuter                         = new NetworkCluster <BaseMessage>(_transactionManager, _baseMessageConnectionPool, ProtocolTypes.Intellegence);
     _metadataCluster                 = new NetworkCluster <MetadataContainer>(_metadataTransactionManager, _metadataConnectionPool, ProtocolTypes.Metadata);
     _baseMessageTransactionProxy     = new BusinessMessageTransactionProxy(_protocolStackContainer, _clsuter, _transactionManager, _hostProxy, _appUniqueId);
     _metadataMessageTransactionProxy = new MetadataMessageTransactionProxy(_protocolStackContainer, _metadataCluster, _metadataTransactionManager, _hostProxy, _appUniqueId);
 }
        /// <summary>
        ///     将一个智能对象转换为二进制元数据
        /// </summary>
        /// <param name="obj">智能对象</param>
        /// <param name="proxy">内存段代理器</param>
        /// <returns>返回当前已经被序列化对象的总体长度</returns>
        internal static void ToBytes(IThriftObject obj, IMemorySegmentProxy proxy)
        {
            //获取智能对象中的智能属性,并按照Id来排序
            ToBytesAnalyseResult[] properties = Analyser.ToBytesAnalyser.Analyse(obj);
            if (properties.Length == 0)
            {
                return;
            }
            for (int l = 0; l < properties.Length; l++)
            {
                ToBytesAnalyseResult property = properties[l];
                //先检查完全缓存机制
                if (property.HasCacheFinished)
                {
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    continue;
                }

                #region 普通类型判断

                IThriftTypeProcessor intellectTypeProcessor = ThriftTypeProcessorMapping.Instance.GetProcessor(property.Property.PropertyType);
                if (intellectTypeProcessor != null)
                {
                    //添加热缓存
                    IThriftTypeProcessor processor = intellectTypeProcessor;
                    property.CacheProcess = processor.Process;
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    property.HasCacheFinished = true;
                    continue;
                }

                #endregion

                #region 枚举类型判断

                //枚举类型
                if (property.Property.PropertyType.IsEnum)
                {
                    //获取枚举类型
                    Type enumType = Enum.GetUnderlyingType(property.Property.PropertyType);
                    intellectTypeProcessor = ThriftTypeProcessorMapping.Instance.GetProcessor(enumType);
                    if (intellectTypeProcessor == null)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                    }
                    //添加热缓存
                    IThriftTypeProcessor processor = intellectTypeProcessor;
                    property.CacheProcess = processor.Process;
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    property.HasCacheFinished = true;
                    continue;
                }

                #endregion

                #region 可空类型判断

                Type innerType;
                if ((innerType = Nullable.GetUnderlyingType(property.Property.PropertyType)) != null)
                {
                    intellectTypeProcessor = ThriftTypeProcessorMapping.Instance.GetProcessor(innerType);
                    if (intellectTypeProcessor != null)
                    {
                        //添加热缓存
                        IThriftTypeProcessor processor = intellectTypeProcessor;
                        property.CacheProcess = delegate(IMemorySegmentProxy innerProxy, ThriftPropertyAttribute innerAttribute, ToBytesAnalyseResult innerAnalyseResult, object innerTarget, bool innerIsArrayElement, bool innerNullable)
                        {
                            processor.Process(innerProxy, innerAttribute, innerAnalyseResult, innerTarget, innerIsArrayElement, true);
                        };
                        property.CacheProcess(proxy, property.Attribute, property, obj, false, true);
                        property.HasCacheFinished = true;
                        continue;
                    }
                    throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                }

                #endregion

                #region Thrift Object类型判断

                if (property.Property.PropertyType.GetInterface(Consts.ThriftObjectFullName) != null)
                {
                    //添加热缓存
                    property.CacheProcess = delegate(IMemorySegmentProxy innerProxy, ThriftPropertyAttribute innerAttribute, ToBytesAnalyseResult innerAnalyseResult, object innerTarget, bool innerIsArrayElement, bool innerNullable)
                    {
                        IThriftObject innerIntellectObj = innerAnalyseResult.GetValue <IThriftObject>(innerTarget);
                        if (innerIntellectObj == null)
                        {
                            return;
                        }
                        innerProxy.WriteByte((byte)innerAttribute.Id);
                        MemoryPosition startPos = innerProxy.GetPosition();
                        innerProxy.Skip(4);
                        ToBytes(innerIntellectObj, innerProxy);
                        MemoryPosition endPos = innerProxy.GetPosition();
                        innerProxy.WriteBackInt32(startPos, MemoryPosition.CalcLength(innerProxy.SegmentCount, startPos, endPos) - 4);
                    };
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    property.HasCacheFinished = true;
                    continue;
                }

                #endregion

                #region 数组的判断

                if (property.Property.PropertyType.IsArray)
                {
                    if (!property.Property.PropertyType.HasElementType)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                    }
                    Type elementType = property.Property.PropertyType.GetElementType();
                    VT   vt          = FixedTypeManager.IsVT(elementType);
                    //special optimize.
                    IThriftTypeProcessor arrayProcessor = ArrayTypeProcessorMapping.Instance.GetProcessor(property.Property.PropertyType);
                    if (arrayProcessor != null)
                    {
                        property.CacheProcess = arrayProcessor.Process;
                    }
                    //is VT, but cannot find special processor.
                    else if (vt != null)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                    }
                    else if (elementType.IsSubclassOf(typeof(ThriftObject)))
                    {
                        //Add hot cache.
                        property.CacheProcess = delegate(IMemorySegmentProxy innerProxy, ThriftPropertyAttribute innerAttribute, ToBytesAnalyseResult innerAnalyseResult, object innerTarget, bool innerIsArrayElement, bool innerNullable)
                        {
                            IThriftObject[] array = innerAnalyseResult.GetValue <IThriftObject[]>(innerTarget);
                            if (array == null)
                            {
                                if (innerAttribute.Optional)
                                {
                                    return;
                                }
                                throw new PropertyNullValueException(string.Format(ExceptionMessage.EX_PROPERTY_VALUE, innerAttribute.Id, innerAnalyseResult.Property.Name, innerAnalyseResult.Property.PropertyType));
                            }
                            //property type
                            innerProxy.WriteSByte((sbyte)innerAttribute.PropertyType);
                            innerProxy.WriteInt16(((short)innerAttribute.Id).ToBigEndian());
                            //element type(1) + array length(4)
                            innerProxy.WriteSByte((sbyte)PropertyTypes.Struct);
                            innerProxy.WriteInt32(array.Length.ToBigEndian());
                            for (int i = 0; i < array.Length; i++)
                            {
                                IThriftObject element = array[i];
                                ToBytes(element, innerProxy);
                                //end flag of Thrift protocol object.
                                innerProxy.WriteSByte(0x00);
                            }
                        };
                    }
                    else
                    {
                        intellectTypeProcessor = ThriftTypeProcessorMapping.Instance.GetProcessor(elementType);
                        if (intellectTypeProcessor == null)
                        {
                            throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, elementType));
                        }
                        //Add hot cache.
                        IThriftTypeProcessor processor = intellectTypeProcessor;
                        property.CacheProcess = delegate(IMemorySegmentProxy innerProxy, ThriftPropertyAttribute innerAttribute, ToBytesAnalyseResult innerAnalyseResult, object innerTarget, bool innerIsArrayElement, bool innerNullable)
                        {
                            Array array = innerAnalyseResult.GetValue <Array>(innerTarget);
                            if (array == null)
                            {
                                if (innerAttribute.Optional)
                                {
                                    return;
                                }
                                throw new PropertyNullValueException(string.Format(ExceptionMessage.EX_PROPERTY_VALUE, innerAttribute.Id, innerAnalyseResult.Property.Name, innerAnalyseResult.Property.PropertyType));
                            }
                            //id(1) + total length(4) + rank(4)
                            innerProxy.WriteByte((byte)innerAttribute.Id);
                            MemoryPosition startPosition = innerProxy.GetPosition();
                            innerProxy.Skip(4);
                            innerProxy.WriteInt32(array.Length);
                            for (int i = 0; i < array.Length; i++)
                            {
                                object element = array.GetValue(i);
                                if (element == null)
                                {
                                    innerProxy.WriteUInt16(0);
                                }
                                else
                                {
                                    MemoryPosition innerStartObjPosition = innerProxy.GetPosition();
                                    innerProxy.Skip(Size.UInt16);
                                    processor.Process(innerProxy, innerAttribute, innerAnalyseResult, element, true);
                                    MemoryPosition innerEndObjPosition = innerProxy.GetPosition();
                                    innerProxy.WriteBackUInt16(innerStartObjPosition, (ushort)(MemoryPosition.CalcLength(innerProxy.SegmentCount, innerStartObjPosition, innerEndObjPosition) - 2));
                                }
                            }
                            MemoryPosition endPosition = innerProxy.GetPosition();
                            innerProxy.WriteBackInt32(startPosition, MemoryPosition.CalcLength(innerProxy.SegmentCount, startPosition, endPosition) - 4);
                        };
                    }
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    property.HasCacheFinished = true;
                    continue;
                }

                #endregion

                throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
            }
        }