netq.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * netq.h
  3. *
  4. * Copyright (C) 2022 bzt (bztsrc@gitlab) MIT license
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief Simple reliable datagram implementation with circular FIFO network queue
  27. * https://gitlab.com/bztsrc/netq
  28. */
  29. #ifndef NETQ_H
  30. #define NETQ_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #include <stdint.h>
  35. #include <string.h>
  36. /* the `nq` is a netq_t type, opaque to the application.
  37. * the `net` is a raw network layer type, as you please, opaque to NetQ. */
  38. int netq_send(void *nq, const void *msg, int len, void *net); /* send a normal, reliable message with data */
  39. int netq_rst (void *nq, void *net); /* send a non-reliable, non-message RESET packet */
  40. int netq_ack (void *nq, void *net); /* send a non-reliable, non-message ACK packet */
  41. int netq_nack(void *nq, void *net); /* send a non-reliable, non-message NACK packet */
  42. int netq_push(void *nq, const void *raw, int len, void *net); /* push a received raw packet to the queue */
  43. int netq_pop (void *nq, void *msg, int len); /* pop a message in correct order from the queue */
  44. int netq_pend(void *nq); /* check if there's any message pending */
  45. void netq_dump(void *nq); /* dump queues to stdout (for debugging) */
  46. #if defined(NETQ_CONFIG) || defined(NETQ_IMPLEMENTATION)
  47. #ifndef NETQ_HASCONFIG
  48. #define NETQ_HASCONFIG
  49. /*** configuration ***/
  50. #ifndef NETQ_MTU
  51. #define NETQ_MTU 1280
  52. #endif
  53. #if NETQ_MTU < 576 || NETQ_MTU > 1500
  54. #error "Unlikely NETQ_MTU value. Use something between 576 and 1500"
  55. #endif
  56. #ifndef NETQ_SEQ_BITS
  57. #define NETQ_SEQ_BITS 16
  58. #endif
  59. #ifndef NETQ_ACK_BITS
  60. #define NETQ_ACK_BITS 16
  61. #endif
  62. #ifndef NETQ_NACK_TRES
  63. #define NETQ_NACK_TRES 24
  64. #endif
  65. #ifndef NETQ_PACKED
  66. #ifndef _MSC_VER
  67. #define NETQ_PACKED __attribute__((packed))
  68. #else
  69. #define NETQ_PACKED
  70. #endif
  71. #endif
  72. #if (defined(NETQ_MUTEX_TYPE) && (!defined(NETQ_MUTEX_LOCK) || !defined(NETQ_MUTEX_UNLOCK))) || \
  73. (defined(NETQ_MUTEX_LOCK) && (!defined(NETQ_MUTEX_TYPE) || !defined(NETQ_MUTEX_UNLOCK))) || \
  74. (defined(NETQ_MUTEX_UNLOCK) && (!defined(NETQ_MUTEX_TYPE) || !defined(NETQ_MUTEX_LOCK)))
  75. #error "All of NETQ_MUTEX_TYPE, NETQ_MUTEX_LOCK and NETQ_MUTEX_UNLOCK must be defined together."
  76. #endif
  77. #if !defined(NETQ_MUTEX_TYPE) && !defined(NETQ_NO_PTHREAD) && defined(_PTHREAD_H)
  78. #define NETQ_MUTEX_TYPE pthread_mutex_t
  79. #define NETQ_MUTEX_LOCK(m) pthread_mutex_lock(m)
  80. #define NETQ_MUTEX_UNLOCK(m) pthread_mutex_unlock(m)
  81. #endif
  82. /*** calculated from configuration ***/
  83. #if NETQ_SEQ_BITS == 8
  84. #define NETQ_SEQ uint8_t
  85. #define NETQ_SEQ_MAX 64 /* calculate as 2^(NETQ_SEQ_BITS-2) */
  86. #define NETQ_SEQ_MSK 0x7f /* calculate as (2^NETQ_SEQ_BITS)-1 */
  87. #define NETQ_HTON
  88. #define NETQ_NTOH
  89. #elif NETQ_SEQ_BITS == 16
  90. #define NETQ_SEQ uint16_t
  91. #define NETQ_SEQ_MAX 16384
  92. #define NETQ_SEQ_MSK 0x7fff
  93. #define NETQ_HTON htons
  94. #define NETQ_NTOH ntohs
  95. #elif NETQ_SEQ_BITS == 32
  96. #define NETQ_SEQ uint32_t
  97. #define NETQ_SEQ_MAX 1073741824
  98. #define NETQ_SEQ_MSK 0x7fffffff
  99. #define NETQ_HTON htonl
  100. #define NETQ_NTOH ntohl
  101. #else
  102. #error "Invalid NETQ_SEQ_BITS, can be 8, 16 or 32"
  103. #endif
  104. #if NETQ_ACK_BITS == 8
  105. #define NETQ_ACK uint8_t
  106. #elif NETQ_ACK_BITS == 16
  107. #define NETQ_ACK uint16_t
  108. #elif NETQ_ACK_BITS == 32
  109. #define NETQ_ACK uint32_t
  110. #elif NETQ_ACK_BITS == 64
  111. #define NETQ_ACK uint64_t
  112. #else
  113. #error "Invalid NETQ_ACK_BITS, can be 8, 16, 32 or 64"
  114. #endif
  115. #ifndef NETQ_QUEUE_SIZE
  116. #define NETQ_QUEUE_SIZE NETQ_ACK_BITS
  117. #endif
  118. #if NETQ_QUEUE_SIZE < NETQ_ACK_BITS
  119. #error "NETQ_QUEUE_SIZE must be at least NETQ_ACK_BITS"
  120. #endif
  121. #if NETQ_NACK_TRES < 0 || NETQ_NACK_TRES >= 2 * NETQ_ACK_BITS
  122. #error "Unlikely NETQ_NACK_TRES value. Use something like 1.5 * NETQ_ACK_BITS"
  123. #endif
  124. /* RFC 1982 compatible sequence number comparator */
  125. #define NETQ_SEQ_EQ(i1,i2) ((i1)==(i2))
  126. #define NETQ_SEQ_LE(i1,i2) ((i1)<(i2) && ((i2)-(i1)<NETQ_SEQ_MAX))
  127. /* the message header */
  128. enum { NETQ_MSG_TYPE_MSG, NETQ_MSG_TYPE_RST, NETQ_MSG_TYPE_ACK, NETQ_MSG_TYPE_NACK };
  129. #define NETQ_MSG_TYPE(m) ((((m)->seq >> 7) & 1) | (((m)->ack >> 6) & 2))
  130. #ifdef _MSC_VER
  131. #pragma pack(push)
  132. #pragma pack(1)
  133. #endif
  134. typedef struct {
  135. NETQ_SEQ seq; /* this packet's sequence number, MSB: type bit 0 */
  136. NETQ_SEQ ack; /* ack bitmask first sequence number, MSB: type bit 1 */
  137. NETQ_ACK msk; /* ack bitmask */
  138. } NETQ_PACKED netq_hdr_t;
  139. #ifdef _MSC_VER
  140. #pragma pack(pop)
  141. #endif
  142. /* the network queue context */
  143. #define NETQ_QUE_IDX(s) ((((s) & NETQ_SEQ_MSK) & (NETQ_QUEUE_SIZE - 1)) * (NETQ_MTU + 2)) /* get position in queue */
  144. #define NETQ_QUE_LEN(b) ((((b)[1] & 0xf) << 8) | (b)[0]) /* size of the message in the queue */
  145. #define NETQ_QUE_ACK(b) ((b)[1] & 0x10) /* queue entry got ack */
  146. typedef struct {
  147. NETQ_SEQ seq_out; /* outgoing sequence number */
  148. NETQ_SEQ seq_in; /* incoming sequence number */
  149. NETQ_SEQ seq_ack; /* largest sequence number that got acked */
  150. NETQ_SEQ seq_pop; /* next packet to be popped */
  151. uint8_t send[NETQ_QUEUE_SIZE * (NETQ_MTU + 2)]; /* the outgoing queue */
  152. uint8_t recv[NETQ_QUEUE_SIZE * (NETQ_MTU + 2)]; /* the incoming queue */
  153. #ifdef NETQ_MUTEX_TYPE
  154. NETQ_MUTEX_TYPE mutex;
  155. #endif
  156. } netq_t;
  157. #endif /* NETQ_HASCONFIG */
  158. #endif /* NETQ_CONFIG || NETQ_IMPLEMENTATION */
  159. #ifdef NETQ_IMPLEMENTATION
  160. #if !defined(NETQ_SEND)
  161. #error "You must define a NETQ_SEND function"
  162. #endif
  163. int NETQ_SEND(void *ctx, const void *msg, int len);
  164. /*** Private functions ***/
  165. void _netq_wrack(netq_t *ctx, netq_hdr_t *hdr)
  166. {
  167. NETQ_SEQ i, j;
  168. uint8_t *buf;
  169. #ifdef __BIG_ENDIAN
  170. uint8_t *msk = (uint8_t*)&hdr->msk;
  171. #endif
  172. j = (ctx->seq_in - 1) & NETQ_SEQ_MSK;
  173. hdr->ack = NETQ_HTON(j);
  174. hdr->msk = 0;
  175. for(i = 0; i < NETQ_ACK_BITS; i++) {
  176. buf = &ctx->recv[NETQ_QUE_IDX(j)];
  177. if(((netq_hdr_t*)(buf + 2))->seq == j && NETQ_QUE_LEN(buf))
  178. #ifdef __BIG_ENDIAN
  179. msk[i >> 3] |= (1 << (i & 7));
  180. #else
  181. hdr->msk |= (1 << i);
  182. #endif
  183. j--; j &= NETQ_SEQ_MSK;
  184. }
  185. }
  186. void _netq_resend(netq_t *ctx, NETQ_SEQ a, NETQ_ACK m, void *net)
  187. {
  188. NETQ_SEQ i, j, k;
  189. netq_hdr_t *hdr, ack_new = { 0 };
  190. uint8_t *buf;
  191. _netq_wrack(ctx, &ack_new);
  192. for(i = 0, j = a; i < NETQ_ACK_BITS; i++, m >>= 1) {
  193. k = NETQ_QUE_IDX(j); buf = &ctx->send[k]; hdr = (netq_hdr_t*)(buf + 2);
  194. if((m & 1) && hdr->seq == j && NETQ_QUE_LEN(buf) && !NETQ_QUE_ACK(buf)) {
  195. hdr->seq = NETQ_HTON(hdr->seq);
  196. hdr->ack = ack_new.ack; hdr->msk = ack_new.msk;
  197. NETQ_SEND(net, buf + 2, NETQ_QUE_LEN(buf));
  198. hdr->seq = NETQ_NTOH(hdr->seq);
  199. }
  200. j--; j &= NETQ_SEQ_MSK;
  201. }
  202. }
  203. /*** Public API ***/
  204. /**
  205. * Add net queue send header to message
  206. * @param nq: the NetQ instance (opaque to the application)
  207. * @param msg: the message buffer
  208. * @param len: length of message
  209. * @param net: your networking layer instance (opaque to NetQ)
  210. * @return negative on error, number bytes sent otherwise.
  211. */
  212. int netq_send(void *nq, const void *msg, int len, void *net)
  213. {
  214. int ret;
  215. netq_t *ctx = (netq_t*)nq;
  216. netq_hdr_t *hdr;
  217. uint8_t *buf;
  218. if(!ctx || !msg || len < 0 || len >= NETQ_MTU - (int)sizeof(netq_hdr_t)) return -2;
  219. #ifdef NETQ_MUTEX_TYPE
  220. NETQ_MUTEX_LOCK(&ctx->mutex);
  221. #endif
  222. /* automatically resend all unacked older packets */
  223. _netq_resend(ctx, (ctx->seq_ack - NETQ_ACK_BITS) & NETQ_SEQ_MSK, -1, net);
  224. /* construct and send the current message */
  225. buf = &ctx->send[NETQ_QUE_IDX(ctx->seq_out)]; hdr = (netq_hdr_t*)(buf + 2);
  226. if(msg && len > 0)
  227. memcpy(buf + 2 + sizeof(netq_hdr_t), msg, len);
  228. len += sizeof(netq_hdr_t);
  229. buf[0] = len & 0xff; buf[1] = (len >> 8) & 0xf;
  230. hdr->seq = NETQ_HTON(ctx->seq_out);
  231. _netq_wrack(ctx, hdr);
  232. ret = NETQ_SEND(net, buf + 2, len);
  233. hdr->seq = NETQ_NTOH(hdr->seq);
  234. if(ret < len) { memset(buf, 0, NETQ_MTU + 2); } else { ctx->seq_out++; ctx->seq_out &= NETQ_SEQ_MSK; }
  235. #ifdef NETQ_MUTEX_TYPE
  236. NETQ_MUTEX_UNLOCK(&ctx->mutex);
  237. #endif
  238. return ret;
  239. }
  240. /**
  241. * Send a non-reliable, non-message reset packet
  242. * @param nq: the NetQ instance (opaque to the application)
  243. * @param net: your networking layer instance (opaque to NetQ)
  244. * @return negative on error, number bytes sent otherwise.
  245. */
  246. int netq_rst(void *nq, void *net)
  247. {
  248. netq_t *ctx = (netq_t*)nq;
  249. netq_hdr_t hdr = { 0 };
  250. if(!ctx) return -2;
  251. hdr.seq = NETQ_HTON(ctx->seq_out) | 0x80;
  252. return NETQ_SEND(net, &hdr, sizeof(netq_hdr_t));
  253. }
  254. /**
  255. * Send a non-reliable, non-message acknowledge packet
  256. * @param nq: the NetQ instance (opaque to the application)
  257. * @param net: your networking layer instance (opaque to NetQ)
  258. * @return negative on error, number bytes sent otherwise.
  259. */
  260. int netq_ack(void *nq, void *net)
  261. {
  262. netq_t *ctx = (netq_t*)nq;
  263. netq_hdr_t hdr = { 0 };
  264. if(!ctx) return -2;
  265. hdr.seq = NETQ_HTON(ctx->seq_out);
  266. _netq_wrack(ctx, &hdr);
  267. hdr.ack |= 0x80;
  268. return NETQ_SEND(net, &hdr, sizeof(netq_hdr_t));
  269. }
  270. /**
  271. * Send a non-reliable, non-message negative acknowledge (re-transmission request) packet
  272. * @param nq: the NetQ instance (opaque to the application)
  273. * @param net: your networking layer instance (opaque to NetQ)
  274. * @return negative on error, number bytes sent otherwise.
  275. */
  276. int netq_nack(void *nq, void *net)
  277. {
  278. NETQ_SEQ i, j, k, a;
  279. netq_t *ctx = (netq_t*)nq;
  280. netq_hdr_t hdr = { 0 };
  281. #ifdef __BIG_ENDIAN
  282. uint8_t *msk = (uint8_t*)&hdr.msk;
  283. #endif
  284. if(!ctx) return -2;
  285. a = (ctx->seq_in - 1 - NETQ_ACK_BITS) & NETQ_SEQ_MSK;
  286. for(i = 0, j = a; i < NETQ_ACK_BITS; i++) {
  287. k = NETQ_QUE_IDX(j);
  288. if(((netq_hdr_t*)(&ctx->recv[k + 2]))->seq != j || !NETQ_QUE_LEN(&ctx->recv[k]))
  289. #ifdef __BIG_ENDIAN
  290. msk[i >> 3] |= (1 << (i & 7));
  291. #else
  292. hdr.msk |= (1 << i);
  293. #endif
  294. j--; j &= NETQ_SEQ_MSK;
  295. }
  296. if(hdr.msk) {
  297. hdr.seq = NETQ_HTON(ctx->seq_out) | 0x80;
  298. hdr.ack = NETQ_HTON(a) | 0x80;
  299. return NETQ_SEND(net, &hdr, sizeof(netq_hdr_t));
  300. }
  301. return 0;
  302. }
  303. /**
  304. * Push a raw packet (message with header) to the queue and send ACK or NACK if needed
  305. * @param nq: the NetQ instance (opaque to the application)
  306. * @param raw: the raw packet (message with header) buffer
  307. * @param len: length of raw packet
  308. * @param net: your networking layer instance (opaque to NetQ)
  309. * @return 1 on success, 0 on error (more packet lost or arrived than the queue size).
  310. */
  311. int netq_push(void *nq, const void *raw, int len, void *net)
  312. {
  313. NETQ_SEQ i, j, k, msg_seq, msg_ack;
  314. NETQ_ACK msg_msk = 0;
  315. int ret = 1;
  316. netq_t *ctx = (netq_t*)nq;
  317. netq_hdr_t *hdr_in, *hdr_buf, hdr_ack;
  318. uint8_t *buf, msg_type;
  319. #ifdef __BIG_ENDIAN
  320. uint8_t *msk;
  321. #endif
  322. if(!ctx || !raw || len < (int)sizeof(netq_hdr_t) || len >= NETQ_MTU) return 0;
  323. #ifdef NETQ_MUTEX_TYPE
  324. NETQ_MUTEX_LOCK(&ctx->mutex);
  325. #endif
  326. hdr_in = (netq_hdr_t*)raw; msg_type = NETQ_MSG_TYPE(hdr_in);
  327. msg_seq = NETQ_NTOH(hdr_in->seq) & NETQ_SEQ_MSK; msg_ack = NETQ_NTOH(hdr_in->ack) & NETQ_SEQ_MSK;
  328. buf = &ctx->recv[NETQ_QUE_IDX(msg_seq)]; hdr_buf = (netq_hdr_t*)(buf + 2);
  329. #ifdef __BIG_ENDIAN
  330. msk = (uint8_t*)&hdr_in->msk;
  331. for(i = 0; i < NETQ_ACK_BITS; i++)
  332. if(msk[i >> 3] & (1 << (i & 7))) msg_msk |= (1 << i);
  333. #else
  334. msg_msk = hdr_in->msk;
  335. #endif
  336. switch(msg_type) {
  337. case NETQ_MSG_TYPE_RST:
  338. #ifdef NETQ_MUTEX_TYPE
  339. /* do not clear the mutex */
  340. ctx->seq_out = ctx->seq_in = ctx->seq_ack = ctx->seq_pop = 0;
  341. memset(ctx->send, 0, sizeof(ctx->send)); memset(ctx->recv, 0, sizeof(ctx->recv));
  342. #else
  343. memset(ctx, 0, sizeof(netq_t));
  344. #endif
  345. break;
  346. case NETQ_MSG_TYPE_NACK:
  347. /* for NACK, the ack mask is negated, eg. contains which packets to resend */
  348. _netq_resend(ctx, msg_ack, msg_msk, net);
  349. break;
  350. case NETQ_MSG_TYPE_ACK:
  351. case NETQ_MSG_TYPE_MSG:
  352. /* update ack flags in send queue from the newly received message */
  353. j = msg_ack;
  354. for(i = 0; i < NETQ_ACK_BITS; i++) {
  355. k = NETQ_QUE_IDX(j);
  356. if(((netq_hdr_t*)(&ctx->send[k + 2]))->seq == j) ctx->send[k + 1] |= 0x10;
  357. j--; j &= NETQ_SEQ_MSK;
  358. }
  359. /* send ACK or NACK packets when needed */
  360. if(NETQ_SEQ_LE((ctx->seq_ack + NETQ_NACK_TRES) & NETQ_SEQ_MSK, msg_ack)) {
  361. netq_nack(nq, net);
  362. ctx->seq_ack = msg_ack;
  363. } else
  364. if(NETQ_SEQ_EQ(ctx->seq_ack, msg_ack)) {
  365. _netq_wrack(ctx, &hdr_ack);
  366. if(hdr_in->msk != hdr_ack.msk) netq_ack(nq, net);
  367. }
  368. if(msg_type == NETQ_MSG_TYPE_ACK) break;
  369. /* store message in queue */
  370. if(NETQ_SEQ_EQ(ctx->seq_in, msg_seq) || NETQ_SEQ_LE(ctx->seq_in, msg_seq))
  371. ctx->seq_in = (msg_seq + 1) & NETQ_SEQ_MSK;
  372. else if(NETQ_SEQ_LE(msg_seq, ctx->seq_in) && hdr_buf->seq == msg_seq) break;
  373. len -= sizeof(netq_hdr_t); buf[0] = len & 0xff; buf[1] = (len >> 8) & 0xf;
  374. #ifdef __BIG_ENDIAN
  375. hdr_buf->seq = msg_seq; hdr_buf->ack = msg_ack; hdr_buf->msk = msg_msk;
  376. memcpy(buf + 2 + sizeof(netq_hdr_t), (uint8_t*)raw + sizeof(netq_hdr_t), len);
  377. #else
  378. memcpy(buf + 2, raw, len + sizeof(netq_hdr_t));
  379. #endif
  380. /* uh oh, should never happen, unless queue was too small */
  381. if(NETQ_SEQ_LE(msg_seq, ctx->seq_pop) || NETQ_SEQ_LE(hdr_buf->seq, ctx->seq_pop)) ret = 0;
  382. break;
  383. }
  384. #ifdef NETQ_MUTEX_TYPE
  385. NETQ_MUTEX_UNLOCK(&ctx->mutex);
  386. #endif
  387. return ret;
  388. }
  389. /**
  390. * Receive message in correct order
  391. * @param nq: the NetQ instance (opaque to the application)
  392. * @param msg: output buffer (should be at least NETQ_MTU big)
  393. * @param len: length of output buffer
  394. * @return -1 on error, 0 if there was no new message, otherwise the number of bytes received and message in `msg`.
  395. */
  396. int netq_pop (void *nq, void *msg, int len)
  397. {
  398. int l;
  399. netq_t *ctx = (netq_t*)nq;
  400. uint8_t *buf;
  401. if(!ctx || !msg || len < 1 || !netq_pend(nq)) return 0;
  402. /* no need for locking this */
  403. buf = &ctx->recv[NETQ_QUE_IDX(ctx->seq_pop)];
  404. l = NETQ_QUE_LEN(buf);
  405. if(l < 1 || l > len || ((netq_hdr_t*)(buf + 2))->seq != ctx->seq_pop) return -1;
  406. memcpy(msg, buf + 2 + sizeof(netq_hdr_t), l);
  407. ctx->seq_pop++; ctx->seq_pop &= NETQ_SEQ_MSK;
  408. return l;
  409. }
  410. /**
  411. * Return true if there's a message pending in the queue
  412. */
  413. int netq_pend(void *nq)
  414. {
  415. netq_t *ctx = (netq_t*)nq;
  416. return ctx && NETQ_SEQ_LE(ctx->seq_pop, ctx->seq_in);
  417. }
  418. #ifndef NETQ_NODEBUG
  419. /**
  420. * Dump queue to stdout
  421. */
  422. void netq_dump(void *nq)
  423. {
  424. #if NETQ_QUEUE_SIZE == 8
  425. #define NETQ_STR "2"
  426. #elif NETQ_QUEUE_SIZE == 16
  427. #define NETQ_STR "4"
  428. #elif NETQ_QUEUE_SIZE == 32
  429. #define NETQ_STR "8"
  430. #elif NETQ_QUEUE_SIZE == 64
  431. #define NETQ_STR "16l"
  432. #endif
  433. int i, j;
  434. netq_t *ctx = (netq_t*)nq;
  435. netq_hdr_t *hdr;
  436. uint8_t *buf;
  437. printf("NetQ configuration: sequence number: uint%u_t, ack mask: uint%u_t, mtu: %u, header: %u, queue: %u, memory: %u bytes"
  438. #ifdef NETQ_MUTEX_TYPE
  439. ", thread-safety"
  440. #endif
  441. ".\n", NETQ_SEQ_BITS, NETQ_ACK_BITS, NETQ_MTU, (unsigned int)sizeof(netq_hdr_t), NETQ_QUEUE_SIZE, (unsigned int)sizeof(netq_t));
  442. if(ctx) {
  443. printf("\nCounters: out: %u, in: %u, latest ack: %u, next pop: %u, netq_pend()=%u\n\nSender queue:\n",
  444. ctx->seq_out, ctx->seq_in, ctx->seq_ack, ctx->seq_pop, netq_pend(nq));
  445. for(i = 0, buf = ctx->send; i < NETQ_QUEUE_SIZE; i++, buf += NETQ_MTU + 2)
  446. if(NETQ_QUE_LEN(buf))
  447. printf("%2u. len:%4u, seq:%6u, acked: %u\n", i, NETQ_QUE_LEN(buf), ((netq_hdr_t*)(buf + 2))->seq, buf[1] >> 4);
  448. printf("\nReceiver queue:\n");
  449. for(i = 0, buf = ctx->recv; i < NETQ_QUEUE_SIZE; i++, buf += NETQ_MTU + 2) {
  450. if(NETQ_QUE_LEN(buf)) {
  451. hdr = (netq_hdr_t*)(buf + 2);
  452. printf("%2u. len:%4u, seq:%6u, ack:%6u/%0" NETQ_STR "x (seqs", i, NETQ_QUE_LEN(buf), hdr->seq, hdr->ack, hdr->msk);
  453. for(j = 0; j < NETQ_QUEUE_SIZE; j++) if(hdr->msk & (1 << j)) printf(" %u", hdr->ack - j);
  454. printf(")\n");
  455. }
  456. }
  457. printf("\n");
  458. }
  459. #undef NETQ_STR
  460. }
  461. #endif /* !NETQ_NODEBUG */
  462. #endif /* NETQ_IMPLEMENTATION */
  463. #ifdef __cplusplus
  464. }
  465. #endif
  466. #endif /* NETQ_H */