receiving.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package kcp
  2. import (
  3. "sync"
  4. "v2ray.com/core/common/alloc"
  5. )
  6. type ReceivingWindow struct {
  7. start uint32
  8. size uint32
  9. list []*DataSegment
  10. }
  11. func NewReceivingWindow(size uint32) *ReceivingWindow {
  12. return &ReceivingWindow{
  13. start: 0,
  14. size: size,
  15. list: make([]*DataSegment, size),
  16. }
  17. }
  18. func (this *ReceivingWindow) Size() uint32 {
  19. return this.size
  20. }
  21. func (this *ReceivingWindow) Position(idx uint32) uint32 {
  22. return (idx + this.start) % this.size
  23. }
  24. func (this *ReceivingWindow) Set(idx uint32, value *DataSegment) bool {
  25. pos := this.Position(idx)
  26. if this.list[pos] != nil {
  27. return false
  28. }
  29. this.list[pos] = value
  30. return true
  31. }
  32. func (this *ReceivingWindow) Remove(idx uint32) *DataSegment {
  33. pos := this.Position(idx)
  34. e := this.list[pos]
  35. this.list[pos] = nil
  36. return e
  37. }
  38. func (this *ReceivingWindow) RemoveFirst() *DataSegment {
  39. return this.Remove(0)
  40. }
  41. func (this *ReceivingWindow) Advance() {
  42. this.start++
  43. if this.start == this.size {
  44. this.start = 0
  45. }
  46. }
  47. type AckList struct {
  48. writer SegmentWriter
  49. timestamps []uint32
  50. numbers []uint32
  51. nextFlush []uint32
  52. }
  53. func NewAckList(writer SegmentWriter) *AckList {
  54. return &AckList{
  55. writer: writer,
  56. timestamps: make([]uint32, 0, 32),
  57. numbers: make([]uint32, 0, 32),
  58. nextFlush: make([]uint32, 0, 32),
  59. }
  60. }
  61. func (this *AckList) Add(number uint32, timestamp uint32) {
  62. this.timestamps = append(this.timestamps, timestamp)
  63. this.numbers = append(this.numbers, number)
  64. this.nextFlush = append(this.nextFlush, 0)
  65. }
  66. func (this *AckList) Clear(una uint32) {
  67. count := 0
  68. for i := 0; i < len(this.numbers); i++ {
  69. if this.numbers[i] >= una {
  70. if i != count {
  71. this.numbers[count] = this.numbers[i]
  72. this.timestamps[count] = this.timestamps[i]
  73. this.nextFlush[count] = this.nextFlush[i]
  74. }
  75. count++
  76. }
  77. }
  78. if count < len(this.numbers) {
  79. this.numbers = this.numbers[:count]
  80. this.timestamps = this.timestamps[:count]
  81. this.nextFlush = this.nextFlush[:count]
  82. }
  83. }
  84. func (this *AckList) Flush(current uint32, rto uint32) {
  85. seg := NewAckSegment()
  86. for i := 0; i < len(this.numbers) && !seg.IsFull(); i++ {
  87. if this.nextFlush[i] <= current {
  88. seg.PutNumber(this.numbers[i])
  89. seg.PutTimestamp(this.timestamps[i])
  90. this.nextFlush[i] = current + rto/2
  91. }
  92. }
  93. if seg.Count > 0 {
  94. this.writer.Write(seg)
  95. seg.Release()
  96. }
  97. }
  98. type ReceivingWorker struct {
  99. sync.RWMutex
  100. conn *Connection
  101. leftOver *alloc.Buffer
  102. window *ReceivingWindow
  103. acklist *AckList
  104. nextNumber uint32
  105. windowSize uint32
  106. }
  107. func NewReceivingWorker(kcp *Connection) *ReceivingWorker {
  108. worker := &ReceivingWorker{
  109. conn: kcp,
  110. window: NewReceivingWindow(kcp.Config.GetReceivingBufferSize()),
  111. windowSize: kcp.Config.GetReceivingInFlightSize(),
  112. }
  113. worker.acklist = NewAckList(worker)
  114. return worker
  115. }
  116. func (this *ReceivingWorker) ProcessSendingNext(number uint32) {
  117. this.Lock()
  118. defer this.Unlock()
  119. this.acklist.Clear(number)
  120. }
  121. func (this *ReceivingWorker) ProcessSegment(seg *DataSegment) {
  122. this.Lock()
  123. defer this.Unlock()
  124. number := seg.Number
  125. idx := number - this.nextNumber
  126. if idx >= this.windowSize {
  127. return
  128. }
  129. this.acklist.Clear(seg.SendingNext)
  130. this.acklist.Add(number, seg.Timestamp)
  131. if !this.window.Set(idx, seg) {
  132. seg.Release()
  133. }
  134. }
  135. func (this *ReceivingWorker) Read(b []byte) int {
  136. this.Lock()
  137. defer this.Unlock()
  138. total := 0
  139. if this.leftOver != nil {
  140. nBytes := copy(b, this.leftOver.Value)
  141. if nBytes < this.leftOver.Len() {
  142. this.leftOver.SliceFrom(nBytes)
  143. return nBytes
  144. }
  145. this.leftOver.Release()
  146. this.leftOver = nil
  147. total += nBytes
  148. }
  149. for total < len(b) {
  150. seg := this.window.RemoveFirst()
  151. if seg == nil {
  152. break
  153. }
  154. this.window.Advance()
  155. this.nextNumber++
  156. nBytes := copy(b[total:], seg.Data.Value)
  157. total += nBytes
  158. if nBytes < seg.Data.Len() {
  159. seg.Data.SliceFrom(nBytes)
  160. this.leftOver = seg.Data
  161. seg.Data = nil
  162. seg.Release()
  163. break
  164. }
  165. seg.Release()
  166. }
  167. return total
  168. }
  169. func (this *ReceivingWorker) Flush(current uint32) {
  170. this.Lock()
  171. defer this.Unlock()
  172. this.acklist.Flush(current, this.conn.roundTrip.Timeout())
  173. }
  174. func (this *ReceivingWorker) Write(seg Segment) {
  175. ackSeg := seg.(*AckSegment)
  176. ackSeg.Conv = this.conn.conv
  177. ackSeg.ReceivingNext = this.nextNumber
  178. ackSeg.ReceivingWindow = this.nextNumber + this.windowSize
  179. if this.conn.state == StateReadyToClose {
  180. ackSeg.Option = SegmentOptionClose
  181. }
  182. this.conn.output.Write(ackSeg)
  183. }
  184. func (this *ReceivingWorker) CloseRead() {
  185. }
  186. func (this *ReceivingWorker) UpdateNecessary() bool {
  187. return len(this.acklist.numbers) > 0
  188. }