sending.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package kcp
  2. import (
  3. "sync"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. )
  6. type SendingWindow struct {
  7. start uint32
  8. cap uint32
  9. len uint32
  10. last uint32
  11. data []*DataSegment
  12. prev []uint32
  13. next []uint32
  14. writer SegmentWriter
  15. onPacketLoss func(bool)
  16. }
  17. func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(bool)) *SendingWindow {
  18. window := &SendingWindow{
  19. start: 0,
  20. cap: size,
  21. len: 0,
  22. last: 0,
  23. data: make([]*DataSegment, size),
  24. prev: make([]uint32, size),
  25. next: make([]uint32, size),
  26. writer: writer,
  27. onPacketLoss: onPacketLoss,
  28. }
  29. return window
  30. }
  31. func (this *SendingWindow) Len() int {
  32. return int(this.len)
  33. }
  34. func (this *SendingWindow) Push(seg *DataSegment) {
  35. pos := (this.start + this.len) % this.cap
  36. this.data[pos] = seg
  37. if this.len > 0 {
  38. this.next[this.last] = pos
  39. this.prev[pos] = this.last
  40. }
  41. this.last = pos
  42. this.len++
  43. }
  44. func (this *SendingWindow) First() *DataSegment {
  45. return this.data[this.start]
  46. }
  47. func (this *SendingWindow) Clear(una uint32) {
  48. for this.Len() > 0 && this.data[this.start].Number < una {
  49. this.Remove(0)
  50. }
  51. }
  52. func (this *SendingWindow) Remove(idx uint32) {
  53. if this.len == 0 {
  54. return
  55. }
  56. pos := (this.start + idx) % this.cap
  57. seg := this.data[pos]
  58. if seg == nil {
  59. return
  60. }
  61. seg.Release()
  62. this.data[pos] = nil
  63. if pos == this.start && pos == this.last {
  64. this.len = 0
  65. this.start = 0
  66. this.last = 0
  67. } else if pos == this.start {
  68. delta := this.next[pos] - this.start
  69. if this.next[pos] < this.start {
  70. delta = this.next[pos] + this.cap - this.start
  71. }
  72. this.start = this.next[pos]
  73. this.len -= delta
  74. } else if pos == this.last {
  75. this.last = this.prev[pos]
  76. } else {
  77. this.next[this.prev[pos]] = this.next[pos]
  78. this.prev[this.next[pos]] = this.prev[pos]
  79. }
  80. }
  81. func (this *SendingWindow) HandleFastAck(number uint32) {
  82. if this.len == 0 {
  83. return
  84. }
  85. for i := this.start; ; i = this.next[i] {
  86. seg := this.data[i]
  87. if _itimediff(number, seg.Number) < 0 {
  88. break
  89. }
  90. if number != seg.Number {
  91. seg.ackSkipped++
  92. }
  93. if i == this.last {
  94. break
  95. }
  96. }
  97. }
  98. func (this *SendingWindow) Flush(current uint32, resend uint32, rto uint32) {
  99. if this.Len() == 0 {
  100. return
  101. }
  102. lost := false
  103. for i := this.start; ; i = this.next[i] {
  104. segment := this.data[i]
  105. needsend := false
  106. if segment.transmit == 0 {
  107. needsend = true
  108. segment.transmit++
  109. segment.timeout = current + rto
  110. } else if _itimediff(current, segment.timeout) >= 0 {
  111. needsend = true
  112. segment.transmit++
  113. segment.timeout = current + rto
  114. lost = true
  115. } else if segment.ackSkipped >= resend {
  116. needsend = true
  117. segment.transmit++
  118. segment.ackSkipped = 0
  119. segment.timeout = current + rto
  120. lost = true
  121. }
  122. if needsend {
  123. this.writer.Write(segment)
  124. }
  125. if i == this.last {
  126. break
  127. }
  128. }
  129. this.onPacketLoss(lost)
  130. }
  131. type SendingQueue struct {
  132. start uint32
  133. cap uint32
  134. len uint32
  135. list []*DataSegment
  136. }
  137. func NewSendingQueue(size uint32) *SendingQueue {
  138. return &SendingQueue{
  139. start: 0,
  140. cap: size,
  141. list: make([]*DataSegment, size),
  142. len: 0,
  143. }
  144. }
  145. func (this *SendingQueue) IsFull() bool {
  146. return this.len == this.cap
  147. }
  148. func (this *SendingQueue) IsEmpty() bool {
  149. return this.len == 0
  150. }
  151. func (this *SendingQueue) Pop() *DataSegment {
  152. if this.IsEmpty() {
  153. return nil
  154. }
  155. seg := this.list[this.start]
  156. this.list[this.start] = nil
  157. this.len--
  158. this.start++
  159. if this.start == this.cap {
  160. this.start = 0
  161. }
  162. return seg
  163. }
  164. func (this *SendingQueue) Push(seg *DataSegment) {
  165. if this.IsFull() {
  166. return
  167. }
  168. this.list[(this.start+this.len)%this.cap] = seg
  169. this.len++
  170. }
  171. func (this *SendingQueue) Clear() {
  172. for i := uint32(0); i < this.len; i++ {
  173. this.list[(i+this.start)%this.cap].Release()
  174. this.list[(i+this.start)%this.cap] = nil
  175. }
  176. this.start = 0
  177. this.len = 0
  178. }
  179. func (this *SendingQueue) Len() uint32 {
  180. return this.len
  181. }
  182. type SendingWorker struct {
  183. sync.Mutex
  184. kcp *KCP
  185. window *SendingWindow
  186. queue *SendingQueue
  187. windowSize uint32
  188. firstUnacknowledged uint32
  189. nextNumber uint32
  190. remoteNextNumber uint32
  191. controlWindow uint32
  192. fastResend uint32
  193. updated bool
  194. }
  195. func NewSendingWorker(kcp *KCP) *SendingWorker {
  196. worker := &SendingWorker{
  197. kcp: kcp,
  198. queue: NewSendingQueue(effectiveConfig.GetSendingQueueSize()),
  199. fastResend: 2,
  200. remoteNextNumber: 32,
  201. windowSize: effectiveConfig.GetSendingWindowSize(),
  202. controlWindow: effectiveConfig.GetSendingWindowSize(),
  203. }
  204. worker.window = NewSendingWindow(effectiveConfig.GetSendingWindowSize(), worker, worker.OnPacketLoss)
  205. return worker
  206. }
  207. func (this *SendingWorker) ProcessReceivingNext(nextNumber uint32) {
  208. this.Lock()
  209. defer this.Unlock()
  210. this.window.Clear(nextNumber)
  211. this.FindFirstUnacknowledged()
  212. }
  213. // @Private
  214. func (this *SendingWorker) FindFirstUnacknowledged() {
  215. prevUna := this.firstUnacknowledged
  216. if this.window.Len() > 0 {
  217. this.firstUnacknowledged = this.window.First().Number
  218. } else {
  219. this.firstUnacknowledged = this.nextNumber
  220. }
  221. if this.firstUnacknowledged != prevUna {
  222. this.updated = true
  223. }
  224. }
  225. func (this *SendingWorker) ProcessAck(number uint32) {
  226. if number-this.firstUnacknowledged > this.windowSize {
  227. return
  228. }
  229. this.Lock()
  230. defer this.Unlock()
  231. this.window.Remove(number - this.firstUnacknowledged)
  232. this.FindFirstUnacknowledged()
  233. }
  234. func (this *SendingWorker) ProcessAckSegment(seg *AckSegment) {
  235. if this.remoteNextNumber < seg.ReceivingWindow {
  236. this.remoteNextNumber = seg.ReceivingWindow
  237. }
  238. this.ProcessReceivingNext(seg.ReceivingNext)
  239. var maxack uint32
  240. for i := 0; i < int(seg.Count); i++ {
  241. timestamp := seg.TimestampList[i]
  242. number := seg.NumberList[i]
  243. if this.kcp.current-timestamp > 10000 {
  244. this.kcp.update_ack(int32(this.kcp.current - timestamp))
  245. }
  246. this.ProcessAck(number)
  247. if maxack < number {
  248. maxack = number
  249. }
  250. }
  251. this.Lock()
  252. this.window.HandleFastAck(maxack)
  253. this.Unlock()
  254. }
  255. func (this *SendingWorker) Push(b []byte) int {
  256. nBytes := 0
  257. for len(b) > 0 && !this.queue.IsFull() {
  258. var size int
  259. if len(b) > int(this.kcp.mss) {
  260. size = int(this.kcp.mss)
  261. } else {
  262. size = len(b)
  263. }
  264. seg := &DataSegment{
  265. Data: alloc.NewSmallBuffer().Clear().Append(b[:size]),
  266. }
  267. this.Lock()
  268. this.queue.Push(seg)
  269. this.Unlock()
  270. b = b[size:]
  271. nBytes += size
  272. }
  273. return nBytes
  274. }
  275. func (this *SendingWorker) Write(seg ISegment) {
  276. dataSeg := seg.(*DataSegment)
  277. dataSeg.Conv = this.kcp.conv
  278. dataSeg.Timestamp = this.kcp.current
  279. dataSeg.SendingNext = this.firstUnacknowledged
  280. dataSeg.Opt = 0
  281. if this.kcp.state == StateReadyToClose {
  282. dataSeg.Opt = SegmentOptionClose
  283. }
  284. this.kcp.output.Write(dataSeg)
  285. this.updated = false
  286. }
  287. func (this *SendingWorker) PingNecessary() bool {
  288. return this.updated
  289. }
  290. func (this *SendingWorker) OnPacketLoss(lost bool) {
  291. if !effectiveConfig.Congestion {
  292. return
  293. }
  294. if lost {
  295. this.controlWindow = 3 * this.controlWindow / 4
  296. } else {
  297. this.controlWindow += this.controlWindow / 4
  298. }
  299. if this.controlWindow < 4 {
  300. this.controlWindow = 4
  301. }
  302. if this.controlWindow > 2*this.windowSize {
  303. this.controlWindow = 2 * this.windowSize
  304. }
  305. }
  306. func (this *SendingWorker) Flush() {
  307. this.Lock()
  308. defer this.Unlock()
  309. cwnd := this.firstUnacknowledged + this.windowSize
  310. if cwnd > this.remoteNextNumber {
  311. cwnd = this.remoteNextNumber
  312. }
  313. if effectiveConfig.Congestion && cwnd > this.firstUnacknowledged+this.controlWindow {
  314. cwnd = this.firstUnacknowledged + this.controlWindow
  315. }
  316. for !this.queue.IsEmpty() && _itimediff(this.nextNumber, cwnd) < 0 {
  317. seg := this.queue.Pop()
  318. seg.Number = this.nextNumber
  319. seg.timeout = this.kcp.current
  320. seg.ackSkipped = 0
  321. seg.transmit = 0
  322. this.window.Push(seg)
  323. this.nextNumber++
  324. }
  325. this.window.Flush(this.kcp.current, this.kcp.fastresend, this.kcp.rx_rto)
  326. }
  327. func (this *SendingWorker) CloseWrite() {
  328. this.Lock()
  329. defer this.Unlock()
  330. this.window.Clear(0xFFFFFFFF)
  331. this.queue.Clear()
  332. }