receiving.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package kcp
  2. type ReceivingWindow struct {
  3. start uint32
  4. size uint32
  5. list []*Segment
  6. }
  7. func NewReceivingWindow(size uint32) *ReceivingWindow {
  8. return &ReceivingWindow{
  9. start: 0,
  10. size: size,
  11. list: make([]*Segment, size),
  12. }
  13. }
  14. func (this *ReceivingWindow) Size() uint32 {
  15. return this.size
  16. }
  17. func (this *ReceivingWindow) Position(idx uint32) uint32 {
  18. return (idx + this.start) % this.size
  19. }
  20. func (this *ReceivingWindow) Set(idx uint32, value *Segment) bool {
  21. pos := this.Position(idx)
  22. if this.list[pos] != nil {
  23. return false
  24. }
  25. this.list[pos] = value
  26. return true
  27. }
  28. func (this *ReceivingWindow) Remove(idx uint32) *Segment {
  29. pos := this.Position(idx)
  30. e := this.list[pos]
  31. this.list[pos] = nil
  32. return e
  33. }
  34. func (this *ReceivingWindow) RemoveFirst() *Segment {
  35. return this.Remove(0)
  36. }
  37. func (this *ReceivingWindow) Advance() {
  38. this.start++
  39. if this.start == this.size {
  40. this.start = 0
  41. }
  42. }
  43. type ACKList struct {
  44. timestamps []uint32
  45. numbers []uint32
  46. }
  47. func (this *ACKList) Add(number uint32, timestamp uint32) {
  48. this.timestamps = append(this.timestamps, timestamp)
  49. this.numbers = append(this.numbers, number)
  50. }
  51. func (this *ACKList) Clear(una uint32) {
  52. count := 0
  53. for i := 0; i < len(this.numbers); i++ {
  54. if this.numbers[i] >= una {
  55. if i != count {
  56. this.numbers[count] = this.numbers[i]
  57. this.timestamps[count] = this.timestamps[i]
  58. }
  59. count++
  60. }
  61. }
  62. this.numbers = this.numbers[:count]
  63. this.timestamps = this.timestamps[:count]
  64. }
  65. func (this *ACKList) AsSegment() *ACKSegment {
  66. count := len(this.numbers)
  67. if count > 16 {
  68. count = 16
  69. }
  70. return &ACKSegment{
  71. Count: byte(count),
  72. NumberList: this.numbers[:count],
  73. TimestampList: this.timestamps[:count],
  74. }
  75. }