sending.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package kcp
  2. type SendingWindow struct {
  3. start uint32
  4. cap uint32
  5. len uint32
  6. last uint32
  7. data []*DataSegment
  8. prev []uint32
  9. next []uint32
  10. kcp *KCP
  11. }
  12. func NewSendingWindow(kcp *KCP, size uint32) *SendingWindow {
  13. window := &SendingWindow{
  14. start: 0,
  15. cap: size,
  16. len: 0,
  17. last: 0,
  18. data: make([]*DataSegment, size),
  19. prev: make([]uint32, size),
  20. next: make([]uint32, size),
  21. }
  22. return window
  23. }
  24. func (this *SendingWindow) Len() int {
  25. return int(this.len)
  26. }
  27. func (this *SendingWindow) Push(seg *DataSegment) {
  28. pos := (this.start + this.len) % this.cap
  29. this.data[pos] = seg
  30. if this.len > 0 {
  31. this.next[this.last] = pos
  32. this.prev[pos] = this.last
  33. }
  34. this.last = pos
  35. this.len++
  36. }
  37. func (this *SendingWindow) First() *DataSegment {
  38. return this.data[this.start]
  39. }
  40. func (this *SendingWindow) Clear(una uint32) {
  41. for this.Len() > 0 {
  42. if this.data[this.start].Number < una {
  43. this.Remove(0)
  44. }
  45. }
  46. }
  47. func (this *SendingWindow) Remove(idx uint32) {
  48. pos := (this.start + idx) % this.cap
  49. seg := this.data[pos]
  50. seg.Release()
  51. this.data[pos] = nil
  52. if pos == this.start {
  53. if this.len == 1 {
  54. this.len = 0
  55. this.start = 0
  56. this.last = 0
  57. } else {
  58. delta := this.next[pos] - this.start
  59. this.start = this.next[pos]
  60. this.len -= delta
  61. }
  62. } else if pos == this.last {
  63. this.last = this.prev[pos]
  64. } else {
  65. this.next[this.prev[pos]] = this.next[pos]
  66. this.prev[this.next[pos]] = this.prev[pos]
  67. }
  68. }
  69. func (this *SendingWindow) HandleFastAck(number uint32) {
  70. for i := this.start; ; i = this.next[i] {
  71. seg := this.data[i]
  72. if _itimediff(number, seg.Number) < 0 {
  73. break
  74. }
  75. if number != seg.Number {
  76. seg.ackSkipped++
  77. }
  78. if i == this.last {
  79. break
  80. }
  81. }
  82. }
  83. func (this *SendingWindow) Flush() bool {
  84. current := this.kcp.current
  85. resent := uint32(this.kcp.fastresend)
  86. if this.kcp.fastresend <= 0 {
  87. resent = 0xffffffff
  88. }
  89. lost := false
  90. segSent := false
  91. for i := this.start; ; i = this.next[i] {
  92. segment := this.data[i]
  93. needsend := false
  94. if segment.transmit == 0 {
  95. needsend = true
  96. segment.transmit++
  97. segment.timeout = current + this.kcp.rx_rto
  98. } else if _itimediff(current, segment.timeout) >= 0 {
  99. needsend = true
  100. segment.transmit++
  101. segment.timeout = current + this.kcp.rx_rto
  102. lost = true
  103. } else if segment.ackSkipped >= resent {
  104. needsend = true
  105. segment.transmit++
  106. segment.ackSkipped = 0
  107. segment.timeout = current + this.kcp.rx_rto
  108. lost = true
  109. }
  110. if needsend {
  111. segment.Timestamp = current
  112. segment.SendingNext = this.kcp.snd_una
  113. segment.Opt = 0
  114. if this.kcp.state == StateReadyToClose {
  115. segment.Opt = SegmentOptionClose
  116. }
  117. this.kcp.output.Write(segment)
  118. segSent = true
  119. }
  120. if i == this.last {
  121. break
  122. }
  123. }
  124. this.kcp.HandleLost(lost)
  125. return segSent
  126. }
  127. type SendingQueue struct {
  128. start uint32
  129. cap uint32
  130. len uint32
  131. list []*DataSegment
  132. }
  133. func NewSendingQueue(size uint32) *SendingQueue {
  134. return &SendingQueue{
  135. start: 0,
  136. cap: size,
  137. list: make([]*DataSegment, size),
  138. len: 0,
  139. }
  140. }
  141. func (this *SendingQueue) IsFull() bool {
  142. return this.len == this.cap
  143. }
  144. func (this *SendingQueue) IsEmpty() bool {
  145. return this.len == 0
  146. }
  147. func (this *SendingQueue) Pop() *DataSegment {
  148. if this.IsEmpty() {
  149. return nil
  150. }
  151. seg := this.list[this.start]
  152. this.list[this.start] = nil
  153. this.len--
  154. this.start++
  155. if this.start == this.cap {
  156. this.start = 0
  157. }
  158. return seg
  159. }
  160. func (this *SendingQueue) Push(seg *DataSegment) {
  161. if this.IsFull() {
  162. return
  163. }
  164. this.list[(this.start+this.len)%this.cap] = seg
  165. this.len++
  166. }
  167. func (this *SendingQueue) Clear() {
  168. for i := uint32(0); i < this.len; i++ {
  169. this.list[(i+this.start)%this.cap].Release()
  170. this.list[(i+this.start)%this.cap] = nil
  171. }
  172. this.start = 0
  173. this.len = 0
  174. }
  175. func (this *SendingQueue) Len() uint32 {
  176. return this.len
  177. }