receiving.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. continue
  71. }
  72. if i != count {
  73. this.numbers[count] = this.numbers[i]
  74. this.timestamps[count] = this.timestamps[i]
  75. this.nextFlush[count] = this.nextFlush[i]
  76. }
  77. count++
  78. }
  79. if count < len(this.numbers) {
  80. this.numbers = this.numbers[:count]
  81. this.timestamps = this.timestamps[:count]
  82. this.nextFlush = this.nextFlush[:count]
  83. }
  84. }
  85. func (this *AckList) Flush(current uint32, rto uint32) {
  86. seg := NewAckSegment()
  87. for i := 0; i < len(this.numbers) && !seg.IsFull(); i++ {
  88. if this.nextFlush[i] > current {
  89. continue
  90. }
  91. seg.PutNumber(this.numbers[i])
  92. seg.PutTimestamp(this.timestamps[i])
  93. timeout := rto / 4
  94. if timeout < 20 {
  95. timeout = 20
  96. }
  97. this.nextFlush[i] = current + timeout
  98. }
  99. if seg.Count > 0 {
  100. this.writer.Write(seg)
  101. seg.Release()
  102. }
  103. }
  104. type ReceivingWorker struct {
  105. sync.RWMutex
  106. conn *Connection
  107. leftOver *alloc.Buffer
  108. window *ReceivingWindow
  109. acklist *AckList
  110. nextNumber uint32
  111. windowSize uint32
  112. }
  113. func NewReceivingWorker(kcp *Connection) *ReceivingWorker {
  114. worker := &ReceivingWorker{
  115. conn: kcp,
  116. window: NewReceivingWindow(kcp.Config.GetReceivingBufferSize()),
  117. windowSize: kcp.Config.GetReceivingInFlightSize(),
  118. }
  119. worker.acklist = NewAckList(worker)
  120. return worker
  121. }
  122. func (this *ReceivingWorker) ProcessSendingNext(number uint32) {
  123. this.Lock()
  124. defer this.Unlock()
  125. this.acklist.Clear(number)
  126. }
  127. func (this *ReceivingWorker) ProcessSegment(seg *DataSegment) {
  128. this.Lock()
  129. defer this.Unlock()
  130. number := seg.Number
  131. idx := number - this.nextNumber
  132. if idx >= this.windowSize {
  133. return
  134. }
  135. this.acklist.Clear(seg.SendingNext)
  136. this.acklist.Add(number, seg.Timestamp)
  137. if !this.window.Set(idx, seg) {
  138. seg.Release()
  139. }
  140. }
  141. func (this *ReceivingWorker) Read(b []byte) int {
  142. this.Lock()
  143. defer this.Unlock()
  144. total := 0
  145. if this.leftOver != nil {
  146. nBytes := copy(b, this.leftOver.Value)
  147. if nBytes < this.leftOver.Len() {
  148. this.leftOver.SliceFrom(nBytes)
  149. return nBytes
  150. }
  151. this.leftOver.Release()
  152. this.leftOver = nil
  153. total += nBytes
  154. }
  155. for total < len(b) {
  156. seg := this.window.RemoveFirst()
  157. if seg == nil {
  158. break
  159. }
  160. this.window.Advance()
  161. this.nextNumber++
  162. nBytes := copy(b[total:], seg.Data.Value)
  163. total += nBytes
  164. if nBytes < seg.Data.Len() {
  165. seg.Data.SliceFrom(nBytes)
  166. this.leftOver = seg.Data
  167. seg.Data = nil
  168. seg.Release()
  169. break
  170. }
  171. seg.Release()
  172. }
  173. return total
  174. }
  175. func (this *ReceivingWorker) Flush(current uint32) {
  176. this.Lock()
  177. defer this.Unlock()
  178. this.acklist.Flush(current, this.conn.roundTrip.Timeout())
  179. }
  180. func (this *ReceivingWorker) Write(seg Segment) {
  181. ackSeg := seg.(*AckSegment)
  182. ackSeg.Conv = this.conn.conv
  183. ackSeg.ReceivingNext = this.nextNumber
  184. ackSeg.ReceivingWindow = this.nextNumber + this.windowSize
  185. if this.conn.state == StateReadyToClose {
  186. ackSeg.Option = SegmentOptionClose
  187. }
  188. this.conn.output.Write(ackSeg)
  189. }
  190. func (this *ReceivingWorker) CloseRead() {
  191. }
  192. func (this *ReceivingWorker) UpdateNecessary() bool {
  193. return len(this.acklist.numbers) > 0
  194. }