sending.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 := this.kcp.fastresend
  98. lost := false
  99. segSent := false
  100. for i := this.start; ; i = this.next[i] {
  101. segment := this.data[i]
  102. needsend := false
  103. if segment.transmit == 0 {
  104. needsend = true
  105. segment.transmit++
  106. segment.timeout = current + this.kcp.rx_rto
  107. } else if _itimediff(current, segment.timeout) >= 0 {
  108. needsend = true
  109. segment.transmit++
  110. segment.timeout = current + this.kcp.rx_rto
  111. lost = true
  112. } else if segment.ackSkipped >= resent {
  113. needsend = true
  114. segment.transmit++
  115. segment.ackSkipped = 0
  116. segment.timeout = current + this.kcp.rx_rto
  117. lost = true
  118. }
  119. if needsend {
  120. segment.Timestamp = current
  121. segment.SendingNext = this.kcp.snd_una
  122. segment.Opt = 0
  123. if this.kcp.state == StateReadyToClose {
  124. segment.Opt = SegmentOptionClose
  125. }
  126. this.kcp.output.Write(segment)
  127. segSent = true
  128. }
  129. if i == this.last {
  130. break
  131. }
  132. }
  133. this.kcp.HandleLost(lost)
  134. return segSent
  135. }
  136. type SendingQueue struct {
  137. start uint32
  138. cap uint32
  139. len uint32
  140. list []*DataSegment
  141. }
  142. func NewSendingQueue(size uint32) *SendingQueue {
  143. return &SendingQueue{
  144. start: 0,
  145. cap: size,
  146. list: make([]*DataSegment, size),
  147. len: 0,
  148. }
  149. }
  150. func (this *SendingQueue) IsFull() bool {
  151. return this.len == this.cap
  152. }
  153. func (this *SendingQueue) IsEmpty() bool {
  154. return this.len == 0
  155. }
  156. func (this *SendingQueue) Pop() *DataSegment {
  157. if this.IsEmpty() {
  158. return nil
  159. }
  160. seg := this.list[this.start]
  161. this.list[this.start] = nil
  162. this.len--
  163. this.start++
  164. if this.start == this.cap {
  165. this.start = 0
  166. }
  167. return seg
  168. }
  169. func (this *SendingQueue) Push(seg *DataSegment) {
  170. if this.IsFull() {
  171. return
  172. }
  173. this.list[(this.start+this.len)%this.cap] = seg
  174. this.len++
  175. }
  176. func (this *SendingQueue) Clear() {
  177. for i := uint32(0); i < this.len; i++ {
  178. this.list[(i+this.start)%this.cap].Release()
  179. this.list[(i+this.start)%this.cap] = nil
  180. }
  181. this.start = 0
  182. this.len = 0
  183. }
  184. func (this *SendingQueue) Len() uint32 {
  185. return this.len
  186. }