Debian Codegraph @

third-party/libhv-v1.3.3/event/kcp/ikcp.h

raw symbols SCIP
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 #ifndef __IKCP_H__
13 #define __IKCP_H__
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <assert.h>
18
19
20 //=====================================================================
21 // 32BIT INTEGER DEFINITION
22 //=====================================================================
23 #ifndef __INTEGER_32_BITS__
24 #define __INTEGER_32_BITS__
25 #if defined(_WIN64) || defined(WIN64) || defined(__amd64__) || \
26 defined(__x86_64) || defined(__x86_64__) || defined(_M_IA64) || \
27 defined(_M_AMD64)
28 typedef unsigned int ISTDUINT32;
29 typedef int ISTDINT32;
30 #elif defined(_WIN32) || defined(WIN32) || defined(__i386__) || \
31 defined(__i386) || defined(_M_X86)
32 typedef unsigned long ISTDUINT32;
33 typedef long ISTDINT32;
34 #elif defined(__MACOS__)
35 typedef UInt32 ISTDUINT32;
36 typedef SInt32 ISTDINT32;
37 #elif defined(__APPLE__) && defined(__MACH__)
38 #include <sys/types.h>
39 typedef u_int32_t ISTDUINT32;
40 typedef int32_t ISTDINT32;
41 #elif defined(__BEOS__)
42 #include <sys/inttypes.h>
43 typedef u_int32_t ISTDUINT32;
44 typedef int32_t ISTDINT32;
45 #elif (defined(_MSC_VER) || defined(__BORLANDC__)) && (!defined(__MSDOS__))
46 typedef unsigned __int32 ISTDUINT32;
47 typedef __int32 ISTDINT32;
48 #elif defined(__GNUC__)
49 #include <stdint.h>
50 typedef uint32_t ISTDUINT32;
51 typedef int32_t ISTDINT32;
52 #else
53 typedef unsigned long ISTDUINT32;
54 typedef long ISTDINT32;
55 #endif
56 #endif
57
58
59 //=====================================================================
60 // Integer Definition
61 //=====================================================================
62 #ifndef __IINT8_DEFINED
63 #define __IINT8_DEFINED
64 typedef char IINT8;
65 #endif
66
67 #ifndef __IUINT8_DEFINED
68 #define __IUINT8_DEFINED
69 typedef unsigned char IUINT8;
70 #endif
71
72 #ifndef __IUINT16_DEFINED
73 #define __IUINT16_DEFINED
74 typedef unsigned short IUINT16;
75 #endif
76
77 #ifndef __IINT16_DEFINED
78 #define __IINT16_DEFINED
79 typedef short IINT16;
80 #endif
81
82 #ifndef __IINT32_DEFINED
83 #define __IINT32_DEFINED
84 typedef ISTDINT32 IINT32;
85 #endif
86
87 #ifndef __IUINT32_DEFINED
88 #define __IUINT32_DEFINED
89 typedef ISTDUINT32 IUINT32;
90 #endif
91
92 #ifndef __IINT64_DEFINED
93 #define __IINT64_DEFINED
94 #if defined(_MSC_VER) || defined(__BORLANDC__)
95 typedef __int64 IINT64;
96 #else
97 typedef long long IINT64;
98 #endif
99 #endif
100
101 #ifndef __IUINT64_DEFINED
102 #define __IUINT64_DEFINED
103 #if defined(_MSC_VER) || defined(__BORLANDC__)
104 typedef unsigned __int64 IUINT64;
105 #else
106 typedef unsigned long long IUINT64;
107 #endif
108 #endif
109
110 #ifndef INLINE
111 #if defined(__GNUC__)
112
113 #if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
114 #define INLINE __inline__ __attribute__((always_inline))
115 #else
116 #define INLINE __inline__
117 #endif
118
119 #elif (defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__))
120 #define INLINE __inline
121 #else
122 #define INLINE
123 #endif
124 #endif
125
126 #if (!defined(__cplusplus)) && (!defined(inline))
127 #define inline INLINE
128 #endif
129
130
131 //=====================================================================
132 // QUEUE DEFINITION
133 //=====================================================================
134 #ifndef __IQUEUE_DEF__
135 #define __IQUEUE_DEF__
136
137 struct IQUEUEHEAD {
138 struct IQUEUEHEAD *next, *prev;
139 };
140
141 typedef struct IQUEUEHEAD iqueue_head;
142
143
144 //---------------------------------------------------------------------
145 // queue init
146 //---------------------------------------------------------------------
147 #define IQUEUE_HEAD_INIT(name) { &(name), &(name) }
148 #define IQUEUE_HEAD(name) \
149 struct IQUEUEHEAD name = IQUEUE_HEAD_INIT(name)
150
151 #define IQUEUE_INIT(ptr) ( \
152 (ptr)->next = (ptr), (ptr)->prev = (ptr))
153
154 #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
155
156 #define ICONTAINEROF(ptr, type, member) ( \
157 (type*)( ((char*)((type*)ptr)) - IOFFSETOF(type, member)) )
158
159 #define IQUEUE_ENTRY(ptr, type, member) ICONTAINEROF(ptr, type, member)
160
161
162 //---------------------------------------------------------------------
163 // queue operation
164 //---------------------------------------------------------------------
165 #define IQUEUE_ADD(node, head) ( \
166 (node)->prev = (head), (node)->next = (head)->next, \
167 (head)->next->prev = (node), (head)->next = (node))
168
169 #define IQUEUE_ADD_TAIL(node, head) ( \
170 (node)->prev = (head)->prev, (node)->next = (head), \
171 (head)->prev->next = (node), (head)->prev = (node))
172
173 #define IQUEUE_DEL_BETWEEN(p, n) ((n)->prev = (p), (p)->next = (n))
174
175 #define IQUEUE_DEL(entry) (\
176 (entry)->next->prev = (entry)->prev, \
177 (entry)->prev->next = (entry)->next, \
178 (entry)->next = 0, (entry)->prev = 0)
179
180 #define IQUEUE_DEL_INIT(entry) do { \
181 IQUEUE_DEL(entry); IQUEUE_INIT(entry); } while (0)
182
183 #define IQUEUE_IS_EMPTY(entry) ((entry) == (entry)->next)
184
185 #define iqueue_init IQUEUE_INIT
186 #define iqueue_entry IQUEUE_ENTRY
187 #define iqueue_add IQUEUE_ADD
188 #define iqueue_add_tail IQUEUE_ADD_TAIL
189 #define iqueue_del IQUEUE_DEL
190 #define iqueue_del_init IQUEUE_DEL_INIT
191 #define iqueue_is_empty IQUEUE_IS_EMPTY
192
193 #define IQUEUE_FOREACH(iterator, head, TYPE, MEMBER) \
194 for ((iterator) = iqueue_entry((head)->next, TYPE, MEMBER); \
195 &((iterator)->MEMBER) != (head); \
196 (iterator) = iqueue_entry((iterator)->MEMBER.next, TYPE, MEMBER))
197
198 #define iqueue_foreach(iterator, head, TYPE, MEMBER) \
199 IQUEUE_FOREACH(iterator, head, TYPE, MEMBER)
200
201 #define iqueue_foreach_entry(pos, head) \
202 for( (pos) = (head)->next; (pos) != (head) ; (pos) = (pos)->next )
203
204
205 #define __iqueue_splice(list, head) do { \
206 iqueue_head *first = (list)->next, *last = (list)->prev; \
207 iqueue_head *at = (head)->next; \
208 (first)->prev = (head), (head)->next = (first); \
209 (last)->next = (at), (at)->prev = (last); } while (0)
210
211 #define iqueue_splice(list, head) do { \
212 if (!iqueue_is_empty(list)) __iqueue_splice(list, head); } while (0)
213
214 #define iqueue_splice_init(list, head) do { \
215 iqueue_splice(list, head); iqueue_init(list); } while (0)
216
217
218 #ifdef _MSC_VER
219 #pragma warning(disable:4311)
220 #pragma warning(disable:4312)
221 #pragma warning(disable:4996)
222 #endif
223
224 #endif
225
226
227 //---------------------------------------------------------------------
228 // BYTE ORDER & ALIGNMENT
229 //---------------------------------------------------------------------
230 #ifndef IWORDS_BIG_ENDIAN
231 #ifdef _BIG_ENDIAN_
232 #if _BIG_ENDIAN_
233 #define IWORDS_BIG_ENDIAN 1
234 #endif
235 #endif
236 #ifndef IWORDS_BIG_ENDIAN
237 #if defined(__hppa__) || \
238 defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
239 (defined(__MIPS__) && defined(__MIPSEB__)) || \
240 defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
241 defined(__sparc__) || defined(__powerpc__) || \
242 defined(__mc68000__) || defined(__s390x__) || defined(__s390__)
243 #define IWORDS_BIG_ENDIAN 1
244 #endif
245 #endif
246 #ifndef IWORDS_BIG_ENDIAN
247 #define IWORDS_BIG_ENDIAN 0
248 #endif
249 #endif
250
251 #ifndef IWORDS_MUST_ALIGN
252 #if defined(__i386__) || defined(__i386) || defined(_i386_)
253 #define IWORDS_MUST_ALIGN 0
254 #elif defined(_M_IX86) || defined(_X86_) || defined(__x86_64__)
255 #define IWORDS_MUST_ALIGN 0
256 #elif defined(__amd64) || defined(__amd64__)
257 #define IWORDS_MUST_ALIGN 0
258 #else
259 #define IWORDS_MUST_ALIGN 1
260 #endif
261 #endif
262
263
264 //=====================================================================
265 // SEGMENT
266 //=====================================================================
267 struct IKCPSEG
268 {
269 struct IQUEUEHEAD node;
270 IUINT32 conv;
271 IUINT32 cmd;
272 IUINT32 frg;
273 IUINT32 wnd;
274 IUINT32 ts;
275 IUINT32 sn;
276 IUINT32 una;
277 IUINT32 len;
278 IUINT32 resendts;
279 IUINT32 rto;
280 IUINT32 fastack;
281 IUINT32 xmit;
282 char data[1];
283 };
284
285
286 //---------------------------------------------------------------------
287 // IKCPCB
288 //---------------------------------------------------------------------
289 struct IKCPCB
290 {
291 IUINT32 conv, mtu, mss, state;
292 IUINT32 snd_una, snd_nxt, rcv_nxt;
293 IUINT32 ts_recent, ts_lastack, ssthresh;
294 IINT32 rx_rttval, rx_srtt, rx_rto, rx_minrto;
295 IUINT32 snd_wnd, rcv_wnd, rmt_wnd, cwnd, probe;
296 IUINT32 current, interval, ts_flush, xmit;
297 IUINT32 nrcv_buf, nsnd_buf;
298 IUINT32 nrcv_que, nsnd_que;
299 IUINT32 nodelay, updated;
300 IUINT32 ts_probe, probe_wait;
301 IUINT32 dead_link, incr;
302 struct IQUEUEHEAD snd_queue;
303 struct IQUEUEHEAD rcv_queue;
304 struct IQUEUEHEAD snd_buf;
305 struct IQUEUEHEAD rcv_buf;
306 IUINT32 *acklist;
307 IUINT32 ackcount;
308 IUINT32 ackblock;
309 void *user;
310 char *buffer;
311 int fastresend;
312 int fastlimit;
313 int nocwnd, stream;
314 int logmask;
315 int (*output)(const char *buf, int len, struct IKCPCB *kcp, void *user);
316 void (*writelog)(const char *log, struct IKCPCB *kcp, void *user);
317 };
318
319
320 typedef struct IKCPCB ikcpcb;
321
322 #define IKCP_LOG_OUTPUT 1
323 #define IKCP_LOG_INPUT 2
324 #define IKCP_LOG_SEND 4
325 #define IKCP_LOG_RECV 8
326 #define IKCP_LOG_IN_DATA 16
327 #define IKCP_LOG_IN_ACK 32
328 #define IKCP_LOG_IN_PROBE 64
329 #define IKCP_LOG_IN_WINS 128
330 #define IKCP_LOG_OUT_DATA 256
331 #define IKCP_LOG_OUT_ACK 512
332 #define IKCP_LOG_OUT_PROBE 1024
333 #define IKCP_LOG_OUT_WINS 2048
334
335 #ifdef __cplusplus
336 extern "C" {
337 #endif
338
339 //---------------------------------------------------------------------
340 // interface
341 //---------------------------------------------------------------------
342
343 // create a new kcp control object, 'conv' must equal in two endpoint
344 // from the same connection. 'user' will be passed to the output callback
345 // output callback can be setup like this: 'kcp->output = my_udp_output'
346 ikcpcb* ikcp_create(IUINT32 conv, void *user);
347
348 // release kcp control object
349 void ikcp_release(ikcpcb *kcp);
350
351 // set output callback, which will be invoked by kcp
352 void ikcp_setoutput(ikcpcb *kcp, int (*output)(const char *buf, int len,
353 ikcpcb *kcp, void *user));
354
355 // user/upper level recv: returns size, returns below zero for EAGAIN
356 int ikcp_recv(ikcpcb *kcp, char *buffer, int len);
357
358 // user/upper level send, returns below zero for error
359 int ikcp_send(ikcpcb *kcp, const char *buffer, int len);
360
361 // update state (call it repeatedly, every 10ms-100ms), or you can ask
362 // ikcp_check when to call it again (without ikcp_input/_send calling).
363 // 'current' - current timestamp in millisec.
364 void ikcp_update(ikcpcb *kcp, IUINT32 current);
365
366 // Determine when should you invoke ikcp_update:
367 // returns when you should invoke ikcp_update in millisec, if there
368 // is no ikcp_input/_send calling. you can call ikcp_update in that
369 // time, instead of call update repeatly.
370 // Important to reduce unnacessary ikcp_update invoking. use it to
371 // schedule ikcp_update (eg. implementing an epoll-like mechanism,
372 // or optimize ikcp_update when handling massive kcp connections)
373 IUINT32 ikcp_check(const ikcpcb *kcp, IUINT32 current);
374
375 // when you received a low level packet (eg. UDP packet), call it
376 int ikcp_input(ikcpcb *kcp, const char *data, long size);
377
378 // flush pending data
379 void ikcp_flush(ikcpcb *kcp);
380
381 // check the size of next message in the recv queue
382 int ikcp_peeksize(const ikcpcb *kcp);
383
384 // change MTU size, default is 1400
385 int ikcp_setmtu(ikcpcb *kcp, int mtu);
386
387 // set maximum window size: sndwnd=32, rcvwnd=32 by default
388 int ikcp_wndsize(ikcpcb *kcp, int sndwnd, int rcvwnd);
389
390 // get how many packet is waiting to be sent
391 int ikcp_waitsnd(const ikcpcb *kcp);
392
393 // fastest: ikcp_nodelay(kcp, 1, 20, 2, 1)
394 // nodelay: 0:disable(default), 1:enable
395 // interval: internal update timer interval in millisec, default is 100ms
396 // resend: 0:disable fast resend(default), 1:enable fast resend
397 // nc: 0:normal congestion control(default), 1:disable congestion control
398 int ikcp_nodelay(ikcpcb *kcp, int nodelay, int interval, int resend, int nc);
399
400
401 void ikcp_log(ikcpcb *kcp, int mask, const char *fmt, ...);
402
403 // setup allocator
404 void ikcp_allocator(void* (*new_malloc)(size_t), void (*new_free)(void*));
405
406 // read conv
407 IUINT32 ikcp_getconv(const void *ptr);
408
409
410 #ifdef __cplusplus
411 }
412 #endif
413
414 #endif
415
416