ikcp.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. //=====================================================================
  2. //
  3. // KCP - A Better ARQ Protocol Implementation
  4. // skywind3000 (at) gmail.com, 2010-2011
  5. //
  6. // Features:
  7. // + Average RTT reduce 30% - 40% vs traditional ARQ like tcp.
  8. // + Maximum RTT reduce three times vs tcp.
  9. // + Lightweight, distributed as a single source file.
  10. //
  11. //=====================================================================
  12. #include "ikcp.h"
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <time.h>
  19. int(*output)(const char* buf, int len, ikcpcb* kcp, void* user) = 0;
  20. void (*writelog)(const char *log, int len, ikcpcb *kcp, void *user) = 0;
  21. //=====================================================================
  22. // KCP BASIC
  23. //=====================================================================
  24. const IUINT32 IKCP_RTO_NDL = 30; // no delay min rto
  25. const IUINT32 IKCP_RTO_MIN = 100; // normal min rto
  26. const IUINT32 IKCP_RTO_DEF = 200;
  27. const IUINT32 IKCP_RTO_MAX = 60000;
  28. const IUINT32 IKCP_CMD_PUSH = 81; // cmd: push data
  29. const IUINT32 IKCP_CMD_ACK = 82; // cmd: ack
  30. const IUINT32 IKCP_CMD_WASK = 83; // cmd: window probe (ask)
  31. const IUINT32 IKCP_CMD_WINS = 84; // cmd: window size (tell)
  32. const IUINT32 IKCP_ASK_SEND = 1; // need to send IKCP_CMD_WASK
  33. const IUINT32 IKCP_ASK_TELL = 2; // need to send IKCP_CMD_WINS
  34. const IUINT32 IKCP_WND_SND = 32;
  35. const IUINT32 IKCP_WND_RCV = 128; // must >= max fragment size
  36. const IUINT32 IKCP_MTU_DEF = 1400;
  37. const IUINT32 IKCP_ACK_FAST = 3;
  38. const IUINT32 IKCP_INTERVAL = 100;
  39. const IUINT32 IKCP_OVERHEAD = 24;
  40. const IUINT32 IKCP_DEADLINK = 20;
  41. const IUINT32 IKCP_THRESH_INIT = 2;
  42. const IUINT32 IKCP_THRESH_MIN = 2;
  43. const IUINT32 IKCP_PROBE_INIT = 7000; // 7 secs to probe window size
  44. const IUINT32 IKCP_PROBE_LIMIT = 120000; // up to 120 secs to probe window
  45. const IUINT32 IKCP_FASTACK_LIMIT = 5; // max times to trigger fastack
  46. #include <time.h>
  47. #ifdef WIN32
  48. #include <windows.h>
  49. #else
  50. #include <sys/time.h>
  51. #endif
  52. #ifdef WIN32
  53. int gettimeofday(struct timeval* tp, void* tzp)
  54. {
  55. time_t clock;
  56. struct tm tm;
  57. SYSTEMTIME wtm;
  58. GetLocalTime(&wtm);
  59. tm.tm_year = wtm.wYear - 1900;
  60. tm.tm_mon = wtm.wMonth - 1;
  61. tm.tm_mday = wtm.wDay;
  62. tm.tm_hour = wtm.wHour;
  63. tm.tm_min = wtm.wMinute;
  64. tm.tm_sec = wtm.wSecond;
  65. tm.tm_isdst = -1;
  66. clock = mktime(&tm);
  67. tp->tv_sec = clock;
  68. tp->tv_usec = wtm.wMilliseconds * 1000;
  69. return (0);
  70. }
  71. #endif
  72. IINT64 ikcp_get_unixtime()
  73. {
  74. struct timeval tm;
  75. gettimeofday(&tm, NULL);
  76. IINT64 ms = (IINT64)(tm.tv_sec) * 1000 + (IINT64)(tm.tv_usec) / 1000;
  77. return ms;
  78. }
  79. //---------------------------------------------------------------------
  80. // encode / decode
  81. //---------------------------------------------------------------------
  82. /* encode 8 bits unsigned int */
  83. static inline char *ikcp_encode8u(char *p, unsigned char c)
  84. {
  85. *(unsigned char*)p++ = c;
  86. return p;
  87. }
  88. /* decode 8 bits unsigned int */
  89. static inline const char *ikcp_decode8u(const char *p, unsigned char *c)
  90. {
  91. *c = *(unsigned char*)p++;
  92. return p;
  93. }
  94. /* encode 16 bits unsigned int (lsb) */
  95. static inline char *ikcp_encode16u(char *p, unsigned short w)
  96. {
  97. #if IWORDS_BIG_ENDIAN || IWORDS_MUST_ALIGN
  98. *(unsigned char*)(p + 0) = (w & 255);
  99. *(unsigned char*)(p + 1) = (w >> 8);
  100. #else
  101. memcpy(p, &w, 2);
  102. #endif
  103. p += 2;
  104. return p;
  105. }
  106. /* decode 16 bits unsigned int (lsb) */
  107. static inline const char *ikcp_decode16u(const char *p, unsigned short *w)
  108. {
  109. #if IWORDS_BIG_ENDIAN || IWORDS_MUST_ALIGN
  110. *w = *(const unsigned char*)(p + 1);
  111. *w = *(const unsigned char*)(p + 0) + (*w << 8);
  112. #else
  113. memcpy(w, p, 2);
  114. #endif
  115. p += 2;
  116. return p;
  117. }
  118. /* encode 32 bits unsigned int (lsb) */
  119. static inline char *ikcp_encode32u(char *p, IUINT32 l)
  120. {
  121. #if IWORDS_BIG_ENDIAN || IWORDS_MUST_ALIGN
  122. *(unsigned char*)(p + 0) = (unsigned char)((l >> 0) & 0xff);
  123. *(unsigned char*)(p + 1) = (unsigned char)((l >> 8) & 0xff);
  124. *(unsigned char*)(p + 2) = (unsigned char)((l >> 16) & 0xff);
  125. *(unsigned char*)(p + 3) = (unsigned char)((l >> 24) & 0xff);
  126. #else
  127. memcpy(p, &l, 4);
  128. #endif
  129. p += 4;
  130. return p;
  131. }
  132. /* decode 32 bits unsigned int (lsb) */
  133. static inline const char *ikcp_decode32u(const char *p, IUINT32 *l)
  134. {
  135. #if IWORDS_BIG_ENDIAN || IWORDS_MUST_ALIGN
  136. *l = *(const unsigned char*)(p + 3);
  137. *l = *(const unsigned char*)(p + 2) + (*l << 8);
  138. *l = *(const unsigned char*)(p + 1) + (*l << 8);
  139. *l = *(const unsigned char*)(p + 0) + (*l << 8);
  140. #else
  141. memcpy(l, p, 4);
  142. #endif
  143. p += 4;
  144. return p;
  145. }
  146. static inline IUINT32 _imin_(IUINT32 a, IUINT32 b) {
  147. return a <= b ? a : b;
  148. }
  149. static inline IUINT32 _imax_(IUINT32 a, IUINT32 b) {
  150. return a >= b ? a : b;
  151. }
  152. static inline IUINT32 _ibound_(IUINT32 lower, IUINT32 middle, IUINT32 upper)
  153. {
  154. return _imin_(_imax_(lower, middle), upper);
  155. }
  156. static inline long _itimediff(IUINT32 later, IUINT32 earlier)
  157. {
  158. return ((IINT32)(later - earlier));
  159. }
  160. //---------------------------------------------------------------------
  161. // manage segment
  162. //---------------------------------------------------------------------
  163. typedef struct IKCPSEG IKCPSEG;
  164. static void* (*ikcp_malloc_hook)(size_t) = NULL;
  165. static void (*ikcp_free_hook)(void *) = NULL;
  166. // internal malloc
  167. static void* ikcp_malloc(size_t size) {
  168. if (ikcp_malloc_hook)
  169. return ikcp_malloc_hook(size);
  170. return malloc(size);
  171. }
  172. // internal free
  173. static void ikcp_free(void *ptr) {
  174. if (ikcp_free_hook) {
  175. ikcp_free_hook(ptr);
  176. } else {
  177. free(ptr);
  178. }
  179. }
  180. // redefine allocator
  181. void ikcp_allocator(void* (*new_malloc)(size_t), void (*new_free)(void*))
  182. {
  183. ikcp_malloc_hook = new_malloc;
  184. ikcp_free_hook = new_free;
  185. }
  186. // allocate a new kcp segment
  187. static IKCPSEG* ikcp_segment_new(ikcpcb *kcp, int size)
  188. {
  189. return (IKCPSEG*)ikcp_malloc(sizeof(IKCPSEG) + size);
  190. }
  191. // delete a segment
  192. static void ikcp_segment_delete(ikcpcb *kcp, IKCPSEG *seg)
  193. {
  194. ikcp_free(seg);
  195. }
  196. // write log
  197. void ikcp_log(ikcpcb *kcp, int mask, const char *fmt, ...)
  198. {
  199. if (writelog == 0) return;
  200. char buffer[1024];
  201. va_list argptr;
  202. va_start(argptr, fmt);
  203. int n = vsprintf(buffer, fmt, argptr);
  204. va_end(argptr);
  205. writelog(buffer, n, kcp, kcp->user);
  206. }
  207. // check log mask
  208. static int ikcp_canlog(const ikcpcb *kcp, int mask)
  209. {
  210. if (writelog == NULL) return 0;
  211. return 1;
  212. }
  213. // output segment
  214. static int ikcp_output(ikcpcb *kcp, const void *data, int size)
  215. {
  216. assert(kcp);
  217. assert(output);
  218. if (size == 0) return 0;
  219. return output((const char*)data, size, kcp, kcp->user);
  220. }
  221. // output queue
  222. void ikcp_qprint(const char *name, const struct IQUEUEHEAD *head)
  223. {
  224. #if 0
  225. const struct IQUEUEHEAD *p;
  226. printf("<%s>: [", name);
  227. for (p = head->next; p != head; p = p->next) {
  228. const IKCPSEG *seg = iqueue_entry(p, const IKCPSEG, node);
  229. printf("(%lu %d)", (unsigned long)seg->sn, (int)(seg->ts % 10000));
  230. if (p->next != head) printf(",");
  231. }
  232. printf("]\n");
  233. #endif
  234. }
  235. //---------------------------------------------------------------------
  236. // create a new kcpcb
  237. //---------------------------------------------------------------------
  238. ikcpcb* ikcp_create(IUINT32 conv, void *user)
  239. {
  240. ikcpcb *kcp = (ikcpcb*)ikcp_malloc(sizeof(struct IKCPCB));
  241. if (kcp == NULL) return NULL;
  242. kcp->conv = conv;
  243. kcp->user = user;
  244. kcp->snd_una = 0;
  245. kcp->snd_nxt = 0;
  246. kcp->rcv_nxt = 0;
  247. kcp->ts_recent = 0;
  248. kcp->ts_lastack = 0;
  249. kcp->ts_probe = 0;
  250. kcp->probe_wait = 0;
  251. kcp->snd_wnd = IKCP_WND_SND;
  252. kcp->rcv_wnd = IKCP_WND_RCV;
  253. kcp->rmt_wnd = IKCP_WND_RCV;
  254. kcp->cwnd = 0;
  255. kcp->incr = 0;
  256. kcp->probe = 0;
  257. kcp->mtu = IKCP_MTU_DEF;
  258. kcp->mss = kcp->mtu - IKCP_OVERHEAD;
  259. kcp->stream = 0;
  260. kcp->buffer = (char*)ikcp_malloc((kcp->mtu + IKCP_OVERHEAD) * 3);
  261. if (kcp->buffer == NULL) {
  262. ikcp_free(kcp);
  263. return NULL;
  264. }
  265. iqueue_init(&kcp->snd_queue);
  266. iqueue_init(&kcp->rcv_queue);
  267. iqueue_init(&kcp->snd_buf);
  268. iqueue_init(&kcp->rcv_buf);
  269. kcp->nrcv_buf = 0;
  270. kcp->nsnd_buf = 0;
  271. kcp->nrcv_que = 0;
  272. kcp->nsnd_que = 0;
  273. kcp->state = 0;
  274. kcp->acklist = NULL;
  275. kcp->ackblock = 0;
  276. kcp->ackcount = 0;
  277. kcp->rx_srtt = 0;
  278. kcp->rx_rttval = 0;
  279. kcp->rx_rto = IKCP_RTO_DEF;
  280. kcp->rx_minrto = IKCP_RTO_MIN;
  281. kcp->current = 0;
  282. kcp->interval = IKCP_INTERVAL;
  283. kcp->ts_flush = IKCP_INTERVAL;
  284. kcp->nodelay = 0;
  285. kcp->updated = 0;
  286. kcp->logmask = 0;
  287. kcp->ssthresh = IKCP_THRESH_INIT;
  288. kcp->fastresend = 0;
  289. kcp->fastlimit = IKCP_FASTACK_LIMIT;
  290. kcp->nocwnd = 0;
  291. kcp->xmit = 0;
  292. kcp->dead_link = IKCP_DEADLINK;
  293. return kcp;
  294. }
  295. //---------------------------------------------------------------------
  296. // release a new kcpcb
  297. //---------------------------------------------------------------------
  298. void ikcp_release(ikcpcb *kcp)
  299. {
  300. assert(kcp);
  301. if (kcp) {
  302. IKCPSEG *seg;
  303. while (!iqueue_is_empty(&kcp->snd_buf)) {
  304. seg = iqueue_entry(kcp->snd_buf.next, IKCPSEG, node);
  305. iqueue_del(&seg->node);
  306. ikcp_segment_delete(kcp, seg);
  307. }
  308. while (!iqueue_is_empty(&kcp->rcv_buf)) {
  309. seg = iqueue_entry(kcp->rcv_buf.next, IKCPSEG, node);
  310. iqueue_del(&seg->node);
  311. ikcp_segment_delete(kcp, seg);
  312. }
  313. while (!iqueue_is_empty(&kcp->snd_queue)) {
  314. seg = iqueue_entry(kcp->snd_queue.next, IKCPSEG, node);
  315. iqueue_del(&seg->node);
  316. ikcp_segment_delete(kcp, seg);
  317. }
  318. while (!iqueue_is_empty(&kcp->rcv_queue)) {
  319. seg = iqueue_entry(kcp->rcv_queue.next, IKCPSEG, node);
  320. iqueue_del(&seg->node);
  321. ikcp_segment_delete(kcp, seg);
  322. }
  323. if (kcp->buffer) {
  324. ikcp_free(kcp->buffer);
  325. }
  326. if (kcp->acklist) {
  327. ikcp_free(kcp->acklist);
  328. }
  329. kcp->nrcv_buf = 0;
  330. kcp->nsnd_buf = 0;
  331. kcp->nrcv_que = 0;
  332. kcp->nsnd_que = 0;
  333. kcp->ackcount = 0;
  334. kcp->buffer = NULL;
  335. kcp->acklist = NULL;
  336. ikcp_free(kcp);
  337. }
  338. }
  339. //---------------------------------------------------------------------
  340. // set output callback, which will be invoked by kcp
  341. //---------------------------------------------------------------------
  342. void ikcp_setoutput(int(*op)(const char *buf, int len, ikcpcb *kcp, void *user))
  343. {
  344. output = op;
  345. }
  346. void ikcp_setlog(void(*op)(const char *buf, int len, ikcpcb *kcp, void *user))
  347. {
  348. writelog = op;
  349. }
  350. //---------------------------------------------------------------------
  351. // user/upper level recv: returns size, returns below zero for EAGAIN
  352. //---------------------------------------------------------------------
  353. int ikcp_recv(ikcpcb *kcp, char *buffer, int len)
  354. {
  355. struct IQUEUEHEAD *p;
  356. int ispeek = (len < 0)? 1 : 0;
  357. int peeksize;
  358. int recover = 0;
  359. IKCPSEG *seg;
  360. assert(kcp);
  361. if (iqueue_is_empty(&kcp->rcv_queue))
  362. return -1;
  363. if (len < 0) len = -len;
  364. peeksize = ikcp_peeksize(kcp);
  365. if (peeksize < 0)
  366. return -2;
  367. if (peeksize > len)
  368. return -3;
  369. if (kcp->nrcv_que >= kcp->rcv_wnd)
  370. recover = 1;
  371. // merge fragment
  372. for (len = 0, p = kcp->rcv_queue.next; p != &kcp->rcv_queue; ) {
  373. int fragment;
  374. seg = iqueue_entry(p, IKCPSEG, node);
  375. p = p->next;
  376. if (buffer) {
  377. memcpy(buffer, seg->data, seg->len);
  378. buffer += seg->len;
  379. }
  380. len += seg->len;
  381. fragment = seg->frg;
  382. if (ikcp_canlog(kcp, IKCP_LOG_RECV)) {
  383. ikcp_log(kcp, IKCP_LOG_RECV, "recv sn=%lu", (unsigned long)seg->sn);
  384. }
  385. if (ispeek == 0) {
  386. iqueue_del(&seg->node);
  387. ikcp_segment_delete(kcp, seg);
  388. kcp->nrcv_que--;
  389. }
  390. if (fragment == 0)
  391. break;
  392. }
  393. assert(len == peeksize);
  394. // move available data from rcv_buf -> rcv_queue
  395. while (! iqueue_is_empty(&kcp->rcv_buf)) {
  396. seg = iqueue_entry(kcp->rcv_buf.next, IKCPSEG, node);
  397. if (seg->sn == kcp->rcv_nxt && kcp->nrcv_que < kcp->rcv_wnd) {
  398. iqueue_del(&seg->node);
  399. kcp->nrcv_buf--;
  400. iqueue_add_tail(&seg->node, &kcp->rcv_queue);
  401. kcp->nrcv_que++;
  402. kcp->rcv_nxt++;
  403. } else {
  404. break;
  405. }
  406. }
  407. // fast recover
  408. if (kcp->nrcv_que < kcp->rcv_wnd && recover) {
  409. // ready to send back IKCP_CMD_WINS in ikcp_flush
  410. // tell remote my window size
  411. kcp->probe |= IKCP_ASK_TELL;
  412. }
  413. return len;
  414. }
  415. //---------------------------------------------------------------------
  416. // peek data size
  417. //---------------------------------------------------------------------
  418. int ikcp_peeksize(const ikcpcb *kcp)
  419. {
  420. struct IQUEUEHEAD *p;
  421. IKCPSEG *seg;
  422. int length = 0;
  423. assert(kcp);
  424. if (iqueue_is_empty(&kcp->rcv_queue)) return -1;
  425. seg = iqueue_entry(kcp->rcv_queue.next, IKCPSEG, node);
  426. if (seg->frg == 0) return seg->len;
  427. if (kcp->nrcv_que < seg->frg + 1) return -1;
  428. for (p = kcp->rcv_queue.next; p != &kcp->rcv_queue; p = p->next) {
  429. seg = iqueue_entry(p, IKCPSEG, node);
  430. length += seg->len;
  431. if (seg->frg == 0) break;
  432. }
  433. return length;
  434. }
  435. //---------------------------------------------------------------------
  436. // user/upper level send, returns below zero for error
  437. //---------------------------------------------------------------------
  438. int ikcp_send(ikcpcb *kcp, const char *buffer, int offset, int len)
  439. {
  440. IKCPSEG *seg;
  441. int count, i;
  442. assert(kcp->mss > 0);
  443. if (len < 0) return -1;
  444. buffer += offset;
  445. // append to previous segment in streaming mode (if possible)
  446. if (kcp->stream != 0) {
  447. if (!iqueue_is_empty(&kcp->snd_queue)) {
  448. IKCPSEG *old = iqueue_entry(kcp->snd_queue.prev, IKCPSEG, node);
  449. if (old->len < kcp->mss) {
  450. int capacity = kcp->mss - old->len;
  451. int extend = (len < capacity)? len : capacity;
  452. seg = ikcp_segment_new(kcp, old->len + extend);
  453. assert(seg);
  454. if (seg == NULL) {
  455. return -2;
  456. }
  457. iqueue_add_tail(&seg->node, &kcp->snd_queue);
  458. memcpy(seg->data, old->data, old->len);
  459. if (buffer) {
  460. memcpy(seg->data + old->len, buffer, extend);
  461. buffer += extend;
  462. }
  463. seg->len = old->len + extend;
  464. seg->frg = 0;
  465. len -= extend;
  466. iqueue_del_init(&old->node);
  467. ikcp_segment_delete(kcp, old);
  468. }
  469. }
  470. if (len <= 0) {
  471. return 0;
  472. }
  473. }
  474. if (len <= (int)kcp->mss) count = 1;
  475. else count = (len + kcp->mss - 1) / kcp->mss;
  476. if (count >= (int)IKCP_WND_RCV) return -2;
  477. if (count == 0) count = 1;
  478. // fragment
  479. for (i = 0; i < count; i++) {
  480. int size = len > (int)kcp->mss ? (int)kcp->mss : len;
  481. seg = ikcp_segment_new(kcp, size);
  482. assert(seg);
  483. if (seg == NULL) {
  484. return -2;
  485. }
  486. if (buffer && len > 0) {
  487. memcpy(seg->data, buffer, size);
  488. }
  489. seg->len = size;
  490. seg->frg = (kcp->stream == 0)? (count - i - 1) : 0;
  491. iqueue_init(&seg->node);
  492. iqueue_add_tail(&seg->node, &kcp->snd_queue);
  493. kcp->nsnd_que++;
  494. if (buffer) {
  495. buffer += size;
  496. }
  497. len -= size;
  498. }
  499. return 0;
  500. }
  501. //---------------------------------------------------------------------
  502. // parse ack
  503. //---------------------------------------------------------------------
  504. static void ikcp_update_ack(ikcpcb *kcp, IINT32 rtt)
  505. {
  506. IINT32 rto = 0;
  507. if (kcp->rx_srtt == 0) {
  508. kcp->rx_srtt = rtt;
  509. kcp->rx_rttval = rtt / 2;
  510. } else {
  511. long delta = rtt - kcp->rx_srtt;
  512. if (delta < 0) delta = -delta;
  513. kcp->rx_rttval = (3 * kcp->rx_rttval + delta) / 4;
  514. kcp->rx_srtt = (7 * kcp->rx_srtt + rtt) / 8;
  515. if (kcp->rx_srtt < 1) kcp->rx_srtt = 1;
  516. }
  517. rto = kcp->rx_srtt + _imax_(kcp->interval, 4 * kcp->rx_rttval);
  518. kcp->rx_rto = _ibound_(kcp->rx_minrto, rto, IKCP_RTO_MAX);
  519. }
  520. static void ikcp_shrink_buf(ikcpcb *kcp)
  521. {
  522. struct IQUEUEHEAD *p = kcp->snd_buf.next;
  523. if (p != &kcp->snd_buf) {
  524. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  525. kcp->snd_una = seg->sn;
  526. } else {
  527. kcp->snd_una = kcp->snd_nxt;
  528. }
  529. }
  530. static void ikcp_parse_ack(ikcpcb *kcp, IUINT32 sn)
  531. {
  532. struct IQUEUEHEAD *p, *next;
  533. if (_itimediff(sn, kcp->snd_una) < 0 || _itimediff(sn, kcp->snd_nxt) >= 0)
  534. return;
  535. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = next) {
  536. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  537. next = p->next;
  538. if (sn == seg->sn) {
  539. iqueue_del(p);
  540. ikcp_segment_delete(kcp, seg);
  541. kcp->nsnd_buf--;
  542. break;
  543. }
  544. if (_itimediff(sn, seg->sn) < 0) {
  545. break;
  546. }
  547. }
  548. }
  549. static void ikcp_parse_una(ikcpcb *kcp, IUINT32 una)
  550. {
  551. struct IQUEUEHEAD *p, *next;
  552. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = next) {
  553. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  554. next = p->next;
  555. if (_itimediff(una, seg->sn) > 0) {
  556. iqueue_del(p);
  557. ikcp_segment_delete(kcp, seg);
  558. kcp->nsnd_buf--;
  559. } else {
  560. break;
  561. }
  562. }
  563. }
  564. static void ikcp_parse_fastack(ikcpcb *kcp, IUINT32 sn, IUINT32 ts)
  565. {
  566. struct IQUEUEHEAD *p, *next;
  567. if (_itimediff(sn, kcp->snd_una) < 0 || _itimediff(sn, kcp->snd_nxt) >= 0)
  568. return;
  569. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = next) {
  570. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  571. next = p->next;
  572. if (_itimediff(sn, seg->sn) < 0) {
  573. break;
  574. }
  575. else if (sn != seg->sn) {
  576. #ifndef IKCP_FASTACK_CONSERVE
  577. seg->fastack++;
  578. #else
  579. if (_itimediff(ts, seg->ts) >= 0)
  580. seg->fastack++;
  581. #endif
  582. }
  583. }
  584. }
  585. //---------------------------------------------------------------------
  586. // ack append
  587. //---------------------------------------------------------------------
  588. static void ikcp_ack_push(ikcpcb *kcp, IUINT32 sn, IUINT32 ts)
  589. {
  590. size_t newsize = kcp->ackcount + 1;
  591. IUINT32 *ptr;
  592. if (newsize > kcp->ackblock) {
  593. IUINT32 *acklist;
  594. size_t newblock;
  595. for (newblock = 8; newblock < newsize; newblock <<= 1);
  596. acklist = (IUINT32*)ikcp_malloc(newblock * sizeof(IUINT32) * 2);
  597. if (acklist == NULL) {
  598. assert(acklist != NULL);
  599. abort();
  600. }
  601. if (kcp->acklist != NULL) {
  602. size_t x;
  603. for (x = 0; x < kcp->ackcount; x++) {
  604. acklist[x * 2 + 0] = kcp->acklist[x * 2 + 0];
  605. acklist[x * 2 + 1] = kcp->acklist[x * 2 + 1];
  606. }
  607. ikcp_free(kcp->acklist);
  608. }
  609. kcp->acklist = acklist;
  610. kcp->ackblock = newblock;
  611. }
  612. ptr = &kcp->acklist[kcp->ackcount * 2];
  613. ptr[0] = sn;
  614. ptr[1] = ts;
  615. kcp->ackcount++;
  616. }
  617. static void ikcp_ack_get(const ikcpcb *kcp, int p, IUINT32 *sn, IUINT32 *ts)
  618. {
  619. if (sn) sn[0] = kcp->acklist[p * 2 + 0];
  620. if (ts) ts[0] = kcp->acklist[p * 2 + 1];
  621. }
  622. //---------------------------------------------------------------------
  623. // parse data
  624. //---------------------------------------------------------------------
  625. void ikcp_parse_data(ikcpcb *kcp, IKCPSEG *newseg)
  626. {
  627. struct IQUEUEHEAD *p, *prev;
  628. IUINT32 sn = newseg->sn;
  629. int repeat = 0;
  630. if (_itimediff(sn, kcp->rcv_nxt + kcp->rcv_wnd) >= 0 ||
  631. _itimediff(sn, kcp->rcv_nxt) < 0) {
  632. ikcp_segment_delete(kcp, newseg);
  633. return;
  634. }
  635. for (p = kcp->rcv_buf.prev; p != &kcp->rcv_buf; p = prev) {
  636. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  637. prev = p->prev;
  638. if (seg->sn == sn) {
  639. repeat = 1;
  640. break;
  641. }
  642. if (_itimediff(sn, seg->sn) > 0) {
  643. break;
  644. }
  645. }
  646. if (repeat == 0) {
  647. iqueue_init(&newseg->node);
  648. iqueue_add(&newseg->node, p);
  649. kcp->nrcv_buf++;
  650. } else {
  651. ikcp_segment_delete(kcp, newseg);
  652. }
  653. #if 0
  654. ikcp_qprint("rcvbuf", &kcp->rcv_buf);
  655. printf("rcv_nxt=%lu\n", kcp->rcv_nxt);
  656. #endif
  657. // move available data from rcv_buf -> rcv_queue
  658. while (! iqueue_is_empty(&kcp->rcv_buf)) {
  659. IKCPSEG *seg = iqueue_entry(kcp->rcv_buf.next, IKCPSEG, node);
  660. if (seg->sn == kcp->rcv_nxt && kcp->nrcv_que < kcp->rcv_wnd) {
  661. iqueue_del(&seg->node);
  662. kcp->nrcv_buf--;
  663. iqueue_add_tail(&seg->node, &kcp->rcv_queue);
  664. kcp->nrcv_que++;
  665. kcp->rcv_nxt++;
  666. } else {
  667. break;
  668. }
  669. }
  670. #if 0
  671. ikcp_qprint("queue", &kcp->rcv_queue);
  672. printf("rcv_nxt=%lu\n", kcp->rcv_nxt);
  673. #endif
  674. #if 1
  675. // printf("snd(buf=%d, queue=%d)\n", kcp->nsnd_buf, kcp->nsnd_que);
  676. // printf("rcv(buf=%d, queue=%d)\n", kcp->nrcv_buf, kcp->nrcv_que);
  677. #endif
  678. }
  679. //---------------------------------------------------------------------
  680. // input data
  681. //---------------------------------------------------------------------
  682. int ikcp_input(ikcpcb *kcp, const char *data, int offset, int size)
  683. {
  684. IUINT32 prev_una = kcp->snd_una;
  685. IUINT32 maxack = 0, latest_ts = 0;
  686. int flag = 0;
  687. if (data == NULL || (int)size < (int)IKCP_OVERHEAD) return -1;
  688. data += offset;
  689. while (1) {
  690. IUINT32 ts, sn, len, una, conv;
  691. IUINT16 wnd;
  692. IUINT8 cmd, frg;
  693. IKCPSEG *seg;
  694. if (size < (int)IKCP_OVERHEAD) break;
  695. data = ikcp_decode32u(data, &conv);
  696. /*if (conv != kcp->conv) return -1;*/
  697. data = ikcp_decode8u(data, &cmd);
  698. data = ikcp_decode8u(data, &frg);
  699. data = ikcp_decode16u(data, &wnd);
  700. data = ikcp_decode32u(data, &ts);
  701. data = ikcp_decode32u(data, &sn);
  702. data = ikcp_decode32u(data, &una);
  703. data = ikcp_decode32u(data, &len);
  704. size -= IKCP_OVERHEAD;
  705. if ((long)size < (long)len || (int)len < 0) return -2;
  706. if (cmd != IKCP_CMD_PUSH && cmd != IKCP_CMD_ACK &&
  707. cmd != IKCP_CMD_WASK && cmd != IKCP_CMD_WINS)
  708. return -3;
  709. kcp->rmt_wnd = wnd;
  710. ikcp_parse_una(kcp, una);
  711. ikcp_shrink_buf(kcp);
  712. if (cmd == IKCP_CMD_ACK) {
  713. if (_itimediff(kcp->current, ts) >= 0) {
  714. ikcp_update_ack(kcp, _itimediff(kcp->current, ts));
  715. }
  716. ikcp_parse_ack(kcp, sn);
  717. ikcp_shrink_buf(kcp);
  718. if (flag == 0) {
  719. flag = 1;
  720. maxack = sn;
  721. latest_ts = ts;
  722. } else {
  723. if (_itimediff(sn, maxack) > 0) {
  724. #ifndef IKCP_FASTACK_CONSERVE
  725. maxack = sn;
  726. latest_ts = ts;
  727. #else
  728. if (_itimediff(ts, latest_ts) > 0) {
  729. maxack = sn;
  730. latest_ts = ts;
  731. }
  732. #endif
  733. }
  734. }
  735. if (ikcp_canlog(kcp, IKCP_LOG_IN_ACK)) {
  736. ikcp_log(kcp, IKCP_LOG_IN_ACK,
  737. "input ack: sn=%lu rtt=%ld rto=%ld", (unsigned long)sn,
  738. (long)_itimediff(kcp->current, ts),
  739. (long)kcp->rx_rto);
  740. }
  741. }
  742. else if (cmd == IKCP_CMD_PUSH) {
  743. if (ikcp_canlog(kcp, IKCP_LOG_IN_DATA)) {
  744. ikcp_log(kcp, IKCP_LOG_IN_DATA,
  745. "input psh: sn=%lu ts=%lu", (unsigned long)sn, (unsigned long)ts);
  746. }
  747. if (_itimediff(sn, kcp->rcv_nxt + kcp->rcv_wnd) < 0) {
  748. ikcp_ack_push(kcp, sn, ts);
  749. if (_itimediff(sn, kcp->rcv_nxt) >= 0) {
  750. seg = ikcp_segment_new(kcp, len);
  751. seg->conv = conv;
  752. seg->cmd = cmd;
  753. seg->frg = frg;
  754. seg->wnd = wnd;
  755. seg->ts = ts;
  756. seg->sn = sn;
  757. seg->una = una;
  758. seg->len = len;
  759. if (len > 0) {
  760. memcpy(seg->data, data, len);
  761. }
  762. ikcp_parse_data(kcp, seg);
  763. }
  764. }
  765. }
  766. else if (cmd == IKCP_CMD_WASK) {
  767. // ready to send back IKCP_CMD_WINS in ikcp_flush
  768. // tell remote my window size
  769. kcp->probe |= IKCP_ASK_TELL;
  770. if (ikcp_canlog(kcp, IKCP_LOG_IN_PROBE)) {
  771. ikcp_log(kcp, IKCP_LOG_IN_PROBE, "input probe");
  772. }
  773. }
  774. else if (cmd == IKCP_CMD_WINS) {
  775. // do nothing
  776. if (ikcp_canlog(kcp, IKCP_LOG_IN_WINS)) {
  777. ikcp_log(kcp, IKCP_LOG_IN_WINS,
  778. "input wins: %lu", (unsigned long)(wnd));
  779. }
  780. }
  781. else {
  782. return -3;
  783. }
  784. data += len;
  785. size -= len;
  786. }
  787. if (flag != 0) {
  788. ikcp_parse_fastack(kcp, maxack, latest_ts);
  789. }
  790. if (_itimediff(kcp->snd_una, prev_una) > 0) {
  791. if (kcp->cwnd < kcp->rmt_wnd) {
  792. IUINT32 mss = kcp->mss;
  793. if (kcp->cwnd < kcp->ssthresh) {
  794. kcp->cwnd++;
  795. kcp->incr += mss;
  796. } else {
  797. if (kcp->incr < mss) kcp->incr = mss;
  798. kcp->incr += (mss * mss) / kcp->incr + (mss / 16);
  799. if ((kcp->cwnd + 1) * mss <= kcp->incr) {
  800. #if 1
  801. kcp->cwnd = (kcp->incr + mss - 1) / ((mss > 0)? mss : 1);
  802. #else
  803. kcp->cwnd++;
  804. #endif
  805. }
  806. }
  807. if (kcp->cwnd > kcp->rmt_wnd) {
  808. kcp->cwnd = kcp->rmt_wnd;
  809. kcp->incr = kcp->rmt_wnd * mss;
  810. }
  811. }
  812. }
  813. return 0;
  814. }
  815. //---------------------------------------------------------------------
  816. // ikcp_encode_seg
  817. //---------------------------------------------------------------------
  818. static char *ikcp_encode_seg(char *ptr, const IKCPSEG *seg)
  819. {
  820. ptr = ikcp_encode32u(ptr, seg->conv);
  821. ptr = ikcp_encode8u(ptr, (IUINT8)seg->cmd);
  822. ptr = ikcp_encode8u(ptr, (IUINT8)seg->frg);
  823. ptr = ikcp_encode16u(ptr, (IUINT16)seg->wnd);
  824. ptr = ikcp_encode32u(ptr, seg->ts);
  825. ptr = ikcp_encode32u(ptr, seg->sn);
  826. ptr = ikcp_encode32u(ptr, seg->una);
  827. ptr = ikcp_encode32u(ptr, seg->len);
  828. return ptr;
  829. }
  830. static int ikcp_wnd_unused(const ikcpcb *kcp)
  831. {
  832. if (kcp->nrcv_que < kcp->rcv_wnd) {
  833. return kcp->rcv_wnd - kcp->nrcv_que;
  834. }
  835. return 0;
  836. }
  837. //---------------------------------------------------------------------
  838. // ikcp_flush
  839. //---------------------------------------------------------------------
  840. void ikcp_flush(ikcpcb *kcp)
  841. {
  842. IUINT32 current = kcp->current;
  843. char *buffer = kcp->buffer;
  844. char *ptr = buffer;
  845. int count, size, i;
  846. IUINT32 resent, cwnd;
  847. IUINT32 rtomin;
  848. struct IQUEUEHEAD *p;
  849. int change = 0;
  850. int lost = 0;
  851. IKCPSEG seg;
  852. // 'ikcp_update' haven't been called.
  853. if (kcp->updated == 0) return;
  854. seg.conv = kcp->conv;
  855. seg.cmd = IKCP_CMD_ACK;
  856. seg.frg = 0;
  857. seg.wnd = ikcp_wnd_unused(kcp);
  858. seg.una = kcp->rcv_nxt;
  859. seg.len = 0;
  860. seg.sn = 0;
  861. seg.ts = 0;
  862. // flush acknowledges
  863. count = kcp->ackcount;
  864. for (i = 0; i < count; i++) {
  865. size = (int)(ptr - buffer);
  866. if (size + (int)IKCP_OVERHEAD > (int)kcp->mtu) {
  867. ikcp_output(kcp, buffer, size);
  868. ptr = buffer;
  869. }
  870. ikcp_ack_get(kcp, i, &seg.sn, &seg.ts);
  871. ptr = ikcp_encode_seg(ptr, &seg);
  872. }
  873. kcp->ackcount = 0;
  874. // probe window size (if remote window size equals zero)
  875. if (kcp->rmt_wnd == 0) {
  876. if (kcp->probe_wait == 0) {
  877. kcp->probe_wait = IKCP_PROBE_INIT;
  878. kcp->ts_probe = kcp->current + kcp->probe_wait;
  879. }
  880. else {
  881. if (_itimediff(kcp->current, kcp->ts_probe) >= 0) {
  882. if (kcp->probe_wait < IKCP_PROBE_INIT)
  883. kcp->probe_wait = IKCP_PROBE_INIT;
  884. kcp->probe_wait += kcp->probe_wait / 2;
  885. if (kcp->probe_wait > IKCP_PROBE_LIMIT)
  886. kcp->probe_wait = IKCP_PROBE_LIMIT;
  887. kcp->ts_probe = kcp->current + kcp->probe_wait;
  888. kcp->probe |= IKCP_ASK_SEND;
  889. }
  890. }
  891. } else {
  892. kcp->ts_probe = 0;
  893. kcp->probe_wait = 0;
  894. }
  895. // flush window probing commands
  896. if (kcp->probe & IKCP_ASK_SEND) {
  897. seg.cmd = IKCP_CMD_WASK;
  898. size = (int)(ptr - buffer);
  899. if (size + (int)IKCP_OVERHEAD > (int)kcp->mtu) {
  900. ikcp_output(kcp, buffer, size);
  901. ptr = buffer;
  902. }
  903. ptr = ikcp_encode_seg(ptr, &seg);
  904. }
  905. // flush window probing commands
  906. if (kcp->probe & IKCP_ASK_TELL) {
  907. seg.cmd = IKCP_CMD_WINS;
  908. size = (int)(ptr - buffer);
  909. if (size + (int)IKCP_OVERHEAD > (int)kcp->mtu) {
  910. ikcp_output(kcp, buffer, size);
  911. ptr = buffer;
  912. }
  913. ptr = ikcp_encode_seg(ptr, &seg);
  914. }
  915. kcp->probe = 0;
  916. // calculate window size
  917. cwnd = _imin_(kcp->snd_wnd, kcp->rmt_wnd);
  918. if (kcp->nocwnd == 0) cwnd = _imin_(kcp->cwnd, cwnd);
  919. // move data from snd_queue to snd_buf
  920. while (_itimediff(kcp->snd_nxt, kcp->snd_una + cwnd) < 0) {
  921. IKCPSEG *newseg;
  922. if (iqueue_is_empty(&kcp->snd_queue)) break;
  923. newseg = iqueue_entry(kcp->snd_queue.next, IKCPSEG, node);
  924. iqueue_del(&newseg->node);
  925. iqueue_add_tail(&newseg->node, &kcp->snd_buf);
  926. kcp->nsnd_que--;
  927. kcp->nsnd_buf++;
  928. newseg->conv = kcp->conv;
  929. newseg->cmd = IKCP_CMD_PUSH;
  930. newseg->wnd = seg.wnd;
  931. newseg->ts = current;
  932. newseg->sn = kcp->snd_nxt++;
  933. newseg->una = kcp->rcv_nxt;
  934. newseg->resendts = current;
  935. newseg->rto = kcp->rx_rto;
  936. newseg->fastack = 0;
  937. newseg->xmit = 0;
  938. }
  939. // calculate resent
  940. resent = (kcp->fastresend > 0)? (IUINT32)kcp->fastresend : 0xffffffff;
  941. rtomin = (kcp->nodelay == 0)? (kcp->rx_rto >> 3) : 0;
  942. // flush data segments
  943. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = p->next) {
  944. IKCPSEG *segment = iqueue_entry(p, IKCPSEG, node);
  945. int needsend = 0;
  946. if (segment->xmit == 0) {
  947. needsend = 1;
  948. segment->xmit++;
  949. segment->rto = kcp->rx_rto;
  950. segment->resendts = current + segment->rto + rtomin;
  951. }
  952. else if (_itimediff(current, segment->resendts) >= 0) {
  953. needsend = 1;
  954. segment->xmit++;
  955. kcp->xmit++;
  956. if (kcp->nodelay == 0) {
  957. segment->rto += _imax_(segment->rto, (IUINT32)kcp->rx_rto);
  958. } else {
  959. IINT32 step = (kcp->nodelay < 2)?
  960. ((IINT32)(segment->rto)) : kcp->rx_rto;
  961. segment->rto += step / 2;
  962. }
  963. segment->resendts = current + segment->rto;
  964. lost = 1;
  965. }
  966. else if (segment->fastack >= resent) {
  967. if ((int)segment->xmit <= kcp->fastlimit ||
  968. kcp->fastlimit <= 0) {
  969. needsend = 1;
  970. segment->xmit++;
  971. segment->fastack = 0;
  972. segment->resendts = current + segment->rto;
  973. change++;
  974. }
  975. }
  976. if (needsend) {
  977. int need;
  978. segment->ts = current;
  979. segment->wnd = seg.wnd;
  980. segment->una = kcp->rcv_nxt;
  981. size = (int)(ptr - buffer);
  982. need = IKCP_OVERHEAD + segment->len;
  983. if (size + need > (int)kcp->mtu) {
  984. ikcp_output(kcp, buffer, size);
  985. ptr = buffer;
  986. }
  987. ptr = ikcp_encode_seg(ptr, segment);
  988. if (segment->len > 0) {
  989. memcpy(ptr, segment->data, segment->len);
  990. ptr += segment->len;
  991. }
  992. if (segment->xmit >= kcp->dead_link) {
  993. kcp->state = (IUINT32)-1;
  994. }
  995. }
  996. }
  997. // flash remain segments
  998. size = (int)(ptr - buffer);
  999. if (size > 0) {
  1000. ikcp_output(kcp, buffer, size);
  1001. }
  1002. // update ssthresh
  1003. if (change) {
  1004. IUINT32 inflight = kcp->snd_nxt - kcp->snd_una;
  1005. kcp->ssthresh = inflight / 2;
  1006. if (kcp->ssthresh < IKCP_THRESH_MIN)
  1007. kcp->ssthresh = IKCP_THRESH_MIN;
  1008. kcp->cwnd = kcp->ssthresh + resent;
  1009. kcp->incr = kcp->cwnd * kcp->mss;
  1010. }
  1011. if (lost) {
  1012. kcp->ssthresh = cwnd / 2;
  1013. if (kcp->ssthresh < IKCP_THRESH_MIN)
  1014. kcp->ssthresh = IKCP_THRESH_MIN;
  1015. kcp->cwnd = 1;
  1016. kcp->incr = kcp->mss;
  1017. }
  1018. if (kcp->cwnd < 1) {
  1019. kcp->cwnd = 1;
  1020. kcp->incr = kcp->mss;
  1021. }
  1022. }
  1023. //---------------------------------------------------------------------
  1024. // update state (call it repeatedly, every 10ms-100ms), or you can ask
  1025. // ikcp_check when to call it again (without ikcp_input/_send calling).
  1026. // 'current' - current timestamp in millisec.
  1027. //---------------------------------------------------------------------
  1028. void ikcp_update(ikcpcb *kcp, IUINT32 current)
  1029. {
  1030. IINT32 slap;
  1031. kcp->current = current;
  1032. if (kcp->updated == 0) {
  1033. kcp->updated = 1;
  1034. kcp->ts_flush = kcp->current;
  1035. }
  1036. slap = _itimediff(kcp->current, kcp->ts_flush);
  1037. if (slap >= 10000 || slap < -10000) {
  1038. kcp->ts_flush = kcp->current;
  1039. slap = 0;
  1040. }
  1041. if (slap >= 0) {
  1042. kcp->ts_flush += kcp->interval;
  1043. if (_itimediff(kcp->current, kcp->ts_flush) >= 0) {
  1044. kcp->ts_flush = kcp->current + kcp->interval;
  1045. }
  1046. ikcp_flush(kcp);
  1047. }
  1048. }
  1049. //---------------------------------------------------------------------
  1050. // Determine when should you invoke ikcp_update:
  1051. // returns when you should invoke ikcp_update in millisec, if there
  1052. // is no ikcp_input/_send calling. you can call ikcp_update in that
  1053. // time, instead of call update repeatly.
  1054. // Important to reduce unnacessary ikcp_update invoking. use it to
  1055. // schedule ikcp_update (eg. implementing an epoll-like mechanism,
  1056. // or optimize ikcp_update when handling massive kcp connections)
  1057. //---------------------------------------------------------------------
  1058. IUINT32 ikcp_check(const ikcpcb *kcp, IUINT32 current)
  1059. {
  1060. IUINT32 ts_flush = kcp->ts_flush;
  1061. IINT32 tm_flush = 0x7fffffff;
  1062. IINT32 tm_packet = 0x7fffffff;
  1063. IUINT32 minimal = 0;
  1064. struct IQUEUEHEAD *p;
  1065. if (kcp->updated == 0) {
  1066. return current;
  1067. }
  1068. if (_itimediff(current, ts_flush) >= 10000 ||
  1069. _itimediff(current, ts_flush) < -10000) {
  1070. ts_flush = current;
  1071. }
  1072. if (_itimediff(current, ts_flush) >= 0) {
  1073. return current;
  1074. }
  1075. tm_flush = _itimediff(ts_flush, current);
  1076. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = p->next) {
  1077. const IKCPSEG *seg = iqueue_entry(p, const IKCPSEG, node);
  1078. IINT32 diff = _itimediff(seg->resendts, current);
  1079. if (diff <= 0) {
  1080. return current;
  1081. }
  1082. if (diff < tm_packet) tm_packet = diff;
  1083. }
  1084. minimal = (IUINT32)(tm_packet < tm_flush ? tm_packet : tm_flush);
  1085. if (minimal >= kcp->interval) minimal = kcp->interval;
  1086. return current + minimal;
  1087. }
  1088. int ikcp_setmtu(ikcpcb *kcp, int mtu)
  1089. {
  1090. char *buffer;
  1091. if (mtu < 50 || mtu < (int)IKCP_OVERHEAD)
  1092. return -1;
  1093. buffer = (char*)ikcp_malloc((mtu + IKCP_OVERHEAD) * 3);
  1094. if (buffer == NULL)
  1095. return -2;
  1096. kcp->mtu = mtu;
  1097. kcp->mss = kcp->mtu - IKCP_OVERHEAD;
  1098. ikcp_free(kcp->buffer);
  1099. kcp->buffer = buffer;
  1100. return 0;
  1101. }
  1102. int ikcp_interval(ikcpcb *kcp, int interval)
  1103. {
  1104. if (interval > 5000) interval = 5000;
  1105. else if (interval < 10) interval = 10;
  1106. kcp->interval = interval;
  1107. return 0;
  1108. }
  1109. int ikcp_nodelay(ikcpcb *kcp, int nodelay, int interval, int resend, int nc)
  1110. {
  1111. if (nodelay >= 0) {
  1112. kcp->nodelay = nodelay;
  1113. if (nodelay) {
  1114. kcp->rx_minrto = IKCP_RTO_NDL;
  1115. }
  1116. else {
  1117. kcp->rx_minrto = IKCP_RTO_MIN;
  1118. }
  1119. }
  1120. if (interval >= 0) {
  1121. if (interval > 5000) interval = 5000;
  1122. else if (interval < 10) interval = 10;
  1123. kcp->interval = interval;
  1124. }
  1125. if (resend >= 0) {
  1126. kcp->fastresend = resend;
  1127. }
  1128. if (nc >= 0) {
  1129. kcp->nocwnd = nc;
  1130. }
  1131. return 0;
  1132. }
  1133. int ikcp_wndsize(ikcpcb *kcp, int sndwnd, int rcvwnd)
  1134. {
  1135. if (kcp) {
  1136. if (sndwnd > 0) {
  1137. kcp->snd_wnd = sndwnd;
  1138. }
  1139. if (rcvwnd > 0) { // must >= max fragment size
  1140. kcp->rcv_wnd = _imax_(rcvwnd, IKCP_WND_RCV);
  1141. }
  1142. }
  1143. return 0;
  1144. }
  1145. int ikcp_waitsnd(const ikcpcb *kcp)
  1146. {
  1147. return kcp->nsnd_buf + kcp->nsnd_que;
  1148. }
  1149. // read conv
  1150. IUINT32 ikcp_getconv(const void *ptr)
  1151. {
  1152. IUINT32 conv;
  1153. ikcp_decode32u((const char*)ptr, &conv);
  1154. return conv;
  1155. }
  1156. void ikcp_setminrto(ikcpcb *kcp, int Minrto)
  1157. {
  1158. kcp->rx_minrto = Minrto;
  1159. }