Refactor lastHeardFrom
This commit is contained in:
parent
6592189300
commit
c52a434909
1 changed files with 6 additions and 5 deletions
11
peer.go
11
peer.go
|
@ -15,8 +15,6 @@ type peer struct {
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
raddr *net.UDPAddr
|
raddr *net.UDPAddr
|
||||||
recv chan aurp.Packet
|
recv chan aurp.Packet
|
||||||
|
|
||||||
lastHeardFrom time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// send encodes and sends pkt to the remote host.
|
// send encodes and sends pkt to the remote host.
|
||||||
|
@ -32,7 +30,7 @@ func (p *peer) handle(ctx context.Context) error {
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
p.lastHeardFrom = time.Now()
|
lastHeardFrom := time.Now()
|
||||||
|
|
||||||
// Write an Open-Req packet
|
// Write an Open-Req packet
|
||||||
n, err := p.send(p.tr.NewOpenReqPacket(nil))
|
n, err := p.send(p.tr.NewOpenReqPacket(nil))
|
||||||
|
@ -54,13 +52,15 @@ func (p *peer) handle(ctx context.Context) error {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
// TODO: time-based state changes
|
// TODO: time-based state changes
|
||||||
// Check LHFT, send tickle?
|
// Check LHFT, send tickle?
|
||||||
if time.Since(p.lastHeardFrom) > 10*time.Second {
|
if time.Since(lastHeardFrom) > 10*time.Second {
|
||||||
if _, err := p.send(p.tr.NewTicklePacket()); err != nil {
|
if _, err := p.send(p.tr.NewTicklePacket()); err != nil {
|
||||||
log.Printf("Couldn't send Tickle: %v", err)
|
log.Printf("Couldn't send Tickle: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case pkt := <-p.recv:
|
case pkt := <-p.recv:
|
||||||
|
lastHeardFrom = time.Now()
|
||||||
|
|
||||||
switch pkt := pkt.(type) {
|
switch pkt := pkt.(type) {
|
||||||
case *aurp.AppleTalkPacket:
|
case *aurp.AppleTalkPacket:
|
||||||
// Probably something like:
|
// Probably something like:
|
||||||
|
@ -136,12 +136,13 @@ func (p *peer) handle(ctx context.Context) error {
|
||||||
// TODO: Integrate info into zone table
|
// TODO: Integrate info into zone table
|
||||||
|
|
||||||
case *aurp.TicklePacket:
|
case *aurp.TicklePacket:
|
||||||
|
// Immediately respond with Tickle-Ack
|
||||||
if _, err := p.send(p.tr.NewTickleAckPacket()); err != nil {
|
if _, err := p.send(p.tr.NewTickleAckPacket()); err != nil {
|
||||||
log.Printf("Couldn't send Tickle-Ack: %v", err)
|
log.Printf("Couldn't send Tickle-Ack: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
case *aurp.TickleAckPacket:
|
case *aurp.TickleAckPacket:
|
||||||
p.lastHeardFrom = time.Now()
|
// No need to do anything
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue