internal void ApplyTo(LoadBalancedQueue q)
 {
     q.defaultUpdateInterval = this.updateInterval;
     q.maxUpdatesPerInterval = this.maxUpdatesPerFrame;
     q.maxUpdateTimeInMillisecondsPerUpdate = this.maxUpdateTimeInMillisecondsPerUpdate;
     q.autoAdjust = this.autoAdjust;
     this.associatedLoadBalancer = q;
 }
 internal void Dispose()
 {
     if (!_isDisposed)
     {
         _isDisposed = true;
         parent      = null;
     }
 }
 internal void Dispose()
 {
     if (!this._isDisposed)
     {
         this._isDisposed = true;
         this.parent      = null;
     }
 }
 internal static LoadBalancerConfig From(string name, LoadBalancedQueue q)
 {
     return(new LoadBalancerConfig()
     {
         associatedLoadBalancer = q,
         autoAdjust = q.autoAdjust,
         maxUpdatesPerFrame = q.maxUpdatesPerInterval,
         maxUpdateTimeInMillisecondsPerUpdate = q.maxUpdateTimeInMillisecondsPerUpdate,
         updateInterval = q.defaultUpdateInterval,
         targetLoadBalancer = name
     });
 }
Пример #5
0
        private void ResolveLoadBalancers()
        {
            var resolveBalancers = new List <LoadBalancedQueue>();
            var configSet        = new Dictionary <string, LoadBalancerConfig>(StringComparer.Ordinal);

            if (_configurations != null)
            {
                foreach (var cfg in _configurations)
                {
                    configSet.Add(cfg.targetLoadBalancer, cfg);
                }
            }

            var lbType   = typeof(LoadBalancer);
            var qType    = typeof(LoadBalancedQueue);
            var qTypeAlt = typeof(ILoadBalancer);

#if NETFX_CORE
            var lbTypeInf = lbType.GetTypeInfo();
            var asm       = lbTypeInf.Assembly;

            var sources = (from t in asm.DefinedTypes
                           where t == lbTypeInf || t.IsSubclassOf(lbType)
                           select t).ToArray();

            //Process properties
            var props = from t in sources
                        from p in t.DeclaredProperties
                        where (p.PropertyType == qType || p.PropertyType == qTypeAlt) && p.CanRead && p.GetMethod.IsStatic
                        select p;
#elif UNITY_WP8
            var asm = lbType.Assembly;

            var sources = (from t in asm.GetTypes()
                           where t == lbType || t.IsSubclassOf(lbType)
                           select t).ToArray();

            //Process properties
            var props = from t in sources
                        from p in t.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly)
                        where (p.PropertyType == qType || p.PropertyType == qTypeAlt) && p.CanRead
                        select p;
#else
            var asm = Assembly.GetAssembly(lbType);

            var sources = (from t in asm.GetTypes()
                           where t == lbType || t.IsSubclassOf(lbType)
                           select t).ToArray();

            //Process properties
            var props = from t in sources
                        from p in t.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly)
                        where (p.PropertyType == qType || p.PropertyType == qTypeAlt) && p.CanRead
                        select p;
#endif
            LoadBalancerConfig config;
            foreach (var p in props)
            {
                var lbName = p.Name;

                var balancer = p.GetValue(null, null) as LoadBalancedQueue;
                if (balancer == null && p.CanWrite)
                {
                    balancer = new LoadBalancedQueue(4);
                    p.SetValue(null, balancer, null);
                }

                if (balancer != null)
                {
                    if (!configSet.TryGetValue(lbName, out config))
                    {
                        config = LoadBalancerConfig.From(lbName, balancer);
                        configSet.Add(lbName, config);
                    }
                    else
                    {
                        config.ApplyTo(balancer);
                    }

                    resolveBalancers.Add(balancer);
                }
            }

            //Process fields
#if NETFX_CORE
            var fields = from t in sources
                         from f in t.DeclaredFields
                         where (f.FieldType == qType || f.FieldType == qTypeAlt) && f.IsStatic
                         select f;
#else
            var fields = from t in sources
                         from f in t.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly)
                         where (f.FieldType == qType || f.FieldType == qTypeAlt)
                         select f;
