sending.go 4.0 KB

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