diff --git a/packetmap/packetmap.go b/packetmap/packetmap.go index 83f79f7..e8f0627 100644 --- a/packetmap/packetmap.go +++ b/packetmap/packetmap.go @@ -69,14 +69,7 @@ func (m *Map) reset() { func addMapping(m *Map, seqno, pid uint16, delta, pidDelta uint16) { if len(m.entries) == 0 { - m.entries = []entry{ - entry{ - first: seqno, - count: 1, - delta: delta, - pidDelta: pidDelta, - }, - } + // this shouldn't happen return } @@ -86,9 +79,19 @@ func addMapping(m *Map, seqno, pid uint16, delta, pidDelta uint16) { return } + f := seqno + d := m.entries[i].delta - delta + // expand the interval to cover missing values, but make sure the + // targets don't overlap. + if d < 8192 { + ff := m.entries[i].first + m.entries[i].count + d + if compare(ff, seqno) < 0 { + f = ff + } + } e := entry{ - first: seqno, - count: 1, + first: f, + count: seqno - f + 1, delta: delta, pidDelta: pidDelta, } @@ -106,7 +109,7 @@ func addMapping(m *Map, seqno, pid uint16, delta, pidDelta uint16) { // direct maps a seqno to a target seqno. It returns true if the seqno // could be mapped, the target seqno, and the pid delta to apply. -// Called with the m.mu taken. +// Called with m.mu taken. func (m *Map) direct(seqno uint16) (bool, uint16, uint16) { if len(m.entries) == 0 { return false, 0, 0 @@ -184,6 +187,17 @@ func (m *Map) Drop(seqno uint16, pid uint16) bool { return false } + if len(m.entries) == 0 { + m.entries = []entry{ + entry{ + first: seqno - 8192, + count: 8192, + delta: 0, + pidDelta: 0, + }, + } + } + m.pidDelta += pid - m.nextPid m.nextPid = pid diff --git a/packetmap/packetmap_test.go b/packetmap/packetmap_test.go index e0d69a3..4cd266f 100644 --- a/packetmap/packetmap_test.go +++ b/packetmap/packetmap_test.go @@ -33,6 +33,30 @@ func TestNoDrops(t *testing.T) { } } +func TestReorder(t *testing.T) { + m := Map{} + + ok, s, p := m.Map(42, 1001) + if !ok || s != 42 || p != 0 { + t.Errorf("Expected 42, 0, got %v, %v, %v", ok, s, p) + } + + ok = m.Drop(43, 1002) + if !ok { + t.Errorf("Expected ok") + } + + ok, s, p = m.Map(45, 1003) + if !ok || s != 44 || p != 1 { + t.Errorf("Expected 44, 1, got %v, %v, %v", ok, s, p) + } + + ok, s, p = m.Map(44, 1003) + if !ok || s != 43 || p != 1 { + t.Errorf("Expected 43, 0, got %v, %v, %v", ok, s, p) + } +} + func TestDrop(t *testing.T) { m := Map{} @@ -82,8 +106,8 @@ func TestDrop(t *testing.T) { } ok, s, p = m.Map(13, 1000) - if ok { - t.Errorf("Expected not ok") + if !ok || s != 13 || p != 0 { + t.Errorf("Expected 13, 0, got %v, %v, %v", ok, s, p) } ok, s, p = m.Map(44, 1001) @@ -107,8 +131,8 @@ func TestDrop(t *testing.T) { } ok, s, p = m.direct(13) - if ok { - t.Errorf("Expected not ok") + if !ok || s != 13 || p != 0 { + t.Errorf("Expected 13, 0, got %v, %v, %v", ok, s, p) } ok, s, p = m.Reverse(44)