receiving.go 4.5 KB

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