mirror of
https://github.com/go-ping/ping.git
synced 2025-06-25 05:31:37 +00:00
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:
parent
ad2f544f6b
commit
667d0a66f2
14
ping.go
14
ping.go
@ -48,8 +48,6 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@ -94,7 +92,6 @@ func NewPinger(addr string) (*Pinger, error) {
|
|||||||
network: "udp",
|
network: "udp",
|
||||||
ipv4: ipv4,
|
ipv4: ipv4,
|
||||||
Size: timeSliceLength,
|
Size: timeSliceLength,
|
||||||
|
|
||||||
done: make(chan bool),
|
done: make(chan bool),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -290,14 +287,9 @@ func (p *Pinger) run() {
|
|||||||
|
|
||||||
timeout := time.NewTicker(p.Timeout)
|
timeout := time.NewTicker(p.Timeout)
|
||||||
interval := time.NewTicker(p.Interval)
|
interval := time.NewTicker(p.Interval)
|
||||||
c := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(c, os.Interrupt)
|
|
||||||
signal.Notify(c, syscall.SIGTERM)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-c:
|
|
||||||
close(p.done)
|
|
||||||
case <-p.done:
|
case <-p.done:
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return
|
return
|
||||||
@ -325,6 +317,10 @@ func (p *Pinger) run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Pinger) Stop() {
|
||||||
|
close(p.done)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Pinger) finish() {
|
func (p *Pinger) finish() {
|
||||||
handler := p.OnFinish
|
handler := p.OnFinish
|
||||||
if handler != nil {
|
if handler != nil {
|
||||||
@ -436,7 +432,7 @@ func (p *Pinger) processPacket(recv *packet) error {
|
|||||||
if body.ID != p.id {
|
if body.ID != p.id {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
outPkt := &Packet{
|
outPkt := &Packet{
|
||||||
Nbytes: recv.nbytes,
|
Nbytes: recv.nbytes,
|
||||||
IPAddr: p.ipaddr,
|
IPAddr: p.ipaddr,
|
||||||
|
Loading…
Reference in New Issue
Block a user