receiving.go 961 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if this.list[pos] == nil {
  31. return nil
  32. }
  33. e := this.list[pos]
  34. this.list[pos] = nil
  35. return e
  36. }
  37. func (this *ReceivingWindow) RemoveFirst() *Segment {
  38. return this.Remove(0)
  39. }
  40. func (this *ReceivingWindow) Advance() {
  41. this.start++
  42. if this.start == this.size {
  43. this.start = 0
  44. }
  45. }