receiving.go 917 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. }