sending.go 3.8 KB

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