// ~WindowShowEllipseInfo() // { // MessageBox.Show("回收了"); // //还是没有回收,程序完全关闭才回收,待解决 // } private int CountProtocols(PeerMapPeerInfo peerInfo) { int i = 0; Dictionary <IPAddress, PeerToPeer> tempPeerToPeers = new Dictionary <IPAddress, PeerToPeer>(peerInfo.peerToPeers); //tempPeerToPeers = peerInfo.peerToPeers; List <IPProtocolType> list = new List <IPProtocolType>(); foreach (PeerToPeer peertopeer in tempPeerToPeers.Values) { foreach (IPProtocolType type in peertopeer.protocolPkInfos.Keys) { if (!list.Contains(type)) { list.Add(type); i++; } } } return(i); }
public WindowShowEllipseInfo(Ellipse e, PeerMapShow _uc) { InitializeComponent(); //this.ShowInTaskbar = false; uc = _uc; el = e; timer = new DispatcherTimer(); timer.Interval = new System.TimeSpan(0, 0, 1); timer.Tick += new EventHandler(timer_Tick); timer.Start(); elInfo = (EllipseInfo)el.Tag; ip = elInfo.ip; peerInfo = uc.ipAndPeerInfos[ip]; tbIP.Text = ip.ToString(); sentPackets.Text = peerInfo.allSendPkNum.ToString(); sentBytes.Text = peerInfo.allSendPkSize.ToString(); sentPercent.Text = ((double)peerInfo.allSendPkSize * 100 / (double)uc.totalPkSize).ToString("0.000"); rcvPackets.Text = peerInfo.allrcvPkNum.ToString(); rcvBytes.Text = peerInfo.allrcvPkSize.ToString(); rcvPercent.Text = ((double)peerInfo.allrcvPkSize * 100 / (double)uc.totalPkSize).ToString("0.000"); peers.Text = peerInfo.peerToPeers.Count.ToString(); protocols.Text = CountProtocols(peerInfo).ToString(); }
private void BackgroundThread() { while (!BackgroundThreadStop) { bool shouldSleep = true; lock (QueueLock) { if (PacketQueue.Count != 0) { shouldSleep = false; } } if (shouldSleep) { System.Threading.Thread.Sleep(250); } else // should process the queue { List <RawCapture> ourQueue; lock (QueueLock) { // swap queues, giving the capture callback a new one ourQueue = PacketQueue; PacketQueue = new List <RawCapture>(); } foreach (var packet in ourQueue) { pkSize = packet.Data.Length + 4; //数据帧的大小 pk = Packet.ParsePacket(packet.LinkLayerType, packet.Data); totalPkSize += pkSize; //所有捕获到的数据包大小 totalPkNum++; //所有捕获到的数据包数目 ipPacket = pk.Extract(typeof(IpPacket)) as IpPacket; ipProtocol = ipPacket.Protocol; srcIP = ipPacket.SourceAddress; dstIP = ipPacket.DestinationAddress; hasSrcIP = ipAndPeerInfos.ContainsKey(srcIP); hasDstIP = ipAndPeerInfos.ContainsKey(dstIP); if (hasSrcIP && hasDstIP) // 窗口已经有这两个点了 { #region Linq // var peerInfo = from PeerMapPeerInfo info in peerMapPeerInfos // where info.localIP == srcIP // select info; // int ii = peerInfo.Count(); #endregion //通过把已经划过的线的源目的地址字符串加入drawedLine,来代表已经划过此线。 // 当 A 连接 到B,B 连接到C ,一旦出现A 连接到C,就要进行此判断,否则不会再画线。 if (!drawedLine.Contains(srcIP.ToString() + dstIP.ToString())) { lock (tempEllipseLineLock) { drawLine.Add(new LineSrcDst() { _srcip = srcIP, _dstip = dstIP }); } } } else if (!hasSrcIP && !hasDstIP) // 窗口没有这两个点,需要添加,同时要在pointIPs中添加 { peerInfo = new PeerMapPeerInfo(srcIP); ipAndPeerInfos.Add(srcIP, peerInfo); peerInfo = new PeerMapPeerInfo(dstIP); ipAndPeerInfos.Add(dstIP, peerInfo); drawedLine.Add(srcIP.ToString() + dstIP.ToString()); drawedLine.Add(dstIP.ToString() + srcIP.ToString()); lock (tempEllipseLineLock) { listIP.Add(new LineSrcDst() { _srcip = srcIP, _dstip = dstIP }); listIP.Add(new LineSrcDst() { _srcip = dstIP, _dstip = srcIP }); drawLine.Add(new LineSrcDst() { _srcip = srcIP, _dstip = dstIP }); } } else if (hasSrcIP && !hasDstIP) // 有srcip 没有 dstip { peerInfo = new PeerMapPeerInfo(dstIP); ipAndPeerInfos.Add(dstIP, peerInfo); drawedLine.Add(srcIP.ToString() + dstIP.ToString()); drawedLine.Add(dstIP.ToString() + srcIP.ToString()); lock (tempEllipseLineLock) { listIP.Add(new LineSrcDst() { _srcip = dstIP, _dstip = srcIP }); drawLine.Add(new LineSrcDst() { _srcip = srcIP, _dstip = dstIP }); } } else // 有dstip没有 srcip { peerInfo = new PeerMapPeerInfo(srcIP); ipAndPeerInfos.Add(srcIP, peerInfo); drawedLine.Add(srcIP.ToString() + dstIP.ToString()); drawedLine.Add(dstIP.ToString() + srcIP.ToString()); lock (tempEllipseLineLock) { listIP.Add(new LineSrcDst() { _srcip = srcIP, _dstip = dstIP }); drawLine.Add(new LineSrcDst() { _srcip = srcIP, _dstip = dstIP }); } } ipAndPeerInfos[srcIP].PkFromHere(dstIP, ipProtocol, pkSize); ipAndPeerInfos[dstIP].PkFromHere(srcIP, ipProtocol, 0); ipAndPeerInfos[dstIP].allrcvPkNum++; ipAndPeerInfos[dstIP].allrcvPkSize += pkSize; } } } }