kappanhang/txseqbuf.go

47 lines
1.2 KiB
Go
Raw Normal View History

2020-10-25 20:18:24 +01:00
package main
import "time"
2020-10-29 22:30:42 +01:00
// This value is sent to the transceiver and - according to my observations - it will use
// this is it's RX buf length. Note that if it is set to larger than 500-600ms then audio TX
// won't work (small radio memory?)
2020-10-29 17:05:40 +01:00
const txSeqBufLength = 300 * time.Millisecond
2020-10-25 20:18:24 +01:00
type txSeqBufStruct struct {
entries []seqBufEntry
}
func (s *txSeqBufStruct) add(seq seqNum, p []byte) {
s.entries = append(s.entries, seqBufEntry{
seq: seq,
data: p,
addedAt: time.Now(),
})
s.purgeOldEntries()
}
func (s *txSeqBufStruct) purgeOldEntries() {
2020-10-29 22:30:42 +01:00
// We keep much more entries than the specified length of the TX seqbuf, so we can serve
// any requests coming from the server.
for len(s.entries) > 0 && time.Since(s.entries[0].addedAt) > txSeqBufLength*10 {
2020-10-25 20:18:24 +01:00
s.entries = s.entries[1:]
}
}
func (s *txSeqBufStruct) get(seq seqNum) (d []byte) {
if len(s.entries) == 0 {
return nil
}
// Searching from backwards, as we expect most queries for latest entries.
for i := len(s.entries) - 1; i >= 0; i-- {
if s.entries[i].seq == seq {
2020-10-29 22:31:30 +01:00
d = make([]byte, len(s.entries[i].data))
2020-10-29 22:30:42 +01:00
copy(d, s.entries[i].data)
2020-10-25 20:18:24 +01:00
break
}
}
s.purgeOldEntries()
return d
}