#endif
            foreach (var f in fields)
            {
                var lbName = f.Name;

                var balancer = f.GetValue(null) as LoadBalancedQueue;
                if (balancer == null && !f.IsInitOnly)
                {
                    balancer = new LoadBalancedQueue(4);
                    f.SetValue(null, balancer);
                }

                if (balancer != null)
                {
                    if (!configSet.TryGetValue(lbName, out config))
                    {
                        config = LoadBalancerConfig.From(lbName, balancer);
                        configSet.Add(lbName, config);
                    }
                    else
                    {
                        config.ApplyTo(balancer);
                    }

                    resolveBalancers.Add(balancer);
                }
            }

            _configurations = configSet.Values.Where(c => c.associatedLoadBalancer != null).OrderBy(c => c.targetLoadBalancer).ToArray();
            _loadBalancers  = resolveBalancers.ToArray();
        }
        private void ResolveLoadBalancers()
        {
            LoadBalancerConfig       loadBalancerConfig;
            List <LoadBalancedQueue> loadBalancedQueues  = new List <LoadBalancedQueue>();
            Dictionary <string, LoadBalancerConfig> strs = new Dictionary <string, LoadBalancerConfig>(StringComparer.Ordinal);

            if (this._configurations != null)
            {
                LoadBalancerConfig[] loadBalancerConfigArray = this._configurations;
                for (int i = 0; i < (int)loadBalancerConfigArray.Length; i++)
                {
                    LoadBalancerConfig loadBalancerConfig1 = loadBalancerConfigArray[i];
                    strs.Add(loadBalancerConfig1.targetLoadBalancer, loadBalancerConfig1);
                }
            }
            Type type  = typeof(LoadBalancer);
            Type type1 = typeof(LoadBalancedQueue);
            Type type2 = typeof(ILoadBalancer);

            Type[] array = (
                from asm in (IEnumerable <Assembly>)AppDomain.CurrentDomain.GetAssemblies()
                from t in asm.GetTypes()
                select new { asm = asm, t = t }).Where((argument0) => {
                if (argument0.t == type)
                {
                    return(true);
                }
                return(argument0.t.IsSubclassOf(type));
            }).Select((argument3) => argument3.t).ToArray <Type>();
            foreach (PropertyInfo propertyInfo in (
                         from t in (IEnumerable <Type>) array
                         from p in t.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public)
                         select new { t = t, p = p }).Where((argument1) => {
                if (!(argument1.p.PropertyType == type1) && !(argument1.p.PropertyType == type2))
                {
                    return(false);
                }
                return(argument1.p.CanRead);
            }).Select((argument4) => argument4.p))
            {
                string            name  = propertyInfo.Name;
                LoadBalancedQueue value = propertyInfo.GetValue(null, null) as LoadBalancedQueue;
                if (value == null && propertyInfo.CanWrite)
                {
                    value = new LoadBalancedQueue(4);
                    propertyInfo.SetValue(null, value, null);
                }
                if (value == null)
                {
                    continue;
                }
                if (strs.TryGetValue(name, out loadBalancerConfig))
                {
                    loadBalancerConfig.ApplyTo(value);
                }
                else
                {
                    loadBalancerConfig = LoadBalancerConfig.From(name, value);
                    strs.Add(name, loadBalancerConfig);
                }
                loadBalancedQueues.Add(value);
            }
            foreach (FieldInfo fieldInfo in (
                         from t in (IEnumerable <Type>) array
                         from f in t.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public)
                         select new { t = t, f = f }).Where((argument2) => {
                if (argument2.f.FieldType == type1)
                {
                    return(true);
                }
                return(argument2.f.FieldType == type2);
            }).Select((argument5) => argument5.f))
            {
                string            str = fieldInfo.Name;
                LoadBalancedQueue loadBalancedQueue = fieldInfo.GetValue(null) as LoadBalancedQueue;
                if (loadBalancedQueue == null && !fieldInfo.IsInitOnly)
                {
                    loadBalancedQueue = new LoadBalancedQueue(4);
                    fieldInfo.SetValue(null, loadBalancedQueue);
                }
                if (loadBalancedQueue == null)
                {
                    continue;
                }
                if (strs.TryGetValue(str, out loadBalancerConfig))
                {
                    loadBalancerConfig.ApplyTo(loadBalancedQueue);
                }
                else
                {
                    loadBalancerConfig = LoadBalancerConfig.From(str, loadBalancedQueue);
                    strs.Add(str, loadBalancerConfig);
                }
                loadBalancedQueues.Add(loadBalancedQueue);
            }
            this._configurations = (
                from c in strs.Values
                where c.associatedLoadBalancer != null
                orderby c.targetLoadBalancer
                select c).ToArray <LoadBalancerConfig>();
            this._loadBalancers = loadBalancedQueues.ToArray();
        }