private void UnreginsterEvents(CommuniPort cp) { SocketCommuniPort sckcp = cp as SocketCommuniPort; sckcp.ClosedEvent -= new EventHandler(sckcp_ClosedEvent); sckcp.ReceivedEvent -= new EventHandler(sckcp_ReceivedEvent); }
//新建连接事件 private void item_ConnectedEvent(object sender, EventArgs e) { SocketListener sl = sender as SocketListener; Socket newsocket = sl.NewSocket; if (newsocket == null) { return; } if (!newsocket.Connected) { return; } SocketCommuniPort scp = null; try { scp = new SocketCommuniPort(newsocket); } catch { CloseSocket(newsocket); return; } this.CommuniPortManager.Add(scp); }
//接收事件 private void sckcp_ReceivedEvent(object sender, EventArgs e) { SocketCommuniPort sckcp = sender as SocketCommuniPort; byte[] bytes = sckcp.Read(); string ip = ((IPEndPoint)sckcp.RemoteEndPoint).Address.ToString(); Tool.Gprs.Gprs_activate(ip); Tool.xd100n.Deal_XD100nData(bytes, ip); Tool.xd300.Deal_XD300Data(bytes, ip); Tool.xd100.Deal_XD100Data(bytes, ip); }
//关闭事件 void sckcp_ClosedEvent(object sender, EventArgs e) { SocketCommuniPort sckcp = sender as SocketCommuniPort; string ip = ((IPEndPoint)sckcp.RemoteEndPoint).Address.ToString(); for (int i = 0; i < Gprs._GprsList.Length; i++) { if (Gprs._GprsList[i]._ip == ip) { Gprs._GprsList[i]._socket = sckcp.Socket; Gprs._GprsList[i]._Iscon = false; Gprs._GprsList[i]._Isbusy = false; Gprs._GprsList[i]._activate = false; break; } } this.Remove((CommuniPort)sender); }
//删除集合中已经存在的,相同远程地址的SocketCommuniPort private void BeforeAdd(CommuniPort cp) { if (cp is SocketCommuniPort) { SocketCommuniPort scp = cp as SocketCommuniPort; for (int i = this.CommuniPorts.Count - 1; i >= 0; i--) { CommuniPort cp2 = this.CommuniPorts[i]; if (cp2 is SocketCommuniPort) { SocketCommuniPort scp2 = cp2 as SocketCommuniPort; IPEndPoint ipep = scp.Socket.RemoteEndPoint as IPEndPoint; IPEndPoint ipep2 = scp2.Socket.RemoteEndPoint as IPEndPoint; if (ipep.Address.Equals(ipep2.Address)) { Remove(cp2); } } } } }
//新建连接事件 private void RegisterEvents(CommuniPort cp) { SocketCommuniPort sckcp = cp as SocketCommuniPort; if (sckcp != null) { sckcp.ClosedEvent += new EventHandler(sckcp_ClosedEvent); sckcp.ReceivedEvent += new EventHandler(sckcp_ReceivedEvent); string ip = ((IPEndPoint)sckcp.RemoteEndPoint).Address.ToString(); for (int i = 0; i < Gprs._GprsList.Length; i++) { if (Gprs._GprsList[i]._ip == ip) { Gprs._GprsList[i]._socket = sckcp.Socket; Gprs._GprsList[i]._Iscon = true; Gprs._GprsList[i]._Isbusy = false; //测试 Gprs._GprsList[i]._activate = true; break; } } } }