public virtual void AddClient(Config config, Dictionary <string, dynamic> proxyobj) { _config = config; string[] ipport = ((string)config.GetValue("server", "ip")).Split(';'); int pool = Convert.ToInt32(config.GetValue("server", "pool", 2)); for (int i = 0; i < ipport.Length; i++) { if (ipport[i].Split(':').Length > 1) { string[] ip = ipport[i].Split(':'); ServerPort port = new ServerPort() { Ip = ip[0], Port = int.Parse(ip[1]), Pool = pool }; addClient(port, config, proxyobj); } } string[] relation = config.GetEntryNames("relation"); if (relation != null) { Proxy.RelationDll = new Dictionary <string, string>(); foreach (string va in relation) { if (!Proxy.RelationDll.ContainsKey(va)) { object rdll = config.GetValue("relation", va); if (rdll != null) { Proxy.RelationDll.Add(va, rdll.ToString()); } } } } }
/// <summary> /// 接收广播添加服务 /// </summary> private void GetBorad() { try { if (clientBoard != null) { return; } clientBoard = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); clientBoard.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, false); clientBoard.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); clientBoard.Bind(new IPEndPoint(IPAddress.Any, 7788)); EndPoint endpoint = new IPEndPoint(IPAddress.Any, 0); while (!cts.Token.IsCancellationRequested) { Dictionary <string, dynamic> dic = new Dictionary <string, dynamic>(_proxobj); byte[] buf = new byte[1024]; int l = clientBoard.ReceiveFrom(buf, ref endpoint); int port = int.Parse(Encoding.Default.GetString(buf, 0, l)); IPAddress ip = ((IPEndPoint)endpoint).Address; bool flag = false; foreach (KeyValuePair <string, dynamic> cp in dic) { foreach (ChannelPool p in cp.Value.Client.Channels) { if (p.IpPoint.Address.ToString() == ip.ToString() && p.IpPoint.Port == port) { flag = true; break; } } if (flag == true) { break; } } if (!flag) { ServerPort sport = new ServerPort() { Ip = ip.ToString(), Port = port, Pool = 10 }; // _client.AddClient(config, _proxobj); } } } catch (Exception ex) { Console.WriteLine(ex); _log.Error(ex); } }
private void InitializeClient(Config config) { Pong = new Timer(); string[] ipport = ((string)config.GetValue("server", "ip")).Split(';'); int pool = Convert.ToInt32(config.GetValue("server", "pool", 2)); for (int i = 0; i < ipport.Length; i++) { if (ipport[i].Split(':').Length > 1) { string[] ip = ipport[i].Split(':'); ServerPort port = new ServerPort() { Ip = ip[0], Port = int.Parse(ip[1]), Pool = pool }; AddClient(port); } } string[] relation = config.GetEntryNames("relation"); if (relation != null) { Proxy.RelationDll = new Dictionary <string, string>(); foreach (string va in relation) { if (!Proxy.RelationDll.ContainsKey(va)) { object rdll = config.GetValue("relation", va); if (rdll != null) { Proxy.RelationDll.Add(va, rdll.ToString()); } } } } Pong.Interval = 180000; Pong.Elapsed -= Pong_Elapsed; Pong.Elapsed += Pong_Elapsed; Pong.Start(); getBoardthread = new Thread(new ThreadStart(GetBorad)); getBoardthread.Start(); }
private void addClient(object serverport, Config config, Dictionary <string, dynamic> proxobj) { ServerPort sport = (ServerPort)serverport; string ip = sport.Ip; int port = sport.Port; int pool = sport.Pool; IClient _client; string password = (string)config.GetValue("server", "password"); string username = (string)config.GetValue("server", "username"); ChannelPool channlepool = new ChannelPool { IpPoint = new IPEndPoint(IPAddress.Parse(ip), port), Available = true }; this.Channels.Add(channlepool); this.Authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)); DataEventArgs vali = new DataEventArgs { ActionCmd = CallActionCmd.Validate.ToString(), ActionParam = this.Authorization, TaskId = 0 }; DataEventArgs valiresult = this.GetResult(vali, channlepool); if (valiresult.StatusCode != StatusCode.Success)///如果验证失败退出 { return; } this.Authorization = valiresult.HttpSessionId; DataEventArgs callreg = new DataEventArgs() { HttpSessionId = this.Authorization, ActionCmd = CallActionCmd.Register.ToString(), ActionParam = "Register", T = typeof(List <RegisterInfo>) }; DataEventArgs reg = this.GetResult(callreg, channlepool); if (reg.StatusCode != StatusCode.Success) { return; } this.Run(); List <RegisterInfo> registerInfos = new List <RegisterInfo>(); if (!string.IsNullOrEmpty(reg.Json)) { registerInfos = Serializer.ToEntity <List <RegisterInfo> >(reg.Json); } else { registerInfos = Serializer.ToEntity <List <RegisterInfo> >(reg.Binary); } if (registerInfos != null) { for (int i = 0; i < pool; i++) { ChannelPool channel = new ChannelPool { IpPoint = new IPEndPoint(IPAddress.Parse(ip), port), Available = true }; this.Channels.Add(channel); } foreach (RegisterInfo info in registerInfos) { lock (proxobj) { try { string assname = string.Format("{0}.{1}", info.FaceNameSpace, info.InterfaceName); dynamic val = new { ClassName = info.ClassName, NameSpace = info.NameSpace, Client = this }; if (!proxobj.ContainsKey(assname)) { proxobj.Add(assname, val); } else { bool pc = false; foreach (ChannelPool p in proxobj[assname].Client.Channels) { if (p.IpPoint == channlepool.IpPoint) { pc = true; } } if (!pc) { proxobj[assname].Client.Channels.Add(channlepool); for (int i = 0; i < 10; i++) { ChannelPool channel = new ChannelPool { IpPoint = new IPEndPoint(IPAddress.Parse(ip), port), Available = true }; proxobj[assname].Client.Channels.Add(channel); } } } } catch (Exception ex) { Console.WriteLine(ex); } } } } }