receiving.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package kcp
  2. import (
  3. "io"
  4. "sync"
  5. "time"
  6. "github.com/v2ray/v2ray-core/common/alloc"
  7. )
  8. type ReceivingWindow struct {
  9. start uint32
  10. size uint32
  11. list []*DataSegment
  12. }
  13. func NewReceivingWindow(size uint32) *ReceivingWindow {
  14. return &ReceivingWindow{
  15. start: 0,
  16. size: size,
  17. list: make([]*DataSegment, size),
  18. }
  19. }
  20. func (this *ReceivingWindow) Size() uint32 {
  21. return this.size
  22. }
  23. func (this *ReceivingWindow) Position(idx uint32) uint32 {
  24. return (idx + this.start) % this.size
  25. }
  26. func (this *ReceivingWindow) Set(idx uint32, value *DataSegment) bool {
  27. pos := this.Position(idx)
  28. if this.list[pos] != nil {
  29. return false
  30. }
  31. this.list[pos] = value
  32. return true
  33. }
  34. func (this *ReceivingWindow) Remove(idx uint32) *DataSegment {
  35. pos := this.Position(idx)
  36. e := this.list[pos]
  37. this.list[pos] = nil
  38. return e
  39. }
  40. func (this *ReceivingWindow) RemoveFirst() *DataSegment {
  41. return this.Remove(0)
  42. }
  43. func (this *ReceivingWindow) Advance() {
  44. this.start++
  45. if this.start == this.size {
  46. this.start = 0
  47. }
  48. }
  49. type ReceivingQueue struct {
  50. sync.RWMutex
  51. closed bool
  52. cache *alloc.Buffer
  53. queue chan *alloc.Buffer
  54. timeout time.Time
  55. }
  56. func NewReceivingQueue() *ReceivingQueue {
  57. return &ReceivingQueue{
  58. queue: make(chan *alloc.Buffer, effectiveConfig.GetReceivingQueueSize()),
  59. }
  60. }
  61. func (this *ReceivingQueue) Read(buf []byte) (int, error) {
  62. if this.cache.Len() > 0 {
  63. nBytes, err := this.cache.Read(buf)
  64. if this.cache.IsEmpty() {
  65. this.cache.Release()
  66. this.cache = nil
  67. }
  68. return nBytes, err
  69. }
  70. var totalBytes int
  71. L:
  72. for totalBytes < len(buf) {
  73. timeToSleep := time.Millisecond
  74. select {
  75. case payload, open := <-this.queue:
  76. if !open {
  77. return totalBytes, io.EOF
  78. }
  79. nBytes, err := payload.Read(buf)
  80. totalBytes += nBytes
  81. if err != nil {
  82. return totalBytes, err
  83. }
  84. if !payload.IsEmpty() {
  85. this.cache = payload
  86. }
  87. buf = buf[nBytes:]
  88. case <-time.After(timeToSleep):
  89. if totalBytes > 0 {
  90. break L
  91. }
  92. this.RLock()
  93. if !this.timeout.IsZero() && this.timeout.Before(time.Now()) {
  94. this.RUnlock()
  95. return totalBytes, errTimeout
  96. }
  97. this.RUnlock()
  98. timeToSleep += 500 * time.Millisecond
  99. }
  100. }
  101. return totalBytes, nil
  102. }
  103. func (this *ReceivingQueue) Put(payload *alloc.Buffer) {
  104. this.RLock()
  105. defer this.RUnlock()
  106. if this.closed {
  107. payload.Release()
  108. return
  109. }
  110. this.queue <- payload
  111. }
  112. func (this *ReceivingQueue) SetReadDeadline(t time.Time) error {
  113. this.Lock()
  114. defer this.Unlock()
  115. this.timeout = t
  116. return nil
  117. }
  118. func (this *ReceivingQueue) Close() {
  119. this.Lock()
  120. defer this.Unlock()
  121. if this.closed {
  122. return
  123. }
  124. this.closed = true
  125. close(this.queue)
  126. }
  127. type AckList struct {
  128. kcp *KCP
  129. timestamps []uint32
  130. numbers []uint32
  131. nextFlush []uint32
  132. }
  133. func NewACKList(kcp *KCP) *AckList {
  134. return &AckList{
  135. kcp: kcp,
  136. timestamps: make([]uint32, 0, 32),
  137. numbers: make([]uint32, 0, 32),
  138. nextFlush: make([]uint32, 0, 32),
  139. }
  140. }
  141. func (this *AckList) Add(number uint32, timestamp uint32) {
  142. this.timestamps = append(this.timestamps, timestamp)
  143. this.numbers = append(this.numbers, number)
  144. this.nextFlush = append(this.nextFlush, 0)
  145. }
  146. func (this *AckList) Clear(una uint32) {
  147. count := 0
  148. for i := 0; i < len(this.numbers); i++ {
  149. if this.numbers[i] >= una {
  150. if i != count {
  151. this.numbers[count] = this.numbers[i]
  152. this.timestamps[count] = this.timestamps[i]
  153. this.nextFlush[count] = this.nextFlush[i]
  154. }
  155. count++
  156. }
  157. }
  158. if count < len(this.numbers) {
  159. this.numbers = this.numbers[:count]
  160. this.timestamps = this.timestamps[:count]
  161. this.nextFlush = this.nextFlush[:count]
  162. }
  163. }
  164. func (this *AckList) Flush() bool {
  165. seg := &ACKSegment{
  166. Conv: this.kcp.conv,
  167. ReceivingNext: this.kcp.rcv_nxt,
  168. ReceivingWindow: this.kcp.rcv_nxt + this.kcp.rcv_wnd,
  169. }
  170. if this.kcp.state == StateReadyToClose {
  171. seg.Opt = SegmentOptionClose
  172. }
  173. current := this.kcp.current
  174. for i := 0; i < len(this.numbers); i++ {
  175. if this.nextFlush[i] <= current {
  176. seg.Count++
  177. seg.NumberList = append(seg.NumberList, this.numbers[i])
  178. seg.TimestampList = append(seg.TimestampList, this.timestamps[i])
  179. this.nextFlush[i] = current + 100
  180. if seg.Count == 128 {
  181. break
  182. }
  183. }
  184. }
  185. if seg.Count > 0 {
  186. this.kcp.output.Write(seg)
  187. return true
  188. }
  189. return false
  190. }