private void GateWay_newwayEvent(int temp) { try { int len = (temp + (Proportion - 1)) % Proportion; if (len == 0) { foreach (CommandItem ci in CommandItemS) { P2Pclient p2p = new P2Pclient(false); p2p.receiveServerEvent += (V_receiveServerEvent); p2p.timeoutevent += (V_timeoutevent); p2p.ErrorMge += (V_ErrorMge); if (p2p.start(ci.Ip, ci.Port, false)) { ci.Client.Add(p2p); } } } } catch (Exception e) { EventMylog("EventUpdataConnSoc---新建连接", ""); } }
P2Pclient newp2p(String Ip, int Port) { P2Pclient p2p = new P2Pclient(false); if (Wptype == WeavePortTypeEnum.Bytes) { p2p.receiveServerEventbit += P2p_receiveServerEventbit; } else { p2p.receiveServerEvent += (V_receiveServerEvent); } p2p.timeoutobjevent += P2p_timeoutobjevent; p2p.ErrorMge += (V_ErrorMge); if (p2p.start(Ip, Port, false)) { return(p2p); } else { if (EventMylog != null) { EventMylog("节点连接失败", "命令:" + Ip + ":" + Port + ":节点连接失败,抛弃此节点"); } } return(null); }
private void P2p_timeoutobjevent(P2Pclient p2pobj) { P2Pclient Client = p2pobj; lab1100: if (!Client.Isline) { string port = Client.localprot; if (EventMylog != null) { EventMylog("节点重新连接--:", Client.IP + ":" + Client.PORT); } if (!Client.Restart(false)) { System.Threading.Thread.Sleep(1000); goto lab1100; } else { try { EventMylog("节点重新连接-通知下线-:", Client.IP + ":" + Client.PORT); } catch (Exception ee) { EventMylog("节点重新连接-Restart-:", ee.Message); } //Client.send(0xff, "Restart|"+ port); EventMylog("节点重新连接-Restart-:", Client.IP + ":" + Client.PORT); } } }
public override bool getdata(HttpProcessor p, byte command, string data) { string returnstr = ""; foreach (CommandItem ci in CommandItems) { if (ci.CommName == command) { P2Pclient p2p = new P2Pclient(false); p2p.receiveServerEvent += new P2Pclient.receive((c, text) => { returnstr = text; }); //p2p.timeoutevent += (V_timeoutevent); p2p.ErrorMge += (V_ErrorMge); if (p2p.start(ci.Ip, ci.Port, false)) { System.Threading.Thread.Sleep(200); p2p.send(command, data); int count = 0; while (returnstr == "") { System.Threading.Thread.Sleep(200); if (count > 450) { p.outputStream.WriteLine("响应超时"); return(false); } count++; } p.outputStream.WriteLine(returnstr); return(true); } else { p.outputStream.WriteLine("不能连接指定的服务"); return(false); } } // _0x01. } p.outputStream.WriteLine("不能找到指定的服务"); int i = httpProcessorList.Count; HttpProcessor[] hps = new HttpProcessor[i]; httpProcessorList.CopyTo(hps); foreach (HttpProcessor hp in hps) { if (hp == p) { httpProcessorList.Remove(p); return(true); } } return(false); }
private void button1_Click(object sender, EventArgs e) { P2Pclient p2p = new P2Pclient(false); p2p.receiveServerEvent += P2p_receiveServerEvent; p2p.timeoutevent += P2p_timeoutevent; bool b = p2p.start("127.0.0.1", 8989, false); MessageBox.Show(b.ToString() + ":" + p2p.Tokan); p2p.send(0x01, "你好啊!我很好的。"); }
static void Main(string[] args) { P2Pclient client = new P2Pclient(false); client.receiveServerEvent += Client_receiveServerEvent; //接收服务端消息,也可以不用这么写 //client.AddListenClass(this);静态方法,麻烦,算了,还是正常写把 client.timeoutevent += Client_timeoutevent; //连接断开事件 client.start("127.0.0.1", 8989, true); client.SendRoot <String>(0x31, "login", "C/S测试", 0); //刚才忘了一件事情,这个逻辑要求,必须注册一下,才能收到内容。这句话的意思是注册一下。在刚才看到的那个部分。 Console.ReadKey(); }
static void Main(string[] args) { P2Pclient client = new P2Pclient(false); client.timeoutevent += Client_timeoutevent; client.receiveServerEvent += Client_receiveServerEvent; client.start("127.0.0.1", 8989, false); System.Threading.Thread.Sleep(1000); Console.WriteLine("server link OK:"); client.send(0x1, "test2017-5-5"); Console.WriteLine("send:test2017-5-5"); Console.ReadKey(); }
void tt() { for (int i = 0; i < 300; i++) { P2Pclient p2pc = new P2Pclient(false); p2pc.receiveServerEvent += P2pc_receiveServerEvent; //接收数据事件 p2pc.timeoutevent += P2pc_timeoutevent; //超时(掉线)事件 p2pc.start("127.0.0.1", 8989, false); //11002 是网关的端口号,刚才WEB网关占用了11001,我改成11002了 p2pc.Tokan = "123"; p2pc.SendRoot <int>(0x01, "login", 99987, 0); System.Threading.Thread.Sleep(5); } }
//ThreadSafeDictionary<string, ConnObj> ConnObjlist = new ThreadSafeDictionary<string, ConnObj>(); protected void p2psev_EventUpdataConnSoc(System.Net.Sockets.Socket soc) { ConnObj cobj = new ConnObj(); cobj.Soc = soc; IPEndPoint clientipe = (IPEndPoint)soc.RemoteEndPoint; cobj.Token = EncryptDES(clientipe.Address.ToString() + "|" + DateTime.Now.ToString(), "lllssscc"); try { if (p2psev.send(soc, 0xff, "token|" + cobj.Token + "")) { ConnObjlist.Add(cobj); } int len = ConnObjlist.Count / Proportion; foreach (CommandItem ci in CommandItemS) { if (len > ci.Client.Count) { P2Pclient p2p = new P2Pclient(false); p2p.receiveServerEvent += V_receiveServerEvent; p2p.timeoutevent += V_timeoutevent; p2p.ErrorMge += V_ErrorMge; if (p2p.start(ci.Ip, ci.Port, false)) { ci.Client.Add(p2p); } } } int count = CommandItemS.Count; CommandItem[] comItems = new CommandItem[count]; CommandItemS.CopyTo(0, comItems, 0, count); foreach (CommandItem ci in comItems) { ci.Client[0].send(0xff, "in|" + cobj.Token); } } catch (Exception ex) { if (EventMylog != null) { EventMylog("EventUpdataConnSoc", ex.Message); } } }
//int i = 0; void tt() { // timer1.Start(); for (int i = 0; i < 300; i++) { P2Pclient p2pc = new P2Pclient(DataType.custom); p2pc.ReceiveServerEvent += P2pc_receiveServerEvent; //接收数据事件 p2pc.ReceiveServerEventbit += P2pc_receiveServerEventbit; p2pc.Timeoutevent += P2pc_timeoutevent; //超时(掉线)事件 p2pc.Start("127.0.0.1", 8989, false); //11002 是网关的端口号,刚才WEB网关占用了11001,我改成11002了 p2pc.Send(new byte[1000]); //p2pc.SendRoot<int>(0x01, "login", 99987, 0); System.Threading.Thread.Sleep(5); } }
protected void ReloadFlies(object obj) { try { foreach (CommandItem ci in CommandItemS) { foreach (P2Pclient Client in ci.Client) { Client.stop(); } } CommandItemS.Clear(); XmlDocument xml = new XmlDocument(); xml.Load("node.xml"); foreach (XmlNode xn in xml.FirstChild.ChildNodes) { CommandItem ci = new CommandItem(); ci.Ip = xn.Attributes["ip"].Value; ci.Port = Convert.ToInt32(xn.Attributes["port"].Value); ci.CommName = byte.Parse(xn.Attributes["command"].Value); P2Pclient p2p = new P2Pclient(false); p2p.receiveServerEvent += (V_receiveServerEvent); p2p.timeoutevent += (V_timeoutevent); p2p.ErrorMge += (V_ErrorMge); if (p2p.start(ci.Ip, ci.Port, false)) { ci.Client.Add(p2p); CommandItemS.Add(ci); } else { if (EventMylog != null) { EventMylog("节点连接失败", "命令:" + ci.CommName + ":节点连接失败,抛弃此节点"); } } } } catch (Exception ex) { if (EventMylog != null) { EventMylog("加载异常", ex.Message); } } }
private void button1_Click_1(object sender, EventArgs e) { P2Pclient p2pc = new P2Pclient(false); p2pc.receiveServerEvent += P2pc_receiveServerEvent; //接收数据事件 p2pc.timeoutevent += P2pc_timeoutevent; //超时(掉线)事件 p2pc.start("127.0.0.1", 8989, false); //11002 是网关的端口号,刚才WEB网关占用了11001,我改成11002了 p2pc.Tokan = "123"; p2pc.SendRoot <int>(0x01, "login", 99987, 0); System.Threading.Thread.Sleep(5); // p2pc.Tokan = "123"; // p2pc.SendRoot<String>(0x01, "login", "999899987999879998799987999879998799987999879999989998799987999879998799987999879998799987999879998799987999879998799987999879998799987999879998799987999879998799987798799987999879999899987999879998799987999879998799987999879999989998799987999879998799987999879998799987999879998799987999879998799987999879998799987999879998799987999879998799987798799987999879998799987999879998799987999879998799987999879998799987999877999899987999879998799987999879998799987999879999989998799987999879998799987999879998799987999879998799987999879998799987999879998799987999879998799987999879998799987798799987999879998799987999879998799987999879998799987999879998799987999877998799987999879998799987999879998799987999879998799987999877", 0); //// p2pc.SendRoot<int>(0x01, "login", 34534534, 0); // System.Threading.Thread.Sleep(5); }
private void Client_timeoutobjevent(P2Pclient p2pobj) { try { if (p2pobj.Restart(false)) { Client_timeoutobjevent(p2pobj); System.Threading.Thread.Sleep(5000); } } catch (Exception ex) { if (EventMylog != null) { EventMylog("way节点重新连接", ex.Message); } Client_timeoutobjevent(p2pobj); System.Threading.Thread.Sleep(5000); } }
static void Main(string[] args) { modbus mod = new modbus(); mod.name = "aaa";//这里可以是设备的ID号; mod.ip = "116.255.252.181"; mod.port = 9903; modlist.Add(mod); foreach (modbus m in modlist) { P2Pclient client = new P2Pclient(DataType.bytes); client.ReceiveServerEventbitobj += Client_receiveServerEventbitobj; client.ReceiveServerEventobj += Client_ReceiveServerEventobj; client.Timeoutobjevent += Client_timeoutobjevent; if (client.Start(m.ip, m.port, false)) { m.client = client; } } System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(gogo)); t.Start(); }
public void test(object obj) { P2Pclient p2pc2 = new P2Pclient(false); p2pc2.timeoutevent += P2pc_timeoutevent; p2pc2.receiveServerEvent += new P2Pclient.receive(delegate (byte command, string text) { lock (this) { reccount = reccount + 1; } }); if (p2pc2.start("127.0.0.1", Convert.ToInt32(textBox1.Text), true)) { lock (this) { okcount = okcount + 1; } } else { lock (this) { errorcount = errorcount + 1; } } Ccontext c = new Ccontext(); c.Content = "张三去干活去。"; c.Recusername = "******"; c.Sendusername = "******"; //bm.SetParameter<Ccontext>(c); //向服务器发送数据 // p2pc.send((byte)0x01, bm.Getjson()); p2pc2.SendParameter <Ccontext>(0x01, "Send_content", c, 0); }
private static void Client_ReceiveServerEventobj(byte command, string text, P2Pclient soc) { Console.WriteLine(text); }
/// <summary> /// 收到客户端发来的消息,并转发到服务端中心 /// </summary> /// <param name="command"></param> /// <param name="data"></param> /// <param name="soc"></param> protected void p2psev_receiveevent(byte command, string data, System.Net.Sockets.Socket soc) { try { // JSON.parse<_baseModel>(data);// WeaveSession _0x01; try { _0x01 = Newtonsoft.Json.JsonConvert.DeserializeObject <WeaveSession>(data); } catch { EventMylog("JSON解析错误:", "" + data); return; } if (_0x01.Token == null) { EventMylog("Token是NULL:", "" + data); return; } IPEndPoint clientipe = (IPEndPoint)soc.RemoteEndPoint; int count = CommandItemS.Count; try { // temp = _0x01.Token.Split(':'); //if (temp.Length < 2) // return; _0x01.Token = clientipe.Address.ToString() + ":" + clientipe.Port; } catch (Exception e) { if (EventMylog != null) { EventMylog("p2psev_receiveevent", e.Message); } return; } foreach (CommandItem ci in CommandItemS) { if (ci != null) { if (ci.CommName == command) { P2Pclient[,,,] client4_10temp = ci.Client4_10; P2Pclient p2ptemp = GateHelper.GetP2Pclient(client4_10temp, soc, Pipeline); if (p2ptemp != null) { if (!p2ptemp.Isline) { p2psev.Send(soc, 0xff, "你所请求的服务暂不能使用,已断开连接!"); return; } if (!p2ptemp.send(command, _0x01.Getjson())) { p2psev.Send(soc, 0xff, "你所请求的服务暂不能使用,发送错误。"); } } } } } } catch (Exception ex) { if (EventMylog != null) { EventMylog("p2psev_receiveevent----", ex.Message); } } }
/// <summary> /// 收到客户端发来的Byte消息,并转发到服务端中心 /// </summary> /// <param name="command"></param> /// <param name="data"></param> /// <param name="soc"></param> private void P2psev_weaveReceiveBitEvent(byte command, byte[] data, Socket soc) { try { // JSON.parse<_baseModel>(data);// IPEndPoint clientipe = (IPEndPoint)soc.RemoteEndPoint; int count = CommandItemS.Count; try { // temp = _0x01.Token.Split(':'); //if (temp.Length < 2) // return; //_0x01.Token = clientipe.Address.ToString() + ":" + clientipe.Port; } catch (Exception e) { if (EventMylog != null) { EventMylog("p2psev_receiveevent", e.Message); } return; } foreach (CommandItem ci in CommandItemS) { if (ci != null) { if (ci.CommName == command) { P2Pclient[,,,] client4_10temp = ci.Client4_10; P2Pclient p2ptemp = GateHelper.GetP2Pclient(client4_10temp, soc, Pipeline); byte [] b = GateHelper.GetP2PclientIndex(client4_10temp, soc, Pipeline); if (p2ptemp != null) { if (!p2ptemp.Isline) { p2psev.Send(soc, 0xff, "你所请求的服务暂不能使用,已断开连接!"); return; } byte[] tempdata = new byte[data.Length + b.Length]; Array.Copy(data, 0, tempdata, 0, data.Length); Array.Copy(b, 0, tempdata, data.Length - 1, b.Length); if (!p2ptemp.send(command, tempdata)) { p2psev.Send(soc, 0xff, "你所请求的服务暂不能使用,发送错误。"); } } } } } } catch (Exception ex) { if (EventMylog != null) { EventMylog("p2psev_receiveevent----", ex.Message); } } }
protected void ReloadFlies(object obj) { try { CommandItemS.Clear(); XmlDocument xml = new XmlDocument(); xml.Load("node.xml"); List <String> iplist = new List <string>(); foreach (XmlNode xn in xml.FirstChild.ChildNodes) { string type = "json"; if (Wptype == WeavePortTypeEnum.Bytes) { type = "byte"; } if (type == xn.Attributes["type"].Value) { bool isok = true; CommandItem ci = new CommandItem(); ci.Ip = xn.Attributes["ip"].Value; ci.Port = Convert.ToInt32(xn.Attributes["port"].Value); ci.CommName = byte.Parse(xn.Attributes["command"].Value); CommandItemS.Add(ci); foreach (string s in iplist) { if (s == ci.Ip + ":" + ci.Port) { isok = false; } } if (isok) { iplist.Add(ci.Ip + ":" + ci.Port); } } } int countpipeline = iplist.Count * (int)pipeline; if (countpipeline > 60000) { if (EventMylog != null) { EventMylog("设置异常", "你所开启的通道不能超过6万,计算方法,不同的(IP+PORT)总数*通道数量。开启当前设置需要:" + countpipeline + "本地连接,此链接不被系统支持。"); } return; } foreach (string s in iplist) { P2Pclient[,,,] client4_10temp = new P2Pclient[10, 10, 10, 10]; for (int i = (int)pipeline; i > 1; i--) { String im = (i - 1).ToString(), temp = ""; for (int j = 0; j < 4 - im.Length; j++) { temp += "0"; } im = temp + im; System.Threading.Thread.Sleep(100); P2Pclient ct = newp2p(s.Split(':')[0], Convert.ToInt32(s.Split(':')[1])); //P2Pclient ct = new P2Pclient(false); client4_10temp[int.Parse(im.Substring(0, 1)), int.Parse(im.Substring(1, 1)), int.Parse(im.Substring(2, 1)), int.Parse(im.Substring(3, 1))] = ct; } client4_10temp[0, 0, 0, 0] = newp2p(s.Split(':')[0], Convert.ToInt32(s.Split(':')[1])); foreach (CommandItem ci in CommandItemS) { if (s == ci.Ip + ":" + ci.Port) { ci.Client4_10 = client4_10temp; } } } EventMylog("加载成功", "已完成通道加载。"); } catch (Exception ex) { if (EventMylog != null) { EventMylog("加载异常", ex.Message); } } }
public void test(object obj) { P2Pclient p2pc2 = new P2Pclient(false); p2pc2.timeoutevent += P2pc_timeoutevent; p2pc2.receiveServerEvent += new P2Pclient.receive(delegate (byte command, string text) { lock (this) { reccount = reccount + 1; } }); if (p2pc2.start("127.0.0.1", Convert.ToInt32(textBox1.Text), true)) { lock (this) { okcount = okcount + 1; } } else { lock (this) { errorcount = errorcount + 1; } } Ccontext c = new Ccontext(); c.Content = "张三去干活去。"; c.Recusername = "******"; c.Sendusername = "******"; //bm.SetParameter<Ccontext>(c); //向服务器发送数据 // p2pc.send((byte)0x01, bm.Getjson()); p2pc2.SendParameter<Ccontext>(0x01, "Send_content", c, 0); }
private static void Client_timeoutobjevent(P2Pclient p2pobj) { //断线重连 }
private static void Client_receiveServerEventbitobj(byte command, byte[] data, P2Pclient soc) { Console.WriteLine(System.Text.Encoding.UTF8.GetString(data)); }
protected void ReloadFlies2(object obj) { try { foreach (CommandItem ci in CommandItemS2) { foreach (P2Pclient Client in ci.Client) { Client.Stop(); } } CommandItemS2.Clear(); foreach (CommandItem ci in CommandItemS) { foreach (P2Pclient Client in ci.Client) { Client.Stop(); } } CommandItemS.Clear(); XmlDocument xml = new XmlDocument(); xml.Load(System.AppDomain.CurrentDomain.BaseDirectory + "/" + filename); foreach (XmlNode xn in xml.FirstChild.ChildNodes) { CommandItem ci = new CommandItem(); ci.Ip = xn.Attributes["ip"].Value; ci.Port = Convert.ToInt32(xn.Attributes["port"].Value); ci.CommName = byte.Parse(xn.Attributes["command"].Value); ci.Commfun = xn.Attributes["Commfun"].Value; P2Pclient p2p = new P2Pclient(false); p2p.ReceiveServerEvent += P2p_receiveServerEvent; p2p.Timeoutevent += P2p_timeoutevent; p2p.ErrorMge += P2p_ErrorMge; if (p2p.Start(ci.Ip, ci.Port, false)) { ci.Client.Add(p2p); if (xn.Attributes["type"].Value == "receive") { CommandItemS.Add(ci); } else if (xn.Attributes["type"].Value == "push") { CommandItemS2.Add(ci); } } else { if (EventMylog != null) { EventMylog("节点连接失败", "命令:" + ci.CommName + ":节点连接失败,抛弃此节点"); } } } } catch (Exception ex) { if (EventMylog != null) { EventMylog("加载异常", ex.Message); } } }