Skip to content

Commit

Permalink
use a generic linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Aug 2, 2022
1 parent 936a585 commit 0210635
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 1,080 deletions.
15 changes: 11 additions & 4 deletions conn_id_manager.go
Expand Up @@ -6,11 +6,18 @@ import (
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/qerr"
"github.com/lucas-clemente/quic-go/internal/utils"
list "github.com/lucas-clemente/quic-go/internal/utils/linkedlist"
"github.com/lucas-clemente/quic-go/internal/wire"
)

type newConnID struct {
SequenceNumber uint64
ConnectionID protocol.ConnectionID
StatelessResetToken protocol.StatelessResetToken
}

type connIDManager struct {
queue utils.NewConnectionIDList
queue list.List[newConnID]

handshakeComplete bool
activeSequenceNumber uint64
Expand Down Expand Up @@ -71,7 +78,7 @@ func (h *connIDManager) add(f *wire.NewConnectionIDFrame) error {
// Retire elements in the queue.
// Doesn't retire the active connection ID.
if f.RetirePriorTo > h.highestRetired {
var next *utils.NewConnectionIDElement
var next *list.Element[newConnID]
for el := h.queue.Front(); el != nil; el = next {
if el.Value.SequenceNumber >= f.RetirePriorTo {
break
Expand Down Expand Up @@ -104,7 +111,7 @@ func (h *connIDManager) add(f *wire.NewConnectionIDFrame) error {
func (h *connIDManager) addConnectionID(seq uint64, connID protocol.ConnectionID, resetToken protocol.StatelessResetToken) error {
// insert a new element at the end
if h.queue.Len() == 0 || h.queue.Back().Value.SequenceNumber < seq {
h.queue.PushBack(utils.NewConnectionID{
h.queue.PushBack(newConnID{
SequenceNumber: seq,
ConnectionID: connID,
StatelessResetToken: resetToken,
Expand All @@ -123,7 +130,7 @@ func (h *connIDManager) addConnectionID(seq uint64, connID protocol.ConnectionID
break
}
if el.Value.SequenceNumber > seq {
h.queue.InsertBefore(utils.NewConnectionID{
h.queue.InsertBefore(newConnID{
SequenceNumber: seq,
ConnectionID: connID,
StatelessResetToken: resetToken,
Expand Down
22 changes: 14 additions & 8 deletions frame_sorter.go
Expand Up @@ -4,9 +4,15 @@ import (
"errors"

"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
list "github.com/lucas-clemente/quic-go/internal/utils/linkedlist"
)

// byteInterval is an interval from one ByteCount to the other
type byteInterval struct {
Start protocol.ByteCount
End protocol.ByteCount
}

type frameSorterEntry struct {
Data []byte
DoneCb func()
Expand All @@ -15,17 +21,17 @@ type frameSorterEntry struct {
type frameSorter struct {
queue map[protocol.ByteCount]frameSorterEntry
readPos protocol.ByteCount
gaps *utils.ByteIntervalList
gaps *list.List[byteInterval]
}

var errDuplicateStreamData = errors.New("duplicate stream data")

func newFrameSorter() *frameSorter {
s := frameSorter{
gaps: utils.NewByteIntervalList(),
gaps: list.New[byteInterval](),
queue: make(map[protocol.ByteCount]frameSorterEntry),
}
s.gaps.PushFront(utils.ByteInterval{Start: 0, End: protocol.MaxByteCount})
s.gaps.PushFront(byteInterval{Start: 0, End: protocol.MaxByteCount})
return &s
}

Expand Down Expand Up @@ -118,7 +124,7 @@ func (s *frameSorter) push(data []byte, offset protocol.ByteCount, doneCb func()

if !startGapEqualsEndGap {
s.deleteConsecutive(startGapEnd)
var nextGap *utils.ByteIntervalElement
var nextGap *list.Element[byteInterval]
for gap := startGapNext; gap.Value.End < endGapStart; gap = nextGap {
nextGap = gap.Next()
s.deleteConsecutive(gap.Value.End)
Expand All @@ -140,7 +146,7 @@ func (s *frameSorter) push(data []byte, offset protocol.ByteCount, doneCb func()
} else {
if startGapEqualsEndGap && adjustedStartGapEnd {
// The frame split the existing gap into two.
s.gaps.InsertAfter(utils.ByteInterval{Start: end, End: startGapEnd}, startGap)
s.gaps.InsertAfter(byteInterval{Start: end, End: startGapEnd}, startGap)
} else if !startGapEqualsEndGap {
endGap.Value.Start = end
}
Expand All @@ -164,7 +170,7 @@ func (s *frameSorter) push(data []byte, offset protocol.ByteCount, doneCb func()
return nil
}

func (s *frameSorter) findStartGap(offset protocol.ByteCount) (*utils.ByteIntervalElement, bool) {
func (s *frameSorter) findStartGap(offset protocol.ByteCount) (*list.Element[byteInterval], bool) {
for gap := s.gaps.Front(); gap != nil; gap = gap.Next() {
if offset >= gap.Value.Start && offset <= gap.Value.End {
return gap, true
Expand All @@ -176,7 +182,7 @@ func (s *frameSorter) findStartGap(offset protocol.ByteCount) (*utils.ByteInterv
panic("no gap found")
}

func (s *frameSorter) findEndGap(startGap *utils.ByteIntervalElement, offset protocol.ByteCount) (*utils.ByteIntervalElement, bool) {
func (s *frameSorter) findEndGap(startGap *list.Element[byteInterval], offset protocol.ByteCount) (*list.Element[byteInterval], bool) {
for gap := startGap; gap != nil; gap = gap.Next() {
if offset >= gap.Value.Start && offset < gap.Value.End {
return gap, true
Expand Down

0 comments on commit 0210635

Please sign in to comment.