github.com/AntonOrnatskyi/goproxy@v0.0.0-20190205095733-4526a9fa18b4/core/dst/packetlist.go (about) 1 // Copyright 2014 The DST Authors. All rights reserved. 2 // Use of this source code is governed by an MIT-style 3 // license that can be found in the LICENSE file. 4 5 package dst 6 7 type packetList struct { 8 packets []packet 9 slot int 10 } 11 12 // CutLessSeq cuts packets from the start of the list with sequence numbers 13 // lower than seq. Returns the number of packets that were cut. 14 func (l *packetList) CutLessSeq(seq sequenceNo) int { 15 var i, cut int 16 for i = range l.packets { 17 if i == l.slot { 18 break 19 } 20 if !l.packets[i].LessSeq(seq) { 21 break 22 } 23 cut++ 24 } 25 if cut > 0 { 26 l.Cut(cut) 27 } 28 return cut 29 } 30 31 func (l *packetList) Cut(n int) { 32 copy(l.packets, l.packets[n:]) 33 l.slot -= n 34 } 35 36 func (l *packetList) Full() bool { 37 return l.slot == len(l.packets) 38 } 39 40 func (l *packetList) All() []packet { 41 return l.packets[:l.slot] 42 } 43 44 func (l *packetList) Append(pkt packet) bool { 45 if l.slot == len(l.packets) { 46 return false 47 } 48 l.packets[l.slot] = pkt 49 l.slot++ 50 return true 51 } 52 53 func (l *packetList) AppendAll(pkts []packet) { 54 l.packets = append(l.packets[:l.slot], pkts...) 55 l.slot += len(pkts) 56 } 57 58 func (l *packetList) Cap() int { 59 return len(l.packets) 60 } 61 62 func (l *packetList) Len() int { 63 return l.slot 64 } 65 66 func (l *packetList) Resize(s int) { 67 if s <= cap(l.packets) { 68 l.packets = l.packets[:s] 69 } else { 70 t := make([]packet, s) 71 copy(t, l.packets) 72 l.packets = t 73 } 74 } 75 76 func (l *packetList) InsertSorted(pkt packet) { 77 for i := range l.packets { 78 if i >= l.slot { 79 l.packets[i] = pkt 80 l.slot++ 81 return 82 } 83 if pkt.hdr.sequenceNo == l.packets[i].hdr.sequenceNo { 84 return 85 } 86 if pkt.Less(l.packets[i]) { 87 copy(l.packets[i+1:], l.packets[i:]) 88 l.packets[i] = pkt 89 if l.slot < len(l.packets) { 90 l.slot++ 91 } 92 return 93 } 94 } 95 } 96 97 func (l *packetList) LowestSeq() sequenceNo { 98 return l.packets[0].hdr.sequenceNo 99 } 100 101 func (l *packetList) PopSequence(maxSeq sequenceNo) []packet { 102 highSeq := l.packets[0].hdr.sequenceNo 103 if highSeq >= maxSeq { 104 return nil 105 } 106 107 var i int 108 for i = 1; i < l.slot; i++ { 109 seq := l.packets[i].hdr.sequenceNo 110 if seq != highSeq+1 || seq >= maxSeq { 111 break 112 } 113 highSeq++ 114 } 115 pkts := make([]packet, i) 116 copy(pkts, l.packets[:i]) 117 l.Cut(i) 118 return pkts 119 }