mirror of https://github.com/cbeuw/Cloak
Abandon the frameSeq wrapping
This commit is contained in:
parent
59c18437e4
commit
fb12f096d1
|
|
@ -25,14 +25,13 @@ import (
|
|||
// Stream.rev counts the amount of time the sequence number gets wrapped
|
||||
|
||||
type frameNode struct {
|
||||
seq uint32
|
||||
trueSeq uint64
|
||||
frame *Frame
|
||||
seq uint32
|
||||
frame *Frame
|
||||
}
|
||||
type sorterHeap []*frameNode
|
||||
|
||||
func (sh sorterHeap) Less(i, j int) bool {
|
||||
return sh[i].trueSeq < sh[j].trueSeq
|
||||
return sh[i].seq < sh[j].seq
|
||||
}
|
||||
func (sh sorterHeap) Len() int {
|
||||
return len(sh)
|
||||
|
|
@ -80,29 +79,10 @@ func (s *Stream) recvNewFrame() {
|
|||
|
||||
fs := &frameNode{
|
||||
f.Seq,
|
||||
0,
|
||||
f,
|
||||
}
|
||||
|
||||
if fs.seq < s.nextRecvSeq {
|
||||
// For the ease of demonstration, assume seq is uint8, i.e. it wraps around after 255
|
||||
// e.g. we are on rev=0 (wrap has not happened yet)
|
||||
// and we get the order of recv as 253 254 0 1
|
||||
// after 254, nextN should be 255, but 0 is received and 0 < 255
|
||||
// now 0 should have a trueSeq of 256
|
||||
if !s.wrapMode {
|
||||
// wrapMode is true when the latest seq is wrapped but nextN is not
|
||||
s.wrapMode = true
|
||||
}
|
||||
fs.trueSeq = uint64(1<<16*(s.rev+1)) + uint64(fs.seq) + 1
|
||||
// +1 because wrapped 0 should have trueSeq of 256 instead of 255
|
||||
// when this bit was run on 1, the trueSeq of 1 would become 256
|
||||
} else {
|
||||
fs.trueSeq = uint64(1<<16*s.rev) + uint64(fs.seq)
|
||||
// when this bit was run on 255, the trueSeq of 255 would be 255
|
||||
}
|
||||
heap.Push(&s.sh, fs)
|
||||
|
||||
// Keep popping from the heap until empty or to the point that the wanted seq was not received
|
||||
for len(s.sh) > 0 && s.sh[0].seq == s.nextRecvSeq {
|
||||
frame := heap.Pop(&s.sh).(*frameNode).frame
|
||||
|
|
@ -118,13 +98,9 @@ func (s *Stream) pushFrame(f *Frame) {
|
|||
s.sortedBufCh <- []byte{}
|
||||
return
|
||||
}
|
||||
|
||||
s.sortedBufCh <- f.Payload
|
||||
|
||||
s.nextRecvSeq += 1
|
||||
if s.nextRecvSeq == 0 {
|
||||
// when nextN is wrapped, wrapMode becomes false and rev+1
|
||||
s.rev += 1
|
||||
s.wrapMode = false
|
||||
if s.nextRecvSeq == 0 { // getting wrapped
|
||||
s.Close()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue