mirror of
				https://github.com/taigrr/go-fastping
				synced 2025-01-18 05:03:15 -08:00 
			
		
		
		
	Move sent addresses variable to Pinger struct
This commit is contained in:
		
							parent
							
								
									42755b085b
								
							
						
					
					
						commit
						1f74bfd6d1
					
				
							
								
								
									
										32
									
								
								fastping.go
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								fastping.go
									
									
									
									
									
								
							@ -134,6 +134,7 @@ type Pinger struct {
 | 
				
			|||||||
	seq int
 | 
						seq int
 | 
				
			||||||
	// key string is IPAddr.String()
 | 
						// key string is IPAddr.String()
 | 
				
			||||||
	addrs   map[string]*net.IPAddr
 | 
						addrs   map[string]*net.IPAddr
 | 
				
			||||||
 | 
						sent    map[string]*net.IPAddr
 | 
				
			||||||
	network string
 | 
						network string
 | 
				
			||||||
	source  string
 | 
						source  string
 | 
				
			||||||
	source6 string
 | 
						source6 string
 | 
				
			||||||
@ -442,7 +443,7 @@ func (p *Pinger) run(once bool) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	p.debugln("Run(): call sendICMP()")
 | 
						p.debugln("Run(): call sendICMP()")
 | 
				
			||||||
	queue, err := p.sendICMP(conn, conn6)
 | 
						err := p.sendICMP(conn, conn6)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ticker := time.NewTicker(p.MaxRTT)
 | 
						ticker := time.NewTicker(p.MaxRTT)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -469,10 +470,10 @@ mainloop:
 | 
				
			|||||||
				break mainloop
 | 
									break mainloop
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			p.debugln("Run(): call sendICMP()")
 | 
								p.debugln("Run(): call sendICMP()")
 | 
				
			||||||
			queue, err = p.sendICMP(conn, conn6)
 | 
								err = p.sendICMP(conn, conn6)
 | 
				
			||||||
		case r := <-recv:
 | 
							case r := <-recv:
 | 
				
			||||||
			p.debugln("Run(): <-recv")
 | 
								p.debugln("Run(): <-recv")
 | 
				
			||||||
			p.procRecv(r, queue)
 | 
								p.procRecv(r)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -487,7 +488,7 @@ mainloop:
 | 
				
			|||||||
	close(recv)
 | 
						close(recv)
 | 
				
			||||||
	for r := range recv {
 | 
						for r := range recv {
 | 
				
			||||||
		p.debugln("Run(): <-recv")
 | 
							p.debugln("Run(): <-recv")
 | 
				
			||||||
		p.procRecv(r, queue)
 | 
							p.procRecv(r)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	p.mu.Lock()
 | 
						p.mu.Lock()
 | 
				
			||||||
@ -499,7 +500,7 @@ mainloop:
 | 
				
			|||||||
	p.debugln("Run(): End")
 | 
						p.debugln("Run(): End")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *Pinger) sendICMP(conn, conn6 *icmp.PacketConn) (map[string]*net.IPAddr, error) {
 | 
					func (p *Pinger) sendICMP(conn, conn6 *icmp.PacketConn) error {
 | 
				
			||||||
	type sendResult struct {
 | 
						type sendResult struct {
 | 
				
			||||||
		addr *net.IPAddr
 | 
							addr *net.IPAddr
 | 
				
			||||||
		err  error
 | 
							err  error
 | 
				
			||||||
@ -510,10 +511,9 @@ func (p *Pinger) sendICMP(conn, conn6 *icmp.PacketConn) (map[string]*net.IPAddr,
 | 
				
			|||||||
	p.mu.Lock()
 | 
						p.mu.Lock()
 | 
				
			||||||
	p.id = rand.Intn(0xffff)
 | 
						p.id = rand.Intn(0xffff)
 | 
				
			||||||
	p.seq = rand.Intn(0xffff)
 | 
						p.seq = rand.Intn(0xffff)
 | 
				
			||||||
 | 
						p.sent = make(map[string]*net.IPAddr)
 | 
				
			||||||
	p.mu.Unlock()
 | 
						p.mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	queue := make(map[string]*net.IPAddr)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	addrs := make(chan *net.IPAddr)
 | 
						addrs := make(chan *net.IPAddr)
 | 
				
			||||||
	results := make(chan sendResult, 1)
 | 
						results := make(chan sendResult, 1)
 | 
				
			||||||
	errors := make(chan []error)
 | 
						errors := make(chan []error)
 | 
				
			||||||
@ -524,7 +524,9 @@ func (p *Pinger) sendICMP(conn, conn6 *icmp.PacketConn) (map[string]*net.IPAddr,
 | 
				
			|||||||
			if r.err != nil {
 | 
								if r.err != nil {
 | 
				
			||||||
				errs = append(errs, r.err)
 | 
									errs = append(errs, r.err)
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				queue[r.addr.String()] = r.addr
 | 
									p.mu.Lock()
 | 
				
			||||||
 | 
									p.sent[r.addr.String()] = r.addr
 | 
				
			||||||
 | 
									p.mu.Unlock()
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		errors <- errs
 | 
							errors <- errs
 | 
				
			||||||
@ -611,9 +613,9 @@ func (p *Pinger) sendICMP(conn, conn6 *icmp.PacketConn) (map[string]*net.IPAddr,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	p.debugln("sendICMP(): End")
 | 
						p.debugln("sendICMP(): End")
 | 
				
			||||||
	if len(errs) > 0 {
 | 
						if len(errs) > 0 {
 | 
				
			||||||
		return queue, errs[0]
 | 
							return errs[0]
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return queue, nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *Pinger) recvICMP(conn *icmp.PacketConn, recv chan<- *packet, ctx *context, wg *sync.WaitGroup) {
 | 
					func (p *Pinger) recvICMP(conn *icmp.PacketConn, recv chan<- *packet, ctx *context, wg *sync.WaitGroup) {
 | 
				
			||||||
@ -664,7 +666,7 @@ func (p *Pinger) recvICMP(conn *icmp.PacketConn, recv chan<- *packet, ctx *conte
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *Pinger) procRecv(recv *packet, queue map[string]*net.IPAddr) {
 | 
					func (p *Pinger) procRecv(recv *packet) {
 | 
				
			||||||
	var ipaddr *net.IPAddr
 | 
						var ipaddr *net.IPAddr
 | 
				
			||||||
	switch adr := recv.addr.(type) {
 | 
						switch adr := recv.addr.(type) {
 | 
				
			||||||
	case *net.IPAddr:
 | 
						case *net.IPAddr:
 | 
				
			||||||
@ -721,14 +723,16 @@ func (p *Pinger) procRecv(recv *packet, queue map[string]*net.IPAddr) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if _, ok := queue[addr]; ok {
 | 
						p.mu.Lock()
 | 
				
			||||||
		delete(queue, addr)
 | 
						if _, ok := p.sent[addr]; ok {
 | 
				
			||||||
		p.mu.Lock()
 | 
							delete(p.sent, addr)
 | 
				
			||||||
		handler := p.OnRecv
 | 
							handler := p.OnRecv
 | 
				
			||||||
		p.mu.Unlock()
 | 
							p.mu.Unlock()
 | 
				
			||||||
		if handler != nil {
 | 
							if handler != nil {
 | 
				
			||||||
			handler(ipaddr, rtt)
 | 
								handler(ipaddr, rtt)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							p.mu.Unlock()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user