Refactor onTimeout to be (int, *ping.InFlightPacket)

This commit is contained in:
Hampton Moore
2021-08-27 12:12:02 -04:00
committed by thatmattlove
parent 58ebcd5b44
commit 46c548aa5a
2 changed files with 4 additions and 6 deletions

View File

@@ -77,8 +77,8 @@ func main() {
fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n",
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
}
pinger.OnTimeout = func(pkt *ping.InFlightPacket) {
fmt.Printf("TIMEOUT icmp_seq=%d time=%v\n", pkt.Seq, pkt.DispatchedTime)
pinger.OnTimeout = func(seq int, pkt *ping.InFlightPacket) {
fmt.Printf("TIMEOUT icmp_seq=%d time=%v\n", seq, pkt.DispatchedTime)
}
pinger.OnFinish = func(stats *ping.Statistics) {
fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr)

View File

@@ -178,7 +178,7 @@ type Pinger struct {
OnFinish func(*Statistics)
// OnTimeout is called when packet timeout
OnTimeout func(*InFlightPacket)
OnTimeout func(int, *InFlightPacket)
// OnDuplicateRecv is called when a packet is received that has already been received.
OnDuplicateRecv func(*Packet)
@@ -219,7 +219,6 @@ type Pinger struct {
type InFlightPacket struct {
DispatchedTime time.Time
Seq int
}
type packet struct {
@@ -531,7 +530,7 @@ func (p *Pinger) CheckInFlightPackets() {
if pkt.DispatchedTime.Add(p.PacketTimeout).Before(currentTime) {
delete(p.InFlightPackets, seq)
if p.OnTimeout != nil {
p.OnTimeout(&pkt)
p.OnTimeout(seq, &pkt)
}
}
}
@@ -790,7 +789,6 @@ func (p *Pinger) sendICMP(conn packetConn) error {
// mark this sequence as in-flight
p.InFlightPackets[currentUUID][p.sequence] = InFlightPacket{
DispatchedTime: time.Now(),
Seq: p.sequence,
}
p.PacketsSent++
p.sequence++