File: txseqbuf.go

package info (click to toggle)
kappanhang 1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,524 kB
  • sloc: makefile: 12
file content (52 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main

import "time"

// This value is sent to the transceiver and - according to my observations - it will use
// this as 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?)
const txSeqBufLength = 300 * time.Millisecond

type txSeqBufEntry struct {
	seq     seqNum
	data    []byte
	addedAt time.Time
}

type txSeqBufStruct struct {
	entries []txSeqBufEntry
}

func (s *txSeqBufStruct) add(seq seqNum, p []byte) {
	s.entries = append(s.entries, txSeqBufEntry{
		seq:     seq,
		data:    p,
		addedAt: time.Now(),
	})
	s.purgeOldEntries()
}

func (s *txSeqBufStruct) purgeOldEntries() {
	// 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 {
		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 {
			d = make([]byte, len(s.entries[i].data))
			copy(d, s.entries[i].data)
			break
		}
	}
	s.purgeOldEntries()
	return d
}