sending.go 8.1 KB

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