sending.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package kcp
  2. type SendingQueue struct {
  3. start uint32
  4. cap uint32
  5. len uint32
  6. list []*Segment
  7. }
  8. func NewSendingQueue(size uint32) *SendingQueue {
  9. return &SendingQueue{
  10. start: 0,
  11. cap: size,
  12. list: make([]*Segment, size),
  13. len: 0,
  14. }
  15. }
  16. func (this *SendingQueue) IsFull() bool {
  17. return this.len == this.cap
  18. }
  19. func (this *SendingQueue) IsEmpty() bool {
  20. return this.len == 0
  21. }
  22. func (this *SendingQueue) Pop() *Segment {
  23. if this.IsEmpty() {
  24. return nil
  25. }
  26. seg := this.list[this.start]
  27. this.list[this.start] = nil
  28. this.len--
  29. this.start++
  30. if this.start == this.cap {
  31. this.start = 0
  32. }
  33. return seg
  34. }
  35. func (this *SendingQueue) Push(seg *Segment) {
  36. if this.IsFull() {
  37. return
  38. }
  39. this.list[(this.start+this.len)%this.cap] = seg
  40. this.len++
  41. }
  42. func (this *SendingQueue) Clear() {
  43. for i := uint32(0); i < this.len; i++ {
  44. this.list[(i+this.start)%this.cap].Release()
  45. this.list[(i+this.start)%this.cap] = nil
  46. }
  47. this.start = 0
  48. this.len = 0
  49. }
  50. func (this *SendingQueue) Len() uint32 {
  51. return this.len
  52. }