kcp.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. // Package kcp - A Fast and Reliable ARQ Protocol
  2. //
  3. // Acknowledgement:
  4. // skywind3000@github for inventing the KCP protocol
  5. // xtaci@github for translating to Golang
  6. package kcp
  7. import (
  8. "encoding/binary"
  9. "github.com/v2ray/v2ray-core/common/alloc"
  10. )
  11. const (
  12. IKCP_RTO_NDL = 30 // no delay min rto
  13. IKCP_RTO_MIN = 100 // normal min rto
  14. IKCP_RTO_DEF = 200
  15. IKCP_RTO_MAX = 60000
  16. IKCP_CMD_PUSH = 81 // cmd: push data
  17. IKCP_CMD_ACK = 82 // cmd: ack
  18. IKCP_CMD_WASK = 83 // cmd: window probe (ask)
  19. IKCP_CMD_WINS = 84 // cmd: window size (tell)
  20. IKCP_ASK_SEND = 1 // need to send IKCP_CMD_WASK
  21. IKCP_ASK_TELL = 2 // need to send IKCP_CMD_WINS
  22. IKCP_WND_SND = 32
  23. IKCP_WND_RCV = 32
  24. IKCP_MTU_DEF = 1350
  25. IKCP_ACK_FAST = 3
  26. IKCP_INTERVAL = 100
  27. IKCP_OVERHEAD = 24
  28. IKCP_DEADLINK = 20
  29. IKCP_THRESH_INIT = 2
  30. IKCP_THRESH_MIN = 2
  31. IKCP_PROBE_INIT = 7000 // 7 secs to probe window size
  32. IKCP_PROBE_LIMIT = 120000 // up to 120 secs to probe window
  33. )
  34. // Output is a closure which captures conn and calls conn.Write
  35. type Output func(buf []byte)
  36. /* encode 8 bits unsigned int */
  37. func ikcp_encode8u(p []byte, c byte) []byte {
  38. p[0] = c
  39. return p[1:]
  40. }
  41. /* decode 8 bits unsigned int */
  42. func ikcp_decode8u(p []byte, c *byte) []byte {
  43. *c = p[0]
  44. return p[1:]
  45. }
  46. /* encode 16 bits unsigned int (lsb) */
  47. func ikcp_encode16u(p []byte, w uint16) []byte {
  48. binary.LittleEndian.PutUint16(p, w)
  49. return p[2:]
  50. }
  51. /* decode 16 bits unsigned int (lsb) */
  52. func ikcp_decode16u(p []byte, w *uint16) []byte {
  53. *w = binary.LittleEndian.Uint16(p)
  54. return p[2:]
  55. }
  56. /* encode 32 bits unsigned int (lsb) */
  57. func ikcp_encode32u(p []byte, l uint32) []byte {
  58. binary.LittleEndian.PutUint32(p, l)
  59. return p[4:]
  60. }
  61. /* decode 32 bits unsigned int (lsb) */
  62. func ikcp_decode32u(p []byte, l *uint32) []byte {
  63. *l = binary.LittleEndian.Uint32(p)
  64. return p[4:]
  65. }
  66. func _imin_(a, b uint32) uint32 {
  67. if a <= b {
  68. return a
  69. } else {
  70. return b
  71. }
  72. }
  73. func _imax_(a, b uint32) uint32 {
  74. if a >= b {
  75. return a
  76. } else {
  77. return b
  78. }
  79. }
  80. func _itimediff(later, earlier uint32) int32 {
  81. return (int32)(later - earlier)
  82. }
  83. // Segment defines a KCP segment
  84. type Segment struct {
  85. conv uint32
  86. cmd uint32
  87. frg uint32
  88. wnd uint32
  89. ts uint32
  90. sn uint32
  91. una uint32
  92. resendts uint32
  93. fastack uint32
  94. xmit uint32
  95. data *alloc.Buffer
  96. }
  97. // encode a segment into buffer
  98. func (seg *Segment) encode(ptr []byte) []byte {
  99. ptr = ikcp_encode32u(ptr, seg.conv)
  100. ptr = ikcp_encode8u(ptr, uint8(seg.cmd))
  101. ptr = ikcp_encode8u(ptr, uint8(seg.frg))
  102. ptr = ikcp_encode16u(ptr, uint16(seg.wnd))
  103. ptr = ikcp_encode32u(ptr, seg.ts)
  104. ptr = ikcp_encode32u(ptr, seg.sn)
  105. ptr = ikcp_encode32u(ptr, seg.una)
  106. ptr = ikcp_encode16u(ptr, uint16(seg.data.Len()))
  107. return ptr
  108. }
  109. func (this *Segment) Release() {
  110. this.data.Release()
  111. this.data = nil
  112. }
  113. // NewSegment creates a KCP segment
  114. func NewSegment() *Segment {
  115. return &Segment{
  116. data: alloc.NewSmallBuffer().Clear(),
  117. }
  118. }
  119. // KCP defines a single KCP connection
  120. type KCP struct {
  121. conv, mtu, mss, state uint32
  122. snd_una, snd_nxt, rcv_nxt uint32
  123. ts_recent, ts_lastack, ssthresh uint32
  124. rx_rttvar, rx_srtt, rx_rto uint32
  125. snd_wnd, rcv_wnd, rmt_wnd, cwnd, probe uint32
  126. current, interval, ts_flush, xmit uint32
  127. updated bool
  128. ts_probe, probe_wait uint32
  129. dead_link, incr uint32
  130. snd_queue []*Segment
  131. rcv_queue []*Segment
  132. snd_buf []*Segment
  133. rcv_buf *ReceivingWindow
  134. acklist []uint32
  135. buffer []byte
  136. fastresend int32
  137. congestionControl bool
  138. output Output
  139. }
  140. // NewKCP create a new kcp control object, 'conv' must equal in two endpoint
  141. // from the same connection.
  142. func NewKCP(conv uint32, mtu uint32, sendingWindowSize uint32, receivingWindowSize uint32, output Output) *KCP {
  143. kcp := new(KCP)
  144. kcp.conv = conv
  145. kcp.snd_wnd = sendingWindowSize
  146. kcp.rcv_wnd = receivingWindowSize
  147. kcp.rmt_wnd = IKCP_WND_RCV
  148. kcp.mtu = mtu
  149. kcp.mss = kcp.mtu - IKCP_OVERHEAD
  150. kcp.buffer = make([]byte, (kcp.mtu+IKCP_OVERHEAD)*3)
  151. kcp.rx_rto = IKCP_RTO_DEF
  152. kcp.interval = IKCP_INTERVAL
  153. kcp.ts_flush = IKCP_INTERVAL
  154. kcp.ssthresh = IKCP_THRESH_INIT
  155. kcp.dead_link = IKCP_DEADLINK
  156. kcp.output = output
  157. kcp.rcv_buf = NewReceivingWindow(receivingWindowSize)
  158. return kcp
  159. }
  160. // Recv is user/upper level recv: returns size, returns below zero for EAGAIN
  161. func (kcp *KCP) Recv(buffer []byte) (n int) {
  162. if len(kcp.rcv_queue) == 0 {
  163. return -1
  164. }
  165. var fast_recover bool
  166. if len(kcp.rcv_queue) >= int(kcp.rcv_wnd) {
  167. fast_recover = true
  168. }
  169. // merge fragment
  170. count := 0
  171. for _, seg := range kcp.rcv_queue {
  172. dataLen := seg.data.Len()
  173. if dataLen > len(buffer) {
  174. break
  175. }
  176. copy(buffer, seg.data.Value)
  177. seg.Release()
  178. buffer = buffer[dataLen:]
  179. n += dataLen
  180. count++
  181. }
  182. kcp.rcv_queue = kcp.rcv_queue[count:]
  183. kcp.DumpReceivingBuf()
  184. // fast recover
  185. if len(kcp.rcv_queue) < int(kcp.rcv_wnd) && fast_recover {
  186. // ready to send back IKCP_CMD_WINS in ikcp_flush
  187. // tell remote my window size
  188. kcp.probe |= IKCP_ASK_TELL
  189. }
  190. return
  191. }
  192. // DumpReceivingBuf moves available data from rcv_buf -> rcv_queue
  193. // @Private
  194. func (kcp *KCP) DumpReceivingBuf() {
  195. for {
  196. seg := kcp.rcv_buf.RemoveFirst()
  197. if seg == nil {
  198. break
  199. }
  200. kcp.rcv_queue = append(kcp.rcv_queue, seg)
  201. kcp.rcv_buf.Advance()
  202. kcp.rcv_nxt++
  203. }
  204. }
  205. // Send is user/upper level send, returns below zero for error
  206. func (kcp *KCP) Send(buffer []byte) int {
  207. var count int
  208. if len(buffer) == 0 {
  209. return -1
  210. }
  211. if len(buffer) < int(kcp.mss) {
  212. count = 1
  213. } else {
  214. count = (len(buffer) + int(kcp.mss) - 1) / int(kcp.mss)
  215. }
  216. if count > 255 {
  217. return -2
  218. }
  219. if count == 0 {
  220. count = 1
  221. }
  222. for i := 0; i < count; i++ {
  223. var size int
  224. if len(buffer) > int(kcp.mss) {
  225. size = int(kcp.mss)
  226. } else {
  227. size = len(buffer)
  228. }
  229. seg := NewSegment()
  230. seg.data.Append(buffer[:size])
  231. seg.frg = uint32(count - i - 1)
  232. kcp.snd_queue = append(kcp.snd_queue, seg)
  233. buffer = buffer[size:]
  234. }
  235. return 0
  236. }
  237. // https://tools.ietf.org/html/rfc6298
  238. func (kcp *KCP) update_ack(rtt int32) {
  239. var rto uint32 = 0
  240. if kcp.rx_srtt == 0 {
  241. kcp.rx_srtt = uint32(rtt)
  242. kcp.rx_rttvar = uint32(rtt) / 2
  243. } else {
  244. delta := rtt - int32(kcp.rx_srtt)
  245. if delta < 0 {
  246. delta = -delta
  247. }
  248. kcp.rx_rttvar = (3*kcp.rx_rttvar + uint32(delta)) / 4
  249. kcp.rx_srtt = (7*kcp.rx_srtt + uint32(rtt)) / 8
  250. if kcp.rx_srtt < 1 {
  251. kcp.rx_srtt = 1
  252. }
  253. }
  254. rto = kcp.rx_srtt + _imax_(kcp.interval, 4*kcp.rx_rttvar)
  255. if rto > IKCP_RTO_MAX {
  256. rto = IKCP_RTO_MAX
  257. }
  258. kcp.rx_rto = rto
  259. }
  260. func (kcp *KCP) shrink_buf() {
  261. if len(kcp.snd_buf) > 0 {
  262. seg := kcp.snd_buf[0]
  263. kcp.snd_una = seg.sn
  264. } else {
  265. kcp.snd_una = kcp.snd_nxt
  266. }
  267. }
  268. func (kcp *KCP) parse_ack(sn uint32) {
  269. if _itimediff(sn, kcp.snd_una) < 0 || _itimediff(sn, kcp.snd_nxt) >= 0 {
  270. return
  271. }
  272. for k, seg := range kcp.snd_buf {
  273. if sn == seg.sn {
  274. kcp.snd_buf = append(kcp.snd_buf[:k], kcp.snd_buf[k+1:]...)
  275. seg.Release()
  276. break
  277. }
  278. if _itimediff(sn, seg.sn) < 0 {
  279. break
  280. }
  281. }
  282. }
  283. func (kcp *KCP) parse_fastack(sn uint32) {
  284. if _itimediff(sn, kcp.snd_una) < 0 || _itimediff(sn, kcp.snd_nxt) >= 0 {
  285. return
  286. }
  287. for _, seg := range kcp.snd_buf {
  288. if _itimediff(sn, seg.sn) < 0 {
  289. break
  290. } else if sn != seg.sn {
  291. seg.fastack++
  292. }
  293. }
  294. }
  295. func (kcp *KCP) parse_una(una uint32) {
  296. count := 0
  297. for _, seg := range kcp.snd_buf {
  298. if _itimediff(una, seg.sn) > 0 {
  299. seg.Release()
  300. count++
  301. } else {
  302. break
  303. }
  304. }
  305. kcp.snd_buf = kcp.snd_buf[count:]
  306. }
  307. // ack append
  308. func (kcp *KCP) ack_push(sn, ts uint32) {
  309. kcp.acklist = append(kcp.acklist, sn, ts)
  310. }
  311. func (kcp *KCP) ack_get(p int) (sn, ts uint32) {
  312. return kcp.acklist[p*2+0], kcp.acklist[p*2+1]
  313. }
  314. func (kcp *KCP) parse_data(newseg *Segment) {
  315. sn := newseg.sn
  316. if _itimediff(sn, kcp.rcv_nxt+kcp.rcv_wnd) >= 0 ||
  317. _itimediff(sn, kcp.rcv_nxt) < 0 {
  318. return
  319. }
  320. idx := sn - kcp.rcv_nxt
  321. if !kcp.rcv_buf.Set(idx, newseg) {
  322. newseg.Release()
  323. }
  324. kcp.DumpReceivingBuf()
  325. }
  326. // Input when you received a low level packet (eg. UDP packet), call it
  327. func (kcp *KCP) Input(data []byte) int {
  328. //una := kcp.snd_una
  329. if len(data) < IKCP_OVERHEAD {
  330. return -1
  331. }
  332. var maxack uint32
  333. var flag int
  334. for {
  335. var ts, sn, una, conv uint32
  336. var wnd, length uint16
  337. var cmd, frg uint8
  338. if len(data) < int(IKCP_OVERHEAD) {
  339. break
  340. }
  341. data = ikcp_decode32u(data, &conv)
  342. if conv != kcp.conv {
  343. return -1
  344. }
  345. data = ikcp_decode8u(data, &cmd)
  346. data = ikcp_decode8u(data, &frg)
  347. data = ikcp_decode16u(data, &wnd)
  348. data = ikcp_decode32u(data, &ts)
  349. data = ikcp_decode32u(data, &sn)
  350. data = ikcp_decode32u(data, &una)
  351. data = ikcp_decode16u(data, &length)
  352. if len(data) < int(length) {
  353. return -2
  354. }
  355. if cmd != IKCP_CMD_PUSH && cmd != IKCP_CMD_ACK &&
  356. cmd != IKCP_CMD_WASK && cmd != IKCP_CMD_WINS {
  357. return -3
  358. }
  359. if kcp.rmt_wnd < uint32(wnd) {
  360. kcp.rmt_wnd = uint32(wnd)
  361. }
  362. //kcp.rmt_wnd = uint32(wnd)
  363. kcp.parse_una(una)
  364. kcp.shrink_buf()
  365. if cmd == IKCP_CMD_ACK {
  366. if _itimediff(kcp.current, ts) >= 0 {
  367. kcp.update_ack(_itimediff(kcp.current, ts))
  368. }
  369. kcp.parse_ack(sn)
  370. kcp.shrink_buf()
  371. if flag == 0 {
  372. flag = 1
  373. maxack = sn
  374. } else if _itimediff(sn, maxack) > 0 {
  375. maxack = sn
  376. }
  377. } else if cmd == IKCP_CMD_PUSH {
  378. if _itimediff(sn, kcp.rcv_nxt+kcp.rcv_wnd) < 0 {
  379. kcp.ack_push(sn, ts)
  380. if _itimediff(sn, kcp.rcv_nxt) >= 0 {
  381. seg := NewSegment()
  382. seg.conv = conv
  383. seg.cmd = uint32(cmd)
  384. seg.frg = uint32(frg)
  385. seg.wnd = uint32(wnd)
  386. seg.ts = ts
  387. seg.sn = sn
  388. seg.una = una
  389. seg.data.Append(data[:length])
  390. kcp.parse_data(seg)
  391. }
  392. }
  393. } else if cmd == IKCP_CMD_WASK {
  394. // ready to send back IKCP_CMD_WINS in Ikcp_flush
  395. // tell remote my window size
  396. kcp.probe |= IKCP_ASK_TELL
  397. } else if cmd == IKCP_CMD_WINS {
  398. // do nothing
  399. } else {
  400. return -3
  401. }
  402. data = data[length:]
  403. }
  404. if flag != 0 {
  405. kcp.parse_fastack(maxack)
  406. }
  407. /*
  408. if _itimediff(kcp.snd_una, una) > 0 {
  409. if kcp.cwnd < kcp.rmt_wnd {
  410. mss := kcp.mss
  411. if kcp.cwnd < kcp.ssthresh {
  412. kcp.cwnd++
  413. kcp.incr += mss
  414. } else {
  415. if kcp.incr < mss {
  416. kcp.incr = mss
  417. }
  418. kcp.incr += (mss*mss)/kcp.incr + (mss / 16)
  419. if (kcp.cwnd+1)*mss <= kcp.incr {
  420. kcp.cwnd++
  421. }
  422. }
  423. if kcp.cwnd > kcp.rmt_wnd {
  424. kcp.cwnd = kcp.rmt_wnd
  425. kcp.incr = kcp.rmt_wnd * mss
  426. }
  427. }
  428. }*/
  429. return 0
  430. }
  431. // flush pending data
  432. func (kcp *KCP) flush() {
  433. current := kcp.current
  434. buffer := kcp.buffer
  435. change := 0
  436. //lost := false
  437. if !kcp.updated {
  438. return
  439. }
  440. var seg Segment
  441. seg.conv = kcp.conv
  442. seg.cmd = IKCP_CMD_ACK
  443. seg.wnd = uint32(kcp.rcv_nxt + kcp.rcv_wnd)
  444. seg.una = kcp.rcv_nxt
  445. // flush acknowledges
  446. count := len(kcp.acklist) / 2
  447. ptr := buffer
  448. for i := 0; i < count; i++ {
  449. size := len(buffer) - len(ptr)
  450. if size+IKCP_OVERHEAD > int(kcp.mtu) {
  451. kcp.output(buffer[:size])
  452. ptr = buffer
  453. }
  454. seg.sn, seg.ts = kcp.ack_get(i)
  455. ptr = seg.encode(ptr)
  456. }
  457. kcp.acklist = nil
  458. // probe window size (if remote window size equals zero)
  459. /*
  460. if kcp.rmt_wnd == 0 {
  461. if kcp.probe_wait == 0 {
  462. kcp.probe_wait = IKCP_PROBE_INIT
  463. kcp.ts_probe = kcp.current + kcp.probe_wait
  464. } else {
  465. if _itimediff(kcp.current, kcp.ts_probe) >= 0 {
  466. if kcp.probe_wait < IKCP_PROBE_INIT {
  467. kcp.probe_wait = IKCP_PROBE_INIT
  468. }
  469. kcp.probe_wait += kcp.probe_wait / 2
  470. if kcp.probe_wait > IKCP_PROBE_LIMIT {
  471. kcp.probe_wait = IKCP_PROBE_LIMIT
  472. }
  473. kcp.ts_probe = kcp.current + kcp.probe_wait
  474. kcp.probe |= IKCP_ASK_SEND
  475. }
  476. }
  477. } else {
  478. kcp.ts_probe = 0
  479. kcp.probe_wait = 0
  480. }*/
  481. // flush window probing commands
  482. /*
  483. if (kcp.probe & IKCP_ASK_SEND) != 0 {
  484. seg.cmd = IKCP_CMD_WASK
  485. size := len(buffer) - len(ptr)
  486. if size+IKCP_OVERHEAD > int(kcp.mtu) {
  487. kcp.output(buffer[:size])
  488. ptr = buffer
  489. }
  490. ptr = seg.encode(ptr)
  491. }*/
  492. // flush window probing commands
  493. /*
  494. if (kcp.probe & IKCP_ASK_TELL) != 0 {
  495. seg.cmd = IKCP_CMD_WINS
  496. size := len(buffer) - len(ptr)
  497. if size+IKCP_OVERHEAD > int(kcp.mtu) {
  498. kcp.output(buffer[:size])
  499. ptr = buffer
  500. }
  501. ptr = seg.encode(ptr)
  502. }
  503. kcp.probe = 0*/
  504. // calculate window size
  505. cwnd := _imin_(kcp.snd_nxt+kcp.snd_wnd, kcp.rmt_wnd)
  506. if kcp.congestionControl {
  507. cwnd = _imin_(kcp.cwnd, cwnd)
  508. }
  509. count = 0
  510. for k := range kcp.snd_queue {
  511. if _itimediff(kcp.snd_nxt, cwnd) >= 0 {
  512. break
  513. }
  514. newseg := kcp.snd_queue[k]
  515. newseg.conv = kcp.conv
  516. newseg.cmd = IKCP_CMD_PUSH
  517. newseg.wnd = seg.wnd
  518. newseg.ts = current
  519. newseg.sn = kcp.snd_nxt
  520. newseg.una = kcp.rcv_nxt
  521. newseg.resendts = current
  522. newseg.fastack = 0
  523. newseg.xmit = 0
  524. kcp.snd_buf = append(kcp.snd_buf, newseg)
  525. kcp.snd_nxt++
  526. count++
  527. }
  528. kcp.snd_queue = kcp.snd_queue[count:]
  529. // calculate resent
  530. resent := uint32(kcp.fastresend)
  531. if kcp.fastresend <= 0 {
  532. resent = 0xffffffff
  533. }
  534. // flush data segments
  535. for _, segment := range kcp.snd_buf {
  536. needsend := false
  537. if segment.xmit == 0 {
  538. needsend = true
  539. segment.xmit++
  540. segment.resendts = current + kcp.rx_rto + kcp.interval
  541. } else if _itimediff(current, segment.resendts) >= 0 {
  542. needsend = true
  543. segment.xmit++
  544. kcp.xmit++
  545. segment.resendts = current + kcp.rx_rto + kcp.interval
  546. //lost = true
  547. } else if segment.fastack >= resent {
  548. needsend = true
  549. segment.xmit++
  550. segment.fastack = 0
  551. segment.resendts = current + kcp.rx_rto + kcp.interval
  552. change++
  553. }
  554. if needsend {
  555. segment.ts = current
  556. segment.wnd = seg.wnd
  557. segment.una = kcp.rcv_nxt
  558. size := len(buffer) - len(ptr)
  559. need := IKCP_OVERHEAD + segment.data.Len()
  560. if size+need >= int(kcp.mtu) {
  561. kcp.output(buffer[:size])
  562. ptr = buffer
  563. }
  564. ptr = segment.encode(ptr)
  565. copy(ptr, segment.data.Value)
  566. ptr = ptr[segment.data.Len():]
  567. if segment.xmit >= kcp.dead_link {
  568. kcp.state = 0xFFFFFFFF
  569. }
  570. }
  571. }
  572. // flash remain segments
  573. size := len(buffer) - len(ptr)
  574. if size > 0 {
  575. kcp.output(buffer[:size])
  576. }
  577. // update ssthresh
  578. // rate halving, https://tools.ietf.org/html/rfc6937
  579. /*
  580. if change != 0 {
  581. inflight := kcp.snd_nxt - kcp.snd_una
  582. kcp.ssthresh = inflight / 2
  583. if kcp.ssthresh < IKCP_THRESH_MIN {
  584. kcp.ssthresh = IKCP_THRESH_MIN
  585. }
  586. kcp.cwnd = kcp.ssthresh + resent
  587. kcp.incr = kcp.cwnd * kcp.mss
  588. }*/
  589. // congestion control, https://tools.ietf.org/html/rfc5681
  590. /*
  591. if lost {
  592. kcp.ssthresh = cwnd / 2
  593. if kcp.ssthresh < IKCP_THRESH_MIN {
  594. kcp.ssthresh = IKCP_THRESH_MIN
  595. }
  596. kcp.cwnd = 1
  597. kcp.incr = kcp.mss
  598. }
  599. if kcp.cwnd < 1 {
  600. kcp.cwnd = 1
  601. kcp.incr = kcp.mss
  602. }*/
  603. }
  604. // Update updates state (call it repeatedly, every 10ms-100ms), or you can ask
  605. // ikcp_check when to call it again (without ikcp_input/_send calling).
  606. // 'current' - current timestamp in millisec.
  607. func (kcp *KCP) Update(current uint32) {
  608. var slap int32
  609. kcp.current = current
  610. if !kcp.updated {
  611. kcp.updated = true
  612. kcp.ts_flush = kcp.current
  613. }
  614. slap = _itimediff(kcp.current, kcp.ts_flush)
  615. if slap >= 10000 || slap < -10000 {
  616. kcp.ts_flush = kcp.current
  617. slap = 0
  618. }
  619. if slap >= 0 {
  620. kcp.ts_flush += kcp.interval
  621. if _itimediff(kcp.current, kcp.ts_flush) >= 0 {
  622. kcp.ts_flush = kcp.current + kcp.interval
  623. }
  624. kcp.flush()
  625. }
  626. }
  627. // Check determines when should you invoke ikcp_update:
  628. // returns when you should invoke ikcp_update in millisec, if there
  629. // is no ikcp_input/_send calling. you can call ikcp_update in that
  630. // time, instead of call update repeatly.
  631. // Important to reduce unnacessary ikcp_update invoking. use it to
  632. // schedule ikcp_update (eg. implementing an epoll-like mechanism,
  633. // or optimize ikcp_update when handling massive kcp connections)
  634. func (kcp *KCP) Check(current uint32) uint32 {
  635. ts_flush := kcp.ts_flush
  636. tm_flush := int32(0x7fffffff)
  637. tm_packet := int32(0x7fffffff)
  638. minimal := uint32(0)
  639. if !kcp.updated {
  640. return current
  641. }
  642. if _itimediff(current, ts_flush) >= 10000 ||
  643. _itimediff(current, ts_flush) < -10000 {
  644. ts_flush = current
  645. }
  646. if _itimediff(current, ts_flush) >= 0 {
  647. return current
  648. }
  649. tm_flush = _itimediff(ts_flush, current)
  650. for _, seg := range kcp.snd_buf {
  651. diff := _itimediff(seg.resendts, current)
  652. if diff <= 0 {
  653. return current
  654. }
  655. if diff < tm_packet {
  656. tm_packet = diff
  657. }
  658. }
  659. minimal = uint32(tm_packet)
  660. if tm_packet >= tm_flush {
  661. minimal = uint32(tm_flush)
  662. }
  663. if minimal >= kcp.interval {
  664. minimal = kcp.interval
  665. }
  666. return current + minimal
  667. }
  668. // NoDelay options
  669. // fastest: ikcp_nodelay(kcp, 1, 20, 2, 1)
  670. // nodelay: 0:disable(default), 1:enable
  671. // interval: internal update timer interval in millisec, default is 100ms
  672. // resend: 0:disable fast resend(default), 1:enable fast resend
  673. // nc: 0:normal congestion control(default), 1:disable congestion control
  674. func (kcp *KCP) NoDelay(interval uint32, resend int, congestionControl bool) int {
  675. kcp.interval = interval
  676. if resend >= 0 {
  677. kcp.fastresend = int32(resend)
  678. }
  679. kcp.congestionControl = congestionControl
  680. return 0
  681. }
  682. // WaitSnd gets how many packet is waiting to be sent
  683. func (kcp *KCP) WaitSnd() uint32 {
  684. return uint32(len(kcp.snd_buf) + len(kcp.snd_queue))
  685. }
  686. func (this *KCP) ClearSendQueue() {
  687. for _, seg := range this.snd_queue {
  688. seg.Release()
  689. }
  690. this.snd_queue = nil
  691. for _, seg := range this.snd_buf {
  692. seg.Release()
  693. }
  694. this.snd_buf = nil
  695. }