Exemplo n.º 1
0
        /// <summary>
        /// 调用
        /// </summary>
        /// <param name="endPoint"></param>
        /// <param name="handler"></param>
        /// <param name="isSendPoint"></param>
        /// <param name="paramters"></param>
        /// <returns></returns>
        protected virtual object ExecuteInovke <T>(EndPointInfo endPoint, InvokeDelegate <T> handler, bool isSendPoint,
                                                   params object[] paramters)
        {
            var key = string.Format("{0}{1}", typeof(T), endPoint.Name);

            CheckClient <T>(endPoint, key);
            if (!TryOpen <T>(key, endPoint))
            {
                return(null);
            }
            if (isSendPoint)
            {
                if (paramters == null)
                {
                    paramters = new object[] { endPoint };
                }
                else
                {
                    var temp = new List <object>(paramters)
                    {
                        endPoint
                    };
                    paramters = temp.ToArray();
                }
            }
            object rev = handler((T)Clients[key], paramters);

            endPoint.UseConnect();
            return(rev);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据节点得到EndPointInfo
        /// </summary>
        /// <param name="nodes"></param>
        /// <returns></returns>
        protected virtual IList <EndPointInfo> GetEndPointsByXmlNodes(XmlNodeList nodes)
        {
            var temp = new List <EndPointInfo>();

            EndPoints = new List <EndPointInfo>();
            foreach (XmlNode node in nodes)
            {
                var t = new EndPointInfo();
                if (node.Attributes == null)
                {
                    continue;
                }
                t.Name             = node.Attributes["Name"].Value;
                t.Nickname         = node.Attributes["Nickname"] != null ? node.Attributes["Nickname"].Value : null;
                t.CheckAlivePeriod = node.Attributes["CheckAlivePeriod"] == null ||
                                     string.IsNullOrEmpty(node.Attributes["CheckAlivePeriod"].Value)
                                             ? t.CheckAlivePeriod
                                             : Convert.ToInt32(node.Attributes["CheckAlivePeriod"].Value);
                temp.Add(t);
                if (node.Attributes["IsUsed"] == null || !"false".Equals(node.Attributes["IsUsed"].Value))
                {
                    EndPoints.Add(t);
                }
            }
            return(temp);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 检查连接一次
 /// </summary>
 /// <param name="endPoint"></param>
 protected virtual void CheckConnectionAlive <T>(EndPointInfo endPoint)
 {
     lock (CheckAliveLocker)
     {
         if (endPoint.IsStartCheckAlive || !endPoint.IsException)
         {
             return;
         }
         endPoint.IsStartCheckAlive = true;
     }
     System.Threading.Thread.Sleep(endPoint.CheckAlivePeriod);
     try
     {
         using (var channel = new CustomClientChannel <T>(endPoint.Name, ClientFile))
         {
             var client = channel.CreateChannel() as ICommunicationObject;
             if (client.State != CommunicationState.Opened)
             {
                 client.Open();
             }
             client.Close();
             endPoint.IsException       = false;
             endPoint.IsStartCheckAlive = false;
         }
     }
     catch (Exception ex)
     {
         endPoint.IsException       = true;
         endPoint.IsStartCheckAlive = false;
         Action <EndPointInfo> action = CheckConnectionAlive <T>;
         action.BeginInvoke(endPoint, null, null);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// 调用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="endPointInfos"></param>
        /// <param name="invokeHandler"></param>
        /// <param name="getEndPointsHandler"></param>
        /// <param name="isSendPoint"></param>
        /// <param name="paramters"></param>
        /// <returns></returns>
        public object Invoke <T>(IList <EndPointInfo> endPointInfos, InvokeDelegate <T> invokeHandler, GetEndPointsDelegate getEndPointsHandler, bool isSendPoint,
                                 params object[] paramters)
        {
            var points = getEndPointsHandler != null?getEndPointsHandler(endPointInfos, paramters) : endPointInfos.Where(it => it.IsException == false).ToList();

            if (points == null || points.Count == 0)
            {
                return(null);
            }
            EndPointInfo endPoint  = points.GetBestEndPoint();
            var          endPoints = endPoint.GetAllEndPoints();

            for (int i = 0; i < endPoints.Count; i++)
            {
                try
                {
                    return(ExecuteInovke(endPoints[i], invokeHandler, isSendPoint, paramters));
                }
                catch (Exception ex)
                {
                    Log.AddException(ex);
                    endPoints[i].IsException = true;
                    Action <EndPointInfo> action = CheckConnectionAlive <T>;
                    action.BeginInvoke(endPoint, null, null);
                }
            }
            return(null);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 检查客户端
 /// </summary>
 /// <param name="endPoint"></param>
 /// <param name="key"></param>
 protected virtual void CheckClient <T>(EndPointInfo endPoint, string key)
 {
     if (!Clients.ContainsKey(key))
     {
         var channel = new CustomClientChannel <T>(endPoint.Name, ClientFile);
         var client  = channel.CreateChannel();
         Clients.TryAdd(key, client);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// 尝试打开
        /// </summary>
        /// <typeparam name="T"></typeparam>
        protected virtual bool TryOpen <T>(string key, EndPointInfo endPoint)
        {
            var client = (ICommunicationObject)Clients[key];

            if (client.State != CommunicationState.Opened)
            {
                try
                {
                    client.Open();
                    return(true);
                }
                catch
                {
                    CreateNewClientChannel <T>(key, endPoint);
                }
            }
            return(true);
        }
Exemplo n.º 7
0
 /// <summary>
 /// 创建新通道
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="endPoint"></param>
 protected virtual bool CreateNewClientChannel <T>(string key, EndPointInfo endPoint)
 {
     lock (Locker)
     {
         try
         {
             ((ICommunicationObject)Clients[key]).Close();
         }
         catch
         {
         }
         finally
         {
             Clients[key] = null;
         }
         var channel   = new CustomClientChannel <T>(endPoint.Name, ClientFile);
         var newClient = channel.CreateChannel() as ICommunicationObject;
         Clients[key] = newClient;
         newClient.Open();
         return(true);
     }
 }