ikcp.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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 index, 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. buffer += index;
  362. if (iqueue_is_empty(&kcp->rcv_queue))
  363. return -1;
  364. if (len < 0) len = -len;
  365. peeksize = ikcp_peeksize(kcp);
  366. if (peeksize < 0)
  367. return -2;
  368. if (peeksize > len)
  369. return -3;
  370. if (kcp->nrcv_que >= kcp->rcv_wnd)
  371. recover = 1;
  372. // merge fragment
  373. for (len = 0, p = kcp->rcv_queue.next; p != &kcp->rcv_queue; ) {
  374. int fragment;
  375. seg = iqueue_entry(p, IKCPSEG, node);
  376. p = p->next;
  377. if (buffer) {
  378. memcpy(buffer, seg->data, seg->len);
  379. buffer += seg->len;
  380. }
  381. len += seg->len;
  382. fragment = seg->frg;
  383. if (ikcp_canlog(kcp, IKCP_LOG_RECV)) {
  384. ikcp_log(kcp, IKCP_LOG_RECV, "recv sn=%lu", (unsigned long)seg->sn);
  385. }
  386. if (ispeek == 0) {
  387. iqueue_del(&seg->node);
  388. ikcp_segment_delete(kcp, seg);
  389. kcp->nrcv_que--;
  390. }
  391. if (fragment == 0)
  392. break;
  393. }
  394. assert(len == peeksize);
  395. // move available data from rcv_buf -> rcv_queue
  396. while (! iqueue_is_empty(&kcp->rcv_buf)) {
  397. seg = iqueue_entry(kcp->rcv_buf.next, IKCPSEG, node);
  398. if (seg->sn == kcp->rcv_nxt && kcp->nrcv_que < kcp->rcv_wnd) {
  399. iqueue_del(&seg->node);
  400. kcp->nrcv_buf--;
  401. iqueue_add_tail(&seg->node, &kcp->rcv_queue);
  402. kcp->nrcv_que++;
  403. kcp->rcv_nxt++;
  404. } else {
  405. break;
  406. }
  407. }
  408. // fast recover
  409. if (kcp->nrcv_que < kcp->rcv_wnd && recover) {
  410. // ready to send back IKCP_CMD_WINS in ikcp_flush
  411. // tell remote my window size
  412. kcp->probe |= IKCP_ASK_TELL;
  413. }
  414. return len;
  415. }
  416. //---------------------------------------------------------------------
  417. // peek data size
  418. //---------------------------------------------------------------------
  419. int ikcp_peeksize(const ikcpcb *kcp)
  420. {
  421. struct IQUEUEHEAD *p;
  422. IKCPSEG *seg;
  423. int length = 0;
  424. assert(kcp);
  425. if (iqueue_is_empty(&kcp->rcv_queue)) return -1;
  426. seg = iqueue_entry(kcp->rcv_queue.next, IKCPSEG, node);
  427. if (seg->frg == 0) return seg->len;
  428. if (kcp->nrcv_que < seg->frg + 1) return -1;
  429. for (p = kcp->rcv_queue.next; p != &kcp->rcv_queue; p = p->next) {
  430. seg = iqueue_entry(p, IKCPSEG, node);
  431. length += seg->len;
  432. if (seg->frg == 0) break;
  433. }
  434. return length;
  435. }
  436. //---------------------------------------------------------------------
  437. // user/upper level send, returns below zero for error
  438. //---------------------------------------------------------------------
  439. int ikcp_send(ikcpcb *kcp, const char *buffer, int offset, int len)
  440. {
  441. IKCPSEG *seg;
  442. int count, i;
  443. assert(kcp->mss > 0);
  444. if (len < 0) return -1;
  445. buffer += offset;
  446. // append to previous segment in streaming mode (if possible)
  447. if (kcp->stream != 0) {
  448. if (!iqueue_is_empty(&kcp->snd_queue)) {
  449. IKCPSEG *old = iqueue_entry(kcp->snd_queue.prev, IKCPSEG, node);
  450. if (old->len < kcp->mss) {
  451. int capacity = kcp->mss - old->len;
  452. int extend = (len < capacity)? len : capacity;
  453. seg = ikcp_segment_new(kcp, old->len + extend);
  454. assert(seg);
  455. if (seg == NULL) {
  456. return -2;
  457. }
  458. iqueue_add_tail(&seg->node, &kcp->snd_queue);
  459. memcpy(seg->data, old->data, old->len);
  460. if (buffer) {
  461. memcpy(seg->data + old->len, buffer, extend);
  462. buffer += extend;
  463. }
  464. seg->len = old->len + extend;
  465. seg->frg = 0;
  466. len -= extend;
  467. iqueue_del_init(&old->node);
  468. ikcp_segment_delete(kcp, old);
  469. }
  470. }
  471. if (len <= 0) {
  472. return 0;
  473. }
  474. }
  475. if (len <= (int)kcp->mss) count = 1;
  476. else count = (len + kcp->mss - 1) / kcp->mss;
  477. if (count >= (int)IKCP_WND_RCV) return -2;
  478. if (count == 0) count = 1;
  479. // fragment
  480. for (i = 0; i < count; i++) {
  481. int size = len > (int)kcp->mss ? (int)kcp->mss : len;
  482. seg = ikcp_segment_new(kcp, size);
  483. assert(seg);
  484. if (seg == NULL) {
  485. return -2;
  486. }
  487. if (buffer && len > 0) {
  488. memcpy(seg->data, buffer, size);
  489. }
  490. seg->len = size;
  491. seg->frg = (kcp->stream == 0)? (count - i - 1) : 0;
  492. iqueue_init(&seg->node);
  493. iqueue_add_tail(&seg->node, &kcp->snd_queue);
  494. kcp->nsnd_que++;
  495. if (buffer) {
  496. buffer += size;
  497. }
  498. len -= size;
  499. }
  500. return 0;
  501. }
  502. //---------------------------------------------------------------------
  503. // parse ack
  504. //---------------------------------------------------------------------
  505. static void ikcp_update_ack(ikcpcb *kcp, IINT32 rtt)
  506. {
  507. IINT32 rto = 0;
  508. if (kcp->rx_srtt == 0) {
  509. kcp->rx_srtt = rtt;
  510. kcp->rx_rttval = rtt / 2;
  511. } else {
  512. long delta = rtt - kcp->rx_srtt;
  513. if (delta < 0) delta = -delta;
  514. kcp->rx_rttval = (3 * kcp->rx_rttval + delta) / 4;
  515. kcp->rx_srtt = (7 * kcp->rx_srtt + rtt) / 8;
  516. if (kcp->rx_srtt < 1) kcp->rx_srtt = 1;
  517. }
  518. rto = kcp->rx_srtt + _imax_(kcp->interval, 4 * kcp->rx_rttval);
  519. kcp->rx_rto = _ibound_(kcp->rx_minrto, rto, IKCP_RTO_MAX);
  520. }
  521. static void ikcp_shrink_buf(ikcpcb *kcp)
  522. {
  523. struct IQUEUEHEAD *p = kcp->snd_buf.next;
  524. if (p != &kcp->snd_buf) {
  525. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  526. kcp->snd_una = seg->sn;
  527. } else {
  528. kcp->snd_una = kcp->snd_nxt;
  529. }
  530. }
  531. static void ikcp_parse_ack(ikcpcb *kcp, IUINT32 sn)
  532. {
  533. struct IQUEUEHEAD *p, *next;
  534. if (_itimediff(sn, kcp->snd_una) < 0 || _itimediff(sn, kcp->snd_nxt) >= 0)
  535. return;
  536. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = next) {
  537. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  538. next = p->next;
  539. if (sn == seg->sn) {
  540. iqueue_del(p);
  541. ikcp_segment_delete(kcp, seg);
  542. kcp->nsnd_buf--;
  543. break;
  544. }
  545. if (_itimediff(sn, seg->sn) < 0) {
  546. break;
  547. }
  548. }
  549. }
  550. static void ikcp_parse_una(ikcpcb *kcp, IUINT32 una)
  551. {
  552. struct IQUEUEHEAD *p, *next;
  553. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = next) {
  554. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  555. next = p->next;
  556. if (_itimediff(una, seg->sn) > 0) {
  557. iqueue_del(p);
  558. ikcp_segment_delete(kcp, seg);
  559. kcp->nsnd_buf--;
  560. } else {
  561. break;
  562. }
  563. }
  564. }
  565. static void ikcp_parse_fastack(ikcpcb *kcp, IUINT32 sn, IUINT32 ts)
  566. {
  567. struct IQUEUEHEAD *p, *next;
  568. if (_itimediff(sn, kcp->snd_una) < 0 || _itimediff(sn, kcp->snd_nxt) >= 0)
  569. return;
  570. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = next) {
  571. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  572. next = p->next;
  573. if (_itimediff(sn, seg->sn) < 0) {
  574. break;
  575. }
  576. else if (sn != seg->sn) {
  577. #ifndef IKCP_FASTACK_CONSERVE
  578. seg->fastack++;
  579. #else
  580. if (_itimediff(ts, seg->ts) >= 0)
  581. seg->fastack++;
  582. #endif
  583. }
  584. }
  585. }
  586. //---------------------------------------------------------------------
  587. // ack append
  588. //---------------------------------------------------------------------
  589. static void ikcp_ack_push(ikcpcb *kcp, IUINT32 sn, IUINT32 ts)
  590. {
  591. size_t newsize = kcp->ackcount + 1;
  592. IUINT32 *ptr;
  593. if (newsize > kcp->ackblock) {
  594. IUINT32 *acklist;
  595. size_t newblock;
  596. for (newblock = 8; newblock < newsize; newblock <<= 1);
  597. acklist = (IUINT32*)ikcp_malloc(newblock * sizeof(IUINT32) * 2);
  598. if (acklist == NULL) {
  599. assert(acklist != NULL);
  600. abort();
  601. }
  602. if (kcp->acklist != NULL) {
  603. size_t x;
  604. for (x = 0; x < kcp->ackcount; x++) {
  605. acklist[x * 2 + 0] = kcp->acklist[x * 2 + 0];
  606. acklist[x * 2 + 1] = kcp->acklist[x * 2 + 1];
  607. }
  608. ikcp_free(kcp->acklist);
  609. }
  610. kcp->acklist = acklist;
  611. kcp->ackblock = newblock;
  612. }
  613. ptr = &kcp->acklist[kcp->ackcount * 2];
  614. ptr[0] = sn;
  615. ptr[1] = ts;
  616. kcp->ackcount++;
  617. }
  618. static void ikcp_ack_get(const ikcpcb *kcp, int p, IUINT32 *sn, IUINT32 *ts)
  619. {
  620. if (sn) sn[0] = kcp->acklist[p * 2 + 0];
  621. if (ts) ts[0] = kcp->acklist[p * 2 + 1];
  622. }
  623. //---------------------------------------------------------------------
  624. // parse data
  625. //---------------------------------------------------------------------
  626. void ikcp_parse_data(ikcpcb *kcp, IKCPSEG *newseg)
  627. {
  628. struct IQUEUEHEAD *p, *prev;
  629. IUINT32 sn = newseg->sn;
  630. int repeat = 0;
  631. if (_itimediff(sn, kcp->rcv_nxt + kcp->rcv_wnd) >= 0 ||
  632. _itimediff(sn, kcp->rcv_nxt) < 0) {
  633. ikcp_segment_delete(kcp, newseg);
  634. return;
  635. }
  636. for (p = kcp->rcv_buf.prev; p != &kcp->rcv_buf; p = prev) {
  637. IKCPSEG *seg = iqueue_entry(p, IKCPSEG, node);
  638. prev = p->prev;
  639. if (seg->sn == sn) {
  640. repeat = 1;
  641. break;
  642. }
  643. if (_itimediff(sn, seg->sn) > 0) {
  644. break;
  645. }
  646. }
  647. if (repeat == 0) {
  648. iqueue_init(&newseg->node);
  649. iqueue_add(&newseg->node, p);
  650. kcp->nrcv_buf++;
  651. } else {
  652. ikcp_segment_delete(kcp, newseg);
  653. }
  654. #if 0
  655. ikcp_qprint("rcvbuf", &kcp->rcv_buf);
  656. printf("rcv_nxt=%lu\n", kcp->rcv_nxt);
  657. #endif
  658. // move available data from rcv_buf -> rcv_queue
  659. while (! iqueue_is_empty(&kcp->rcv_buf)) {
  660. IKCPSEG *seg = iqueue_entry(kcp->rcv_buf.next, IKCPSEG, node);
  661. if (seg->sn == kcp->rcv_nxt && kcp->nrcv_que < kcp->rcv_wnd) {
  662. iqueue_del(&seg->node);
  663. kcp->nrcv_buf--;
  664. iqueue_add_tail(&seg->node, &kcp->rcv_queue);
  665. kcp->nrcv_que++;
  666. kcp->rcv_nxt++;
  667. } else {
  668. break;
  669. }
  670. }
  671. #if 0
  672. ikcp_qprint("queue", &kcp->rcv_queue);
  673. printf("rcv_nxt=%lu\n", kcp->rcv_nxt);
  674. #endif
  675. #if 1
  676. // printf("snd(buf=%d, queue=%d)\n", kcp->nsnd_buf, kcp->nsnd_que);
  677. // printf("rcv(buf=%d, queue=%d)\n", kcp->nrcv_buf, kcp->nrcv_que);
  678. #endif
  679. }
  680. //---------------------------------------------------------------------
  681. // input data
  682. //---------------------------------------------------------------------
  683. int ikcp_input(ikcpcb *kcp, const char *data, int offset, int size)
  684. {
  685. IUINT32 prev_una = kcp->snd_una;
  686. IUINT32 maxack = 0, latest_ts = 0;
  687. int flag = 0;
  688. if (data == NULL || (int)size < (int)IKCP_OVERHEAD) return -1;
  689. data += offset;
  690. while (1) {
  691. IUINT32 ts, sn, len, una, conv;
  692. IUINT16 wnd;
  693. IUINT8 cmd, frg;
  694. IKCPSEG *seg;
  695. if (size < (int)IKCP_OVERHEAD) break;
  696. data = ikcp_decode32u(data, &conv);
  697. /*if (conv != kcp->conv) return -1;*/
  698. data = ikcp_decode8u(data, &cmd);
  699. data = ikcp_decode8u(data, &frg);
  700. data = ikcp_decode16u(data, &wnd);
  701. data = ikcp_decode32u(data, &ts);
  702. data = ikcp_decode32u(data, &sn);
  703. data = ikcp_decode32u(data, &una);
  704. data = ikcp_decode32u(data, &len);
  705. size -= IKCP_OVERHEAD;
  706. if ((long)size < (long)len || (int)len < 0) return -2;
  707. if (cmd != IKCP_CMD_PUSH && cmd != IKCP_CMD_ACK &&
  708. cmd != IKCP_CMD_WASK && cmd != IKCP_CMD_WINS)
  709. return -3;
  710. kcp->rmt_wnd = wnd;
  711. ikcp_parse_una(kcp, una);
  712. ikcp_shrink_buf(kcp);
  713. if (cmd == IKCP_CMD_ACK) {
  714. if (_itimediff(kcp->current, ts) >= 0) {
  715. ikcp_update_ack(kcp, _itimediff(kcp->current, ts));
  716. }
  717. ikcp_parse_ack(kcp, sn);
  718. ikcp_shrink_buf(kcp);
  719. if (flag == 0) {
  720. flag = 1;
  721. maxack = sn;
  722. latest_ts = ts;
  723. } else {
  724. if (_itimediff(sn, maxack) > 0) {
  725. #ifndef IKCP_FASTACK_CONSERVE
  726. maxack = sn;
  727. latest_ts = ts;
  728. #else
  729. if (_itimediff(ts, latest_ts) > 0) {
  730. maxack = sn;
  731. latest_ts = ts;
  732. }
  733. #endif
  734. }
  735. }
  736. if (ikcp_canlog(kcp, IKCP_LOG_IN_ACK)) {
  737. ikcp_log(kcp, IKCP_LOG_IN_ACK,
  738. "input ack: sn=%lu rtt=%ld rto=%ld", (unsigned long)sn,
  739. (long)_itimediff(kcp->current, ts),
  740. (long)kcp->rx_rto);
  741. }
  742. }
  743. else if (cmd == IKCP_CMD_PUSH) {
  744. if (ikcp_canlog(kcp, IKCP_LOG_IN_DATA)) {
  745. ikcp_log(kcp, IKCP_LOG_IN_DATA,
  746. "input psh: sn=%lu ts=%lu", (unsigned long)sn, (unsigned long)ts);
  747. }
  748. if (_itimediff(sn, kcp->rcv_nxt + kcp->rcv_wnd) < 0) {
  749. ikcp_ack_push(kcp, sn, ts);
  750. if (_itimediff(sn, kcp->rcv_nxt) >= 0) {
  751. seg = ikcp_segment_new(kcp, len);
  752. seg->conv = conv;
  753. seg->cmd = cmd;
  754. seg->frg = frg;
  755. seg->wnd = wnd;
  756. seg->ts = ts;
  757. seg->sn = sn;
  758. seg->una = una;
  759. seg->len = len;
  760. if (len > 0) {
  761. memcpy(seg->data, data, len);
  762. }
  763. ikcp_parse_data(kcp, seg);
  764. }
  765. }
  766. }
  767. else if (cmd == IKCP_CMD_WASK) {
  768. // ready to send back IKCP_CMD_WINS in ikcp_flush
  769. // tell remote my window size
  770. kcp->probe |= IKCP_ASK_TELL;
  771. if (ikcp_canlog(kcp, IKCP_LOG_IN_PROBE)) {
  772. ikcp_log(kcp, IKCP_LOG_IN_PROBE, "input probe");
  773. }
  774. }
  775. else if (cmd == IKCP_CMD_WINS) {
  776. // do nothing
  777. if (ikcp_canlog(kcp, IKCP_LOG_IN_WINS)) {
  778. ikcp_log(kcp, IKCP_LOG_IN_WINS,
  779. "input wins: %lu", (unsigned long)(wnd));
  780. }
  781. }
  782. else {
  783. return -3;
  784. }
  785. data += len;
  786. size -= len;
  787. }
  788. if (flag != 0) {
  789. ikcp_parse_fastack(kcp, maxack, latest_ts);
  790. }
  791. if (_itimediff(kcp->snd_una, prev_una) > 0) {
  792. if (kcp->cwnd < kcp->rmt_wnd) {
  793. IUINT32 mss = kcp->mss;
  794. if (kcp->cwnd < kcp->ssthresh) {
  795. kcp->cwnd++;
  796. kcp->incr += mss;
  797. } else {
  798. if (kcp->incr < mss) kcp->incr = mss;
  799. kcp->incr += (mss * mss) / kcp->incr + (mss / 16);
  800. if ((kcp->cwnd + 1) * mss <= kcp->incr) {
  801. #if 1
  802. kcp->cwnd = (kcp->incr + mss - 1) / ((mss > 0)? mss : 1);
  803. #else
  804. kcp->cwnd++;
  805. #endif
  806. }
  807. }
  808. if (kcp->cwnd > kcp->rmt_wnd) {
  809. kcp->cwnd = kcp->rmt_wnd;
  810. kcp->incr = kcp->rmt_wnd * mss;
  811. }
  812. }
  813. }
  814. return 0;
  815. }
  816. //---------------------------------------------------------------------
  817. // ikcp_encode_seg
  818. //---------------------------------------------------------------------
  819. static char *ikcp_encode_seg(char *ptr, const IKCPSEG *seg)
  820. {
  821. ptr = ikcp_encode32u(ptr, seg->conv);
  822. ptr = ikcp_encode8u(ptr, (IUINT8)seg->cmd);
  823. ptr = ikcp_encode8u(ptr, (IUINT8)seg->frg);
  824. ptr = ikcp_encode16u(ptr, (IUINT16)seg->wnd);
  825. ptr = ikcp_encode32u(ptr, seg->ts);
  826. ptr = ikcp_encode32u(ptr, seg->sn);
  827. ptr = ikcp_encode32u(ptr, seg->una);
  828. ptr = ikcp_encode32u(ptr, seg->len);
  829. return ptr;
  830. }
  831. static int ikcp_wnd_unused(const ikcpcb *kcp)
  832. {
  833. if (kcp->nrcv_que < kcp->rcv_wnd) {
  834. return kcp->rcv_wnd - kcp->nrcv_que;
  835. }
  836. return 0;
  837. }
  838. //---------------------------------------------------------------------
  839. // ikcp_flush
  840. //---------------------------------------------------------------------
  841. void ikcp_flush(ikcpcb *kcp)
  842. {
  843. IUINT32 current = kcp->current;
  844. char *buffer = kcp->buffer;
  845. char *ptr = buffer;
  846. int count, size, i;
  847. IUINT32 resent, cwnd;
  848. IUINT32 rtomin;
  849. struct IQUEUEHEAD *p;
  850. int change = 0;
  851. int lost = 0;
  852. IKCPSEG seg;
  853. // 'ikcp_update' haven't been called.
  854. if (kcp->updated == 0) return;
  855. seg.conv = kcp->conv;
  856. seg.cmd = IKCP_CMD_ACK;
  857. seg.frg = 0;
  858. seg.wnd = ikcp_wnd_unused(kcp);
  859. seg.una = kcp->rcv_nxt;
  860. seg.len = 0;
  861. seg.sn = 0;
  862. seg.ts = 0;
  863. // flush acknowledges
  864. count = kcp->ackcount;
  865. for (i = 0; i < count; i++) {
  866. size = (int)(ptr - buffer);
  867. if (size + (int)IKCP_OVERHEAD > (int)kcp->mtu) {
  868. ikcp_output(kcp, buffer, size);
  869. ptr = buffer;
  870. }
  871. ikcp_ack_get(kcp, i, &seg.sn, &seg.ts);
  872. ptr = ikcp_encode_seg(ptr, &seg);
  873. }
  874. kcp->ackcount = 0;
  875. // probe window size (if remote window size equals zero)
  876. if (kcp->rmt_wnd == 0) {
  877. if (kcp->probe_wait == 0) {
  878. kcp->probe_wait = IKCP_PROBE_INIT;
  879. kcp->ts_probe = kcp->current + kcp->probe_wait;
  880. }
  881. else {
  882. if (_itimediff(kcp->current, kcp->ts_probe) >= 0) {
  883. if (kcp->probe_wait < IKCP_PROBE_INIT)
  884. kcp->probe_wait = IKCP_PROBE_INIT;
  885. kcp->probe_wait += kcp->probe_wait / 2;
  886. if (kcp->probe_wait > IKCP_PROBE_LIMIT)
  887. kcp->probe_wait = IKCP_PROBE_LIMIT;
  888. kcp->ts_probe = kcp->current + kcp->probe_wait;
  889. kcp->probe |= IKCP_ASK_SEND;
  890. }
  891. }
  892. } else {
  893. kcp->ts_probe = 0;
  894. kcp->probe_wait = 0;
  895. }
  896. // flush window probing commands
  897. if (kcp->probe & IKCP_ASK_SEND) {
  898. seg.cmd = IKCP_CMD_WASK;
  899. size = (int)(ptr - buffer);
  900. if (size + (int)IKCP_OVERHEAD > (int)kcp->mtu) {
  901. ikcp_output(kcp, buffer, size);
  902. ptr = buffer;
  903. }
  904. ptr = ikcp_encode_seg(ptr, &seg);
  905. }
  906. // flush window probing commands
  907. if (kcp->probe & IKCP_ASK_TELL) {
  908. seg.cmd = IKCP_CMD_WINS;
  909. size = (int)(ptr - buffer);
  910. if (size + (int)IKCP_OVERHEAD > (int)kcp->mtu) {
  911. ikcp_output(kcp, buffer, size);
  912. ptr = buffer;
  913. }
  914. ptr = ikcp_encode_seg(ptr, &seg);
  915. }
  916. kcp->probe = 0;
  917. // calculate window size
  918. cwnd = _imin_(kcp->snd_wnd, kcp->rmt_wnd);
  919. if (kcp->nocwnd == 0) cwnd = _imin_(kcp->cwnd, cwnd);
  920. // move data from snd_queue to snd_buf
  921. while (_itimediff(kcp->snd_nxt, kcp->snd_una + cwnd) < 0) {
  922. IKCPSEG *newseg;
  923. if (iqueue_is_empty(&kcp->snd_queue)) break;
  924. newseg = iqueue_entry(kcp->snd_queue.next, IKCPSEG, node);
  925. iqueue_del(&newseg->node);
  926. iqueue_add_tail(&newseg->node, &kcp->snd_buf);
  927. kcp->nsnd_que--;
  928. kcp->nsnd_buf++;
  929. newseg->conv = kcp->conv;
  930. newseg->cmd = IKCP_CMD_PUSH;
  931. newseg->wnd = seg.wnd;
  932. newseg->ts = current;
  933. newseg->sn = kcp->snd_nxt++;
  934. newseg->una = kcp->rcv_nxt;
  935. newseg->resendts = current;
  936. newseg->rto = kcp->rx_rto;
  937. newseg->fastack = 0;
  938. newseg->xmit = 0;
  939. }
  940. // calculate resent
  941. resent = (kcp->fastresend > 0)? (IUINT32)kcp->fastresend : 0xffffffff;
  942. rtomin = (kcp->nodelay == 0)? (kcp->rx_rto >> 3) : 0;
  943. // flush data segments
  944. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = p->next) {
  945. IKCPSEG *segment = iqueue_entry(p, IKCPSEG, node);
  946. int needsend = 0;
  947. if (segment->xmit == 0) {
  948. needsend = 1;
  949. segment->xmit++;
  950. segment->rto = kcp->rx_rto;
  951. segment->resendts = current + segment->rto + rtomin;
  952. }
  953. else if (_itimediff(current, segment->resendts) >= 0) {
  954. needsend = 1;
  955. segment->xmit++;
  956. kcp->xmit++;
  957. if (kcp->nodelay == 0) {
  958. segment->rto += _imax_(segment->rto, (IUINT32)kcp->rx_rto);
  959. } else {
  960. IINT32 step = (kcp->nodelay < 2)?
  961. ((IINT32)(segment->rto)) : kcp->rx_rto;
  962. segment->rto += step / 2;
  963. }
  964. segment->resendts = current + segment->rto;
  965. lost = 1;
  966. }
  967. else if (segment->fastack >= resent) {
  968. if ((int)segment->xmit <= kcp->fastlimit ||
  969. kcp->fastlimit <= 0) {
  970. needsend = 1;
  971. segment->xmit++;
  972. segment->fastack = 0;
  973. segment->resendts = current + segment->rto;
  974. change++;
  975. }
  976. }
  977. if (needsend) {
  978. int need;
  979. segment->ts = current;
  980. segment->wnd = seg.wnd;
  981. segment->una = kcp->rcv_nxt;
  982. size = (int)(ptr - buffer);
  983. need = IKCP_OVERHEAD + segment->len;
  984. if (size + need > (int)kcp->mtu) {
  985. ikcp_output(kcp, buffer, size);
  986. ptr = buffer;
  987. }
  988. ptr = ikcp_encode_seg(ptr, segment);
  989. if (segment->len > 0) {
  990. memcpy(ptr, segment->data, segment->len);
  991. ptr += segment->len;
  992. }
  993. if (segment->xmit >= kcp->dead_link) {
  994. kcp->state = (IUINT32)-1;
  995. }
  996. }
  997. }
  998. // flash remain segments
  999. size = (int)(ptr - buffer);
  1000. if (size > 0) {
  1001. ikcp_output(kcp, buffer, size);
  1002. }
  1003. // update ssthresh
  1004. if (change) {
  1005. IUINT32 inflight = kcp->snd_nxt - kcp->snd_una;
  1006. kcp->ssthresh = inflight / 2;
  1007. if (kcp->ssthresh < IKCP_THRESH_MIN)
  1008. kcp->ssthresh = IKCP_THRESH_MIN;
  1009. kcp->cwnd = kcp->ssthresh + resent;
  1010. kcp->incr = kcp->cwnd * kcp->mss;
  1011. }
  1012. if (lost) {
  1013. kcp->ssthresh = cwnd / 2;
  1014. if (kcp->ssthresh < IKCP_THRESH_MIN)
  1015. kcp->ssthresh = IKCP_THRESH_MIN;
  1016. kcp->cwnd = 1;
  1017. kcp->incr = kcp->mss;
  1018. }
  1019. if (kcp->cwnd < 1) {
  1020. kcp->cwnd = 1;
  1021. kcp->incr = kcp->mss;
  1022. }
  1023. }
  1024. //---------------------------------------------------------------------
  1025. // update state (call it repeatedly, every 10ms-100ms), or you can ask
  1026. // ikcp_check when to call it again (without ikcp_input/_send calling).
  1027. // 'current' - current timestamp in millisec.
  1028. //---------------------------------------------------------------------
  1029. void ikcp_update(ikcpcb *kcp, IUINT32 current)
  1030. {
  1031. IINT32 slap;
  1032. kcp->current = current;
  1033. if (kcp->updated == 0) {
  1034. kcp->updated = 1;
  1035. kcp->ts_flush = kcp->current;
  1036. }
  1037. slap = _itimediff(kcp->current, kcp->ts_flush);
  1038. if (slap >= 10000 || slap < -10000) {
  1039. kcp->ts_flush = kcp->current;
  1040. slap = 0;
  1041. }
  1042. if (slap >= 0) {
  1043. kcp->ts_flush += kcp->interval;
  1044. if (_itimediff(kcp->current, kcp->ts_flush) >= 0) {
  1045. kcp->ts_flush = kcp->current + kcp->interval;
  1046. }
  1047. ikcp_flush(kcp);
  1048. }
  1049. }
  1050. //---------------------------------------------------------------------
  1051. // Determine when should you invoke ikcp_update:
  1052. // returns when you should invoke ikcp_update in millisec, if there
  1053. // is no ikcp_input/_send calling. you can call ikcp_update in that
  1054. // time, instead of call update repeatly.
  1055. // Important to reduce unnacessary ikcp_update invoking. use it to
  1056. // schedule ikcp_update (eg. implementing an epoll-like mechanism,
  1057. // or optimize ikcp_update when handling massive kcp connections)
  1058. //---------------------------------------------------------------------
  1059. IUINT32 ikcp_check(const ikcpcb *kcp, IUINT32 current)
  1060. {
  1061. IUINT32 ts_flush = kcp->ts_flush;
  1062. IINT32 tm_flush = 0x7fffffff;
  1063. IINT32 tm_packet = 0x7fffffff;
  1064. IUINT32 minimal = 0;
  1065. struct IQUEUEHEAD *p;
  1066. if (kcp->updated == 0) {
  1067. return current;
  1068. }
  1069. if (_itimediff(current, ts_flush) >= 10000 ||
  1070. _itimediff(current, ts_flush) < -10000) {
  1071. ts_flush = current;
  1072. }
  1073. if (_itimediff(current, ts_flush) >= 0) {
  1074. return current;
  1075. }
  1076. tm_flush = _itimediff(ts_flush, current);
  1077. for (p = kcp->snd_buf.next; p != &kcp->snd_buf; p = p->next) {
  1078. const IKCPSEG *seg = iqueue_entry(p, const IKCPSEG, node);
  1079. IINT32 diff = _itimediff(seg->resendts, current);
  1080. if (diff <= 0) {
  1081. return current;
  1082. }
  1083. if (diff < tm_packet) tm_packet = diff;
  1084. }
  1085. minimal = (IUINT32)(tm_packet < tm_flush ? tm_packet : tm_flush);
  1086. if (minimal >= kcp->interval) minimal = kcp->interval;
  1087. return current + minimal;
  1088. }
  1089. int ikcp_setmtu(ikcpcb *kcp, int mtu)
  1090. {
  1091. char *buffer;
  1092. if (mtu < 50 || mtu < (int)IKCP_OVERHEAD)
  1093. return -1;
  1094. buffer = (char*)ikcp_malloc((mtu + IKCP_OVERHEAD) * 3);
  1095. if (buffer == NULL)
  1096. return -2;
  1097. kcp->mtu = mtu;
  1098. kcp->mss = kcp->mtu - IKCP_OVERHEAD;
  1099. ikcp_free(kcp->buffer);
  1100. kcp->buffer = buffer;
  1101. return 0;
  1102. }
  1103. int ikcp_interval(ikcpcb *kcp, int interval)
  1104. {
  1105. if (interval > 5000) interval = 5000;
  1106. else if (interval < 10) interval = 10;
  1107. kcp->interval = interval;
  1108. return 0;
  1109. }
  1110. int ikcp_nodelay(ikcpcb *kcp, int nodelay, int interval, int resend, int nc)
  1111. {
  1112. if (nodelay >= 0) {
  1113. kcp->nodelay = nodelay;
  1114. if (nodelay) {
  1115. kcp->rx_minrto = IKCP_RTO_NDL;
  1116. }
  1117. else {
  1118. kcp->rx_minrto = IKCP_RTO_MIN;
  1119. }
  1120. }
  1121. if (interval >= 0) {
  1122. if (interval > 5000) interval = 5000;
  1123. else if (interval < 10) interval = 10;
  1124. kcp->interval = interval;
  1125. }
  1126. if (resend >= 0) {
  1127. kcp->fastresend = resend;
  1128. }
  1129. if (nc >= 0) {
  1130. kcp->nocwnd = nc;
  1131. }
  1132. return 0;
  1133. }
  1134. int ikcp_wndsize(ikcpcb *kcp, int sndwnd, int rcvwnd)
  1135. {
  1136. if (kcp) {
  1137. if (sndwnd > 0) {
  1138. kcp->snd_wnd = sndwnd;
  1139. }
  1140. if (rcvwnd > 0) { // must >= max fragment size
  1141. kcp->rcv_wnd = _imax_(rcvwnd, IKCP_WND_RCV);
  1142. }
  1143. }
  1144. return 0;
  1145. }
  1146. int ikcp_waitsnd(const ikcpcb *kcp)
  1147. {
  1148. return kcp->nsnd_buf + kcp->nsnd_que;
  1149. }
  1150. // read conv
  1151. IUINT32 ikcp_getconv(const void *ptr)
  1152. {
  1153. IUINT32 conv;
  1154. ikcp_decode32u((const char*)ptr, &conv);
  1155. return conv;
  1156. }
  1157. void ikcp_setminrto(ikcpcb *kcp, int Minrto)
  1158. {
  1159. kcp->rx_minrto = Minrto;
  1160. }