Exemplo n.º 1
0
        /// <summary>
        /// 注册Remoting客户端
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="name">名称</param>
        /// <param name="uri">URI地址</param>
        public static void RegisterRemotingClient <T>(string name, string uri) where T : class
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(uri))
            {
                return;
            }
            Type   type    = typeof(T);
            string strType = type.ToString();

            if (!Contains(type, uri, name))
            {
                RemotingHandlerItem <T> item = new RemotingHandlerItem <T>();
                item.Name = name;
                item.Uri  = uri;
                if (item.Instance != null)
                {
                    if (_RemotingClientCache.ContainsKey(strType))
                    {
                        List <object> lists = _RemotingClientCache[strType];
                        //if (!lists.Contains(item))
                        //{
                        lists.Add(item);
                        //}
                    }
                    else
                    {
                        List <object> lists = new List <object>();
                        _RemotingClientCache.Add(strType, lists);
                        lists.Add(item);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获得随机的Remoting客户端(通过 Type 的方式)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static RemotingHandlerItem <T> GetRandomRemotingHandlerItem <T>() where T : class
        {
            Type   type    = typeof(T);
            string strType = type.ToString();
            RemotingHandlerItem <T> result = null;

            if (_RemotingClientCache.ContainsKey(strType))
            {
                List <object> lists = _RemotingClientCache[strType];
                if (lists.Count == 1)
                {
                    result = lists[0] as RemotingHandlerItem <T>;
                    if (result.Status != ERemotingClientStatus.Working)
                    {
                        result = null;
                    }
                }
                else
                {
                    List <object> results = lists.FindAll(obj => ((RemotingHandlerItem <T>)obj).Status == ERemotingClientStatus.Working);
                    int           count   = results.Count;
                    if (count == 1)
                    {
                        result = results[0] as RemotingHandlerItem <T>;
                    }
                    else if (count > 1)
                    {
                        result = results[GetRandom(count)] as RemotingHandlerItem <T>;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 3
0
 public override bool Equals(object obj)
 {
     if (obj is RemotingHandlerItem <T> )
     {
         RemotingHandlerItem <T> other = obj as RemotingHandlerItem <T>;
         if (other.Uri == Uri)
         {
             return(true);
         }
     }
     return(false);
 }
        /// <summary>
        /// 获得闲置的Remoting客户端(通过 Type 和 Name 的方式)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public static RemotingHandlerItem <T> GetIdleRemotingHandlerItem <T>(string name) where T : class
        {
            Type   type    = typeof(T);
            string strType = type.ToString();
            RemotingHandlerItem <T> result = null;

            if (_RemotingClientCache.ContainsKey(strType))
            {
                List <object> lists = _RemotingClientCache[strType];
                if (lists.Count == 1)
                {
                    result = lists[0] as RemotingHandlerItem <T>;
                    if (result.Status != ERemotingClientStatus.Working)
                    {
                        result = null;
                    }
                }
                else
                {
                    Type t = typeof(RemotingHandlerItem <>);
                    t = t.MakeGenericType(type);

                    PropertyInfo statusPropertyInfo = t.GetProperty("Status");
                    PropertyInfo processCountInfo   = t.GetProperty("ProcessCount");
                    int          maxValue           = int.MaxValue;
                    foreach (object obj in lists)
                    {
                        RemotingHandlerItem <T> item = obj as RemotingHandlerItem <T>;
                        if (item.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
                        {
                            object statusValue           = statusPropertyInfo.GetValue(obj, null);
                            ERemotingClientStatus status = (ERemotingClientStatus)statusValue;
                            if (status == ERemotingClientStatus.Working)
                            {
                                object processCountValue = processCountInfo.GetValue(obj, null);
                                int    processCount      = int.MaxValue;
                                Int32.TryParse(processCountValue.ToString(), out processCount);
                                if (processCount < maxValue)
                                {
                                    result   = item;
                                    maxValue = processCount;
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 反射方式调用的
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="item"></param>
 private static void SetFailuredTime <T>(RemotingHandlerItem <T> item) where T : class
 {
     if (item != null && item.Status != ERemotingClientStatus.Error)
     {
         item.Status = ERemotingClientStatus.Error;
         item.UpdateFailuredTime();
         //服务链接失败时,指定时间后重新尝试链接
         Action action = new Action(() =>
         {
             Thread.Sleep(RECALL_SERVER_SECONDS);
             item.Status = ERemotingClientStatus.Working;
         });
         action.BeginInvoke(null, null);
     }
 }
        /// <summary>
        /// 获得随机的Remoting客户端(通过 Type 和 Name 的方式)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public static RemotingHandlerItem <T> GetRandomRemotingHandlerItem <T>(string name) where T : class
        {
            Type   type    = typeof(T);
            string strType = type.ToString();
            RemotingHandlerItem <T> result = null;

            if (_RemotingClientCache.ContainsKey(strType))
            {
                List <object> lists = _RemotingClientCache[strType];
                if (lists.Count == 1)
                {
                    result = lists[0] as RemotingHandlerItem <T>;
                    if (result.Status != ERemotingClientStatus.Working)
                    {
                        result = null;
                    }
                }
                else
                {
                    List <object> results = lists.FindAll(obj =>
                    {
                        RemotingHandlerItem <T> item = (RemotingHandlerItem <T>)obj;
                        return(item.Status == ERemotingClientStatus.Working && item.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));
                    });
                    int count = results.Count;
                    if (count == 1)
                    {
                        result = results[0] as RemotingHandlerItem <T>;
                    }
                    else if (count > 1)
                    {
                        result = results[GetRandom(count)] as RemotingHandlerItem <T>;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 7
0
 public int CompareTo(RemotingHandlerItem <T> other)
 {
     return(ProcessCount.CompareTo(other.ProcessCount));
 }
        /// <summary>
        /// 调用Remoting远程方法(通过 Type 和 Name 的方式)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="func"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static object Invoke <T>(string name, string func, params object[] param) where T : class
        {
            if (string.IsNullOrEmpty(func))
            {
                return(null);
            }
            Type   type    = typeof(T);
            string strType = type.ToString();
            object result  = null;

            if (_RemotingClientCache.ContainsKey(strType))
            {
                RemotingHandlerItem <T> obj = GetIdleRemotingHandlerItem <T>(name);
                if (obj == null)
                {
                    obj = GetRandomRemotingHandlerItem <T>(name);
                    if (obj != null)
                    {
                        obj.AddProccessCount();

                        try
                        {
                            MethodInfo methodInfo = type.GetMethod(func);
                            if (methodInfo != null)
                            {
                                result = methodInfo.Invoke(obj.Instance, param);
                            }
                        }
                        catch (Exception ex)
                        {
                            OnRemotingServerError(obj.Name, obj.Uri, ex.ToString());
                        }
                        finally
                        {
                            obj.SubtractProccessCount();
                        }
                    }
                }
                else
                {
                    bool flag = false;
                    while (obj != null && !flag)
                    {
                        obj.AddProccessCount();

                        try
                        {
                            MethodInfo methodInfo = type.GetMethod(func);
                            if (methodInfo != null)
                            {
                                result = methodInfo.Invoke(obj.Instance, param);
                            }
                            flag = true;
                        }
                        catch (SocketException ex1)
                        {
                            MethodInfo method = typeof(RemotingClientManager).GetMethod("SetFailuredTime", BindingFlags.NonPublic | BindingFlags.Static);
                            method = method.MakeGenericMethod(type);
                            method.Invoke(null, new object[] { obj });
                            OnRemotingServerError(obj.Name, obj.Uri, ex1.ToString());
                        }
                        catch (TargetInvocationException ex2)
                        {
                            MethodInfo method = typeof(RemotingClientManager).GetMethod("SetFailuredTime", BindingFlags.NonPublic | BindingFlags.Static);
                            method = method.MakeGenericMethod(type);
                            method.Invoke(null, new object[] { obj });
                            OnRemotingServerError(obj.Name, obj.Uri, ex2.ToString());
                        }
                        catch (Exception ex)
                        {
                            OnRemotingServerError(obj.Name, obj.Uri, ex.ToString());
                        }
                        finally
                        {
                            obj.SubtractProccessCount();
                        }

                        if (!flag)
                        {
                            obj = GetIdleRemotingHandlerItem <T>();
                        }
                    }
                }
            }
            return(result);
        }