sending.go 3.9 KB

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