remove signal handler, add Stop method (#10)

* remove signal handler, add Stop method

* Added ICMP Packet ID to distinguish ICMP replies
This commit is contained in:
Xie Zhenye 2018-11-06 18:48:48 +08:00 committed by Cameron Sparr
parent ad2f544f6b
commit 667d0a66f2

14
ping.go
View File

@ -48,8 +48,6 @@ import (
"math"
"math/rand"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
@ -94,7 +92,6 @@ func NewPinger(addr string) (*Pinger, error) {
network: "udp",
ipv4: ipv4,
Size: timeSliceLength,
done: make(chan bool),
}, nil
}
@ -290,14 +287,9 @@ func (p *Pinger) run() {
timeout := time.NewTicker(p.Timeout)
interval := time.NewTicker(p.Interval)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
for {
select {
case <-c:
close(p.done)
case <-p.done:
wg.Wait()
return
@ -325,6 +317,10 @@ func (p *Pinger) run() {
}
}
func (p *Pinger) Stop() {
close(p.done)
}
func (p *Pinger) finish() {
handler := p.OnFinish
if handler != nil {
@ -436,7 +432,7 @@ func (p *Pinger) processPacket(recv *packet) error {
if body.ID != p.id {
return nil
}
outPkt := &Packet{
Nbytes: recv.nbytes,
IPAddr: p.ipaddr,