receiving.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.ReadBuffer/effectiveConfig.Mtu),
  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. timestamps []uint32
  129. numbers []uint32
  130. }
  131. func (this *ACKList) Add(number uint32, timestamp uint32) {
  132. this.timestamps = append(this.timestamps, timestamp)
  133. this.numbers = append(this.numbers, number)
  134. }
  135. func (this *ACKList) Clear(una uint32) bool {
  136. count := 0
  137. for i := 0; i < len(this.numbers); i++ {
  138. if this.numbers[i] >= una {
  139. if i != count {
  140. this.numbers[count] = this.numbers[i]
  141. this.timestamps[count] = this.timestamps[i]
  142. }
  143. count++
  144. }
  145. }
  146. if count < len(this.numbers) {
  147. this.numbers = this.numbers[:count]
  148. this.timestamps = this.timestamps[:count]
  149. return true
  150. }
  151. return false
  152. }
  153. func (this *ACKList) AsSegment() *ACKSegment {
  154. count := len(this.numbers)
  155. if count == 0 {
  156. return nil
  157. }
  158. if count > 128 {
  159. count = 128
  160. }
  161. seg := &ACKSegment{
  162. Count: byte(count),
  163. NumberList: this.numbers[:count],
  164. TimestampList: this.timestamps[:count],
  165. }
  166. //this.numbers = nil
  167. //this.timestamps = nil
  168. return seg
  169. }