Send retry

This commit is contained in:
Josh Deprez 2024-03-30 16:43:14 +11:00
parent a1b0f072f7
commit d24fca67b8
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw

37
peer.go
View file

@ -10,6 +10,14 @@ import (
"gitea.drjosh.dev/josh/jrouter/aurp" "gitea.drjosh.dev/josh/jrouter/aurp"
) )
const (
// TODO: check these parameters
lastHeardFromTimer = 10 * time.Second
lastHeardFromRetryLimit = 10
sendRetryTimer = 10 * time.Second
sendRetryLimit = 5
)
type receiverState int type receiverState int
const ( const (
@ -43,6 +51,7 @@ func (p *peer) send(pkt aurp.Packet) (int, error) {
if _, err := pkt.WriteTo(&b); err != nil { if _, err := pkt.WriteTo(&b); err != nil {
return 0, err return 0, err
} }
log.Printf("Sending %T (len %d) to %v", pkt, b.Len(), p.raddr)
return p.conn.WriteToUDP(b.Bytes(), p.raddr) return p.conn.WriteToUDP(b.Bytes(), p.raddr)
} }
@ -51,17 +60,17 @@ func (p *peer) handle(ctx context.Context) error {
defer ticker.Stop() defer ticker.Stop()
lastHeardFrom := time.Now() lastHeardFrom := time.Now()
lastSend := time.Now()
sendRetries := 0
rstate := receiverStateUnconnected rstate := receiverStateUnconnected
sstate := senderStateUnconnected sstate := senderStateUnconnected
// Write an Open-Req packet // Write an Open-Req packet
n, err := p.send(p.tr.NewOpenReqPacket(nil)) if _, err := p.send(p.tr.NewOpenReqPacket(nil)); err != nil {
if err != nil {
log.Printf("Couldn't send Open-Req packet: %v", err) log.Printf("Couldn't send Open-Req packet: %v", err)
return err return err
} }
log.Printf("Sent Open-Req (len %d) to peer %v", n, p.raddr)
rstate = receiverStateWaitForOpenRsp rstate = receiverStateWaitForOpenRsp
@ -78,9 +87,27 @@ func (p *peer) handle(ctx context.Context) error {
return ctx.Err() return ctx.Err()
case <-ticker.C: case <-ticker.C:
if rstate == receiverStateConnected { switch rstate {
case receiverStateWaitForOpenRsp:
if time.Since(lastSend) <= sendRetryTimer {
break
}
if sendRetries >= sendRetryLimit {
log.Printf("Send retry limit reached while waiting for Open-Rsp, closing connection")
rstate = receiverStateUnconnected
break
}
// Send another Open-Req
sendRetries++
if _, err := p.send(p.tr.NewOpenReqPacket(nil)); err != nil {
log.Printf("Couldn't send Open-Req packet: %v", err)
return err
}
case receiverStateConnected:
// Check LHFT, send tickle? // Check LHFT, send tickle?
if time.Since(lastHeardFrom) > 10*time.Second { if time.Since(lastHeardFrom) > lastHeardFromTimer {
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)
} }