public static IHostConnection CreateConnection(HostConnectionType ProxyType) { try { if (!ConfigLoaded) { LoadFromConfig(); } foreach (var host in CurrentHostConnections.Values) { if (ProxyType == host.HostType) { return(host); } } return(null); } catch (Exception ex) { LOG.Error(string.Format("创建连接类型{0}失败:{1}", ProxyType.ToString(), ex)); return(null); } }
public HostConnection(HostConnectionType ProxyType, string HostAddress) { HostType = ProxyType; RemoteHostAddress = HostAddress; StartHeartbeat(); StartReconnect(); //StartConnect(); }
public static void ReleaseConnection(HostConnectionType ProxyType) { try { foreach (var host in CurrentHostConnections.Values) { host.StopConnect(); } } catch (Exception ex) { LOG.Error(string.Format("断开连接类型{0}失败:{1}", ProxyType.ToString(), ex)); } }
public RedundancyHostConnection(HostConnectionType ProxyType, string Address1, string Address2) { _address1 = Address1; _address2 = Address2; HostType = ProxyType; _firstHostConnection = new HostConnection(HostType, Address1); _secondHostConnection = new HostConnection(HostType, Address2); _firstHostConnection.OnConnectedHander += SubHostConnectionOnConnected; _secondHostConnection.OnConnectedHander += SubHostConnectionOnConnected; _firstHostConnection.OnDisconnectedHander += SubHostConnectionOnDisConected; _secondHostConnection.OnDisconnectedHander += SubHostConnectionOnDisConected; }
private static void LoadFromConfig() { try { if (ConfigLoaded) { return; } string appPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; string xmlPathfile = appPath + "WCFClient.xml"; XmlDocument doc = new XmlDocument(); doc.Load(xmlPathfile); XmlNodeList RemoteHosts = doc.SelectNodes("//WCFClientSetting//RemoteHost"); foreach (XmlNode RemoteHost in RemoteHosts) {//遍历每一个配置组 string HostGroupName; XmlElement RemoteHostItem = (XmlElement)RemoteHost; HostGroupName = RemoteHostItem.HasAttribute("Name") ? RemoteHostItem.GetAttribute("Name").ToLower() : "default"; Dictionary <string, IHostConnection> connections = new Dictionary <string, IHostConnection>(); //装载连接 foreach (XmlNode item in RemoteHost) {//装载连接配置 if (item.NodeType == XmlNodeType.Comment) { continue; } XmlElement host = (XmlElement)item; string strName = host.GetAttribute("Name"); string strService = host.GetAttribute("Service"); string strAddress = host.GetAttribute("Address"); string partnerAddress = ""; if (host.HasAttribute("PartnerAddress")) {//冗余连接 partnerAddress = host.GetAttribute("PartnerAddress"); } HostConnectionType connectionType = HostConnectionType.Unknow; if (strService == "MachineHost") { connectionType = HostConnectionType.Machine; } else if (strService == "ProcessHost") { connectionType = HostConnectionType.Process; } else if (strService == "ResourceHost") // add by dongmin 20180221 { connectionType = HostConnectionType.Resource; } else if (strService == "PartnerHost") // add by dongmin 20180310 { connectionType = HostConnectionType.Partner; } else if (strService == "AdminHost") // add by dongmin 20191109 { connectionType = HostConnectionType.Admin; } else { throw new Exception("没有定义该连接" + strService); } if (string.IsNullOrEmpty(partnerAddress)) { connections.Add(strName, new HostConnection(connectionType, strAddress)); } else { connections.Add(strName, new RedundancyHostConnection(connectionType, strAddress, partnerAddress)); } } //放到连接组集合 if (!HostConnectionGroups.ContainsKey(HostGroupName)) { HostConnectionGroups.Add(HostGroupName, connections); } } SelectCurrentGroup("default"); ConfigLoaded = true; } catch (Exception ex) { LOG.Error(string.Format("加载WCFClient配置文件出错." + ex)); } }