Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_network_msg.c
Go to the documentation of this file.
1
8#include "nilorea/n_common.h"
9#include "nilorea/n_log.h"
10#include "nilorea/n_network.h"
12
18double double_swap(double value) {
19 double swaped = 0;
20 unsigned char* src = (unsigned char*)&value;
21 unsigned char* dst = (unsigned char*)&swaped;
22
23 /* swap bytes */
24 dst[0] = src[7];
25 dst[1] = src[6];
26 dst[2] = src[5];
27 dst[3] = src[4];
28 dst[4] = src[3];
29 dst[5] = src[2];
30 dst[6] = src[1];
31 dst[7] = src[0];
32
33 return swaped;
34} /* double_swap() */
35
41double htond(double value) {
42 double ret = value;
43 /* if byte order is little like x86, PC, ... we swap to network notation */
44#if (BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN)
45 ret = double_swap(value);
46#endif
47 return ret;
48} /* htond() */
49
55double ntohd(double value) {
56 double ret = value;
57 /* if byte order is little like x86, PC, ... we swap to network notation */
58#if (BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN)
59 ret = double_swap(value);
60#endif
61 return ret;
62} /* ntohd() */
63
69int create_msg(NETW_MSG** msg) {
70 if ((*msg)) {
71 n_log(LOG_ERR, "create_msg destination is valid and should be NULL !");
72 return FALSE;
73 }
74
75 Malloc((*msg), NETW_MSG, 1);
76 __n_assert(msg && (*msg), return FALSE);
77
78 (*msg)->tabstr = new_generic_list(MAX_LIST_ITEMS);
79 __n_assert((*msg)->tabstr, Free((*msg)); return FALSE);
80 (*msg)->tabint = new_generic_list(MAX_LIST_ITEMS);
81 __n_assert((*msg)->tabint, list_destroy(&(*msg)->tabstr); Free((*msg)); return FALSE);
82 (*msg)->tabflt = new_generic_list(MAX_LIST_ITEMS);
83 __n_assert((*msg)->tabflt, list_destroy(&(*msg)->tabstr); list_destroy(&(*msg)->tabint); Free((*msg)); return FALSE);
84
85 return TRUE;
86} /* create_msg( ... ) */
87
93int delete_msg(NETW_MSG** msg) {
94 if ((*msg)) {
95 if ((*msg)->tabint) {
96 list_destroy(&(*msg)->tabint);
97 }
98 if ((*msg)->tabflt) {
99 list_destroy(&(*msg)->tabflt);
100 }
101 if ((*msg)->tabstr) {
102 list_destroy(&(*msg)->tabstr);
103 }
104 Free((*msg));
105 } else {
106 n_log(LOG_ERR, "(*msg) already NULL");
107 return FALSE;
108 }
109 return TRUE;
110} /* delete_msg */
111
117int empty_msg(NETW_MSG** msg) {
118 __n_assert(msg && (*msg), return FALSE);
119
120 list_empty((*msg)->tabint);
121 list_empty((*msg)->tabflt);
122 list_empty((*msg)->tabstr);
123
124 return TRUE;
125} /* empty_msg(...) */
126
134int add_nb_to_msg(NETW_MSG* msg, double value) {
135 double* new_val = NULL;
136
137 __n_assert(msg, return FALSE);
138
139 Malloc(new_val, double, 1);
140 __n_assert(new_val, return FALSE);
141
142 new_val[0] = value;
143
144 return list_push(msg->tabflt, new_val, free);
145} /* add_nb_to_msg( ... ) */
146
153int add_int_to_msg(NETW_MSG* msg, int value) {
154 int* new_val = NULL;
155
156 __n_assert(msg, return FALSE);
157
158 Malloc(new_val, int, 1);
159 __n_assert(new_val, return FALSE);
160
161 new_val[0] = value;
162
163 return list_push(msg->tabint, new_val, free);
164} /* add_int_to_msg( ... ) */
165
173 __n_assert(msg, return FALSE);
174 __n_assert(str, return FALSE);
175 __n_assert(str->data, return FALSE);
176 return list_push(msg->tabstr, str, free_nstr_ptr);
177} /* add_nstrptr_to_msg(...) */
178
186 __n_assert(msg, return FALSE);
187 __n_assert(str, return FALSE);
188 __n_assert(str->data, return FALSE);
189 N_STR* dup = nstrdup(str);
190 __n_assert(dup, return FALSE);
191 return list_push(msg->tabstr, dup, free_nstr_ptr);
192} /* add_nstrdup_to_msg(...) */
193
200int add_strdup_to_msg(NETW_MSG* msg, const char* str) {
201 __n_assert(msg, return FALSE);
202 __n_assert(str, return FALSE);
203
204 N_STR* dup = char_to_nstr(str);
205 __n_assert(dup, return FALSE);
206
207 return list_push(msg->tabstr, dup, free_nstr_ptr);
208} /* add_strdup_to_msg(...) */
209
216int get_nb_from_msg(NETW_MSG* msg, double* value) {
217 double* val = NULL;
218
219 __n_assert(msg, return FALSE);
220
221 val = list_shift(msg->tabflt, double);
222 if (val) {
223 (*value) = val[0];
224 Free(val);
225 return TRUE;
226 }
227 return FALSE;
228} /* get_nb_from_msg(...) */
229
236int get_int_from_msg(NETW_MSG* msg, int* value) {
237 int* val = NULL;
238
239 __n_assert(msg, return FALSE);
240
241 val = list_shift(msg->tabint, int);
242 if (val) {
243 (*value) = val[0];
244 Free(val);
245 return TRUE;
246 }
247 return FALSE;
248
249} /* get_int_from_msg(...) */
250
257int get_nstr_from_msg(NETW_MSG* msg, N_STR** value) {
258 N_STR* val = NULL;
259
260 __n_assert(msg, return FALSE);
261
262 val = list_shift(msg->tabstr, N_STR);
263 if (val) {
264 if ((*value)) {
265 n_log(LOG_ERR, "Previous pointer value %p overriden by pointer %p", (*value), val);
266 }
267 (*value) = val;
268 return TRUE;
269 }
270 return FALSE;
271} /* get_nstr_from_msg(...) */
272
279int get_str_from_msg(NETW_MSG* msg, char** value) {
280 N_STR* val = NULL;
281
282 __n_assert(msg, return FALSE);
283
284 val = list_shift(msg->tabstr, N_STR);
285 if (val) {
286 if ((*value)) {
287 n_log(LOG_ERR, "Previous pointer value %p overriden by pointer %p", (*value), val);
288 }
289
290 (*value) = val->data;
291 Free(val);
292 return TRUE;
293 }
294 return FALSE;
295} /* get_str_from_msg(...) */
296
303 size_t str_length = 0;
304 LIST_NODE* node = NULL;
305
306 N_STR *strptr = NULL,
307 *generated_str = NULL;
308
309 char* charptr = NULL;
310
311 __n_assert(msg, return NULL);
312
313 /* the first thing to do is to computer the resulting string size
314 the first 12 octets are for the number of ints , floats, strings in the message
315 */
316 str_length = 3 * sizeof(int32_t) + (msg->tabint->nb_items * sizeof(int32_t)) + (msg->tabflt->nb_items * sizeof(double));
317
318 /* let's count the eventual string blocks */
319 node = msg->tabstr->start;
320 while (node) {
321 strptr = (N_STR*)node->ptr;
322 str_length += sizeof(int32_t) + sizeof(int32_t) + strptr->written;
323 node = node->next;
324 }
325
326 /* preparing the new string */
327 Malloc(generated_str, N_STR, 1);
328 __n_assert(generated_str, return NULL);
329 Malloc(generated_str->data, char, str_length + 1);
330 __n_assert(generated_str->data, Free(generated_str); return NULL);
331
332 generated_str->length = str_length + 1;
333 generated_str->written = str_length;
334
335 /* copying header */
336 charptr = generated_str->data;
337
338 /* testing limits */
339 if (msg->tabint->nb_items >= UINT32_MAX) {
340 n_log(LOG_ERR, "too much items (>=UINT32_MAX) in int list of message %p", msg);
341 free_nstr(&generated_str);
342 return NULL;
343 }
344 if (msg->tabflt->nb_items >= UINT32_MAX) {
345 n_log(LOG_ERR, "too much items (>=UINT32_MAX) in float list of message %p", msg);
346 free_nstr(&generated_str);
347 return NULL;
348 }
349 if (msg->tabstr->nb_items >= UINT32_MAX) {
350 n_log(LOG_ERR, "too much items (>=UINT32_MAX) in string list of message %p", msg);
351 free_nstr(&generated_str);
352 return NULL;
353 }
354
355 /* byte order */
356 uint32_t nb_int_items = htonl((uint32_t)msg->tabint->nb_items);
357 uint32_t nb_float_items = htonl((uint32_t)msg->tabflt->nb_items);
358 uint32_t nb_str_items = htonl((uint32_t)msg->tabstr->nb_items);
359
360 /* number of ints inside the message */
361 memcpy(charptr, &nb_int_items, sizeof(uint32_t));
362 charptr += sizeof(uint32_t);
363 /* number of doubles inside the message */
364 memcpy(charptr, &nb_float_items, sizeof(uint32_t));
365 charptr += sizeof(uint32_t);
366 /* number of string inside the message */
367 memcpy(charptr, &nb_str_items, sizeof(uint32_t));
368 charptr += sizeof(uint32_t);
369
370 /* if there are numbers, copy all them ! */
371 node = msg->tabint->start;
372 while (node) {
373 uint32_t* nbptr = (uint32_t*)node->ptr;
374 uint32_t val = htonl(nbptr[0]);
375 memcpy(charptr, &val, sizeof(uint32_t));
376 charptr += sizeof(uint32_t);
377 node = node->next;
378 }
379
380 /* if there are numbers, copy all them ! */
381 node = msg->tabflt->start;
382 while (node) {
383 double* nbptr = (double*)node->ptr;
384 double val = htond(nbptr[0]);
385 memcpy(charptr, &val, sizeof(double));
386 charptr += sizeof(double);
387 node = node->next;
388 }
389
390 /* if there are strings, copy all them ! */
391 if (msg->tabstr->nb_items > 0) {
392 node = msg->tabstr->start;
393 while (node) {
394 strptr = (N_STR*)node->ptr;
395 if (strptr->length >= UINT32_MAX) {
396 n_log(LOG_ERR, "string too long (>=UINT32_MAX) in string list of message %p", msg);
397 free_nstr(&generated_str);
398 return NULL;
399 }
400 uint32_t var = htonl((uint32_t)strptr->length);
401 memcpy(charptr, &var, sizeof(uint32_t));
402 charptr += sizeof(uint32_t);
403
404 if (strptr->written >= UINT32_MAX) {
405 n_log(LOG_ERR, "string too long (>=UINT32_MAX) in string list of message %p", msg);
406 free_nstr(&generated_str);
407 return NULL;
408 }
409 var = htonl((uint32_t)strptr->written);
410 memcpy(charptr, &var, sizeof(uint32_t));
411 charptr += sizeof(uint32_t);
412
413 memcpy(charptr, strptr->data, strptr->written);
414 charptr += strptr->written;
415 node = node->next;
416 }
417 }
418
419 return generated_str;
420} /* make_str_from_msg(...) */
421
428 NETW_MSG* generated_msg = NULL;
429 N_STR* tmpstr = NULL;
430
431 char* charptr = NULL;
432
433 uint32_t it,
434 nb_int = 0,
435 nb_flt = 0,
436 nb_str = 0;
437 int32_t tmpnb = 0;
438
439 double tmpflt = 0;
440
441 __n_assert(str, return NULL);
442
443 charptr = str->data;
444
445 create_msg(&generated_msg);
446
447 __n_assert(generated_msg, return NULL);
448
449 memcpy(&nb_int, charptr, sizeof(uint32_t));
450 charptr += sizeof(uint32_t);
451 nb_int = ntohl(nb_int);
452
453 memcpy(&nb_flt, charptr, sizeof(uint32_t));
454 charptr += sizeof(uint32_t);
455 nb_flt = ntohl(nb_flt);
456
457 memcpy(&nb_str, charptr, sizeof(uint32_t));
458 charptr += sizeof(uint32_t);
459 nb_str = ntohl(nb_str);
460
461 if (nb_int > 0) {
462 for (it = 0; it < nb_int; it++) {
463 memcpy(&tmpnb, charptr, sizeof(uint32_t));
464 tmpnb = (int32_t)ntohl((uint32_t)tmpnb);
465 charptr += sizeof(uint32_t);
466 add_int_to_msg(generated_msg, tmpnb);
467 }
468 }
469
470 if (nb_flt > 0) {
471 for (it = 0; it < nb_flt; it++) {
472 memcpy(&tmpflt, charptr, sizeof(double));
473 tmpflt = ntohd(tmpflt);
474 charptr += sizeof(double);
475 add_nb_to_msg(generated_msg, tmpflt);
476 }
477 }
478
479 if (nb_str > 0) {
480 for (it = 0; it < nb_str; it++) {
481 Malloc(tmpstr, N_STR, 1);
482 __n_assert(tmpstr, return NULL);
483 uint32_t var = 0;
484 memcpy(&var, charptr, sizeof(uint32_t));
485 charptr += sizeof(uint32_t);
486 tmpstr->length = ntohl(var);
487
488 memcpy(&var, charptr, sizeof(uint32_t));
489 charptr += sizeof(uint32_t);
490 tmpstr->written = ntohl(var);
491
492 Malloc(tmpstr->data, char, tmpstr->length);
493 __n_assert(tmpstr->data, return NULL);
494
495 memcpy(tmpstr->data, charptr, tmpstr->written);
496 charptr += tmpstr->written;
497
498 add_nstrptr_to_msg(generated_msg, tmpstr);
499 }
500 }
501 return generated_msg;
502} /* make_msg_from_str(...) */
503
504#ifndef NO_NETMSG_API
505
514N_STR* netmsg_make_ping(int type, int id_from, int id_to, int time) {
515 NETW_MSG* msg = NULL;
516
517 N_STR* tmpstr = NULL;
518
519 create_msg(&msg);
520
521 __n_assert(msg, return NULL);
522
523 if (add_int_to_msg(msg, type) == FALSE) {
524 delete_msg(&msg);
525 return NULL;
526 }
527 if (add_int_to_msg(msg, id_from) == FALSE) {
528 delete_msg(&msg);
529 return NULL;
530 }
531 if (add_int_to_msg(msg, id_to) == FALSE) {
532 delete_msg(&msg);
533 return NULL;
534 }
535 if (add_int_to_msg(msg, time) == FALSE) {
536 delete_msg(&msg);
537 return NULL;
538 }
539
540 tmpstr = make_str_from_msg(msg);
541 delete_msg(&msg);
542
543 return tmpstr;
544} /* netmsg_make_ping() */
545
554N_STR* netmsg_make_ident(int type, int id, N_STR* name, N_STR* passwd) {
555 NETW_MSG* msg = NULL;
556 N_STR* tmpstr = NULL;
557
558 create_msg(&msg);
559 __n_assert(msg, return FALSE);
560
561 if (add_int_to_msg(msg, type) == FALSE) {
562 delete_msg(&msg);
563 return NULL;
564 }
565 if (add_int_to_msg(msg, id) == FALSE) {
566 delete_msg(&msg);
567 return NULL;
568 }
569
570 if (add_nstrdup_to_msg(msg, name) == FALSE) {
571 delete_msg(&msg);
572 return NULL;
573 }
574 if (add_nstrdup_to_msg(msg, passwd) == FALSE) {
575 delete_msg(&msg);
576 return NULL;
577 }
578
579 tmpstr = make_str_from_msg(msg);
580 delete_msg(&msg);
581
582 return tmpstr;
583} /* netmsg_make_ident() */
584
597N_STR* netmsg_make_position_msg(int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp) {
598 NETW_MSG* msg = NULL;
599 N_STR* tmpstr = NULL;
600
601 create_msg(&msg);
602
603 __n_assert(msg, return NULL);
604
605 if (add_int_to_msg(msg, NETMSG_POSITION) == FALSE) {
606 delete_msg(&msg);
607 return NULL;
608 }
609 if (add_int_to_msg(msg, id) == FALSE) {
610 delete_msg(&msg);
611 return NULL;
612 }
613 if (add_nb_to_msg(msg, X) == FALSE) {
614 delete_msg(&msg);
615 return NULL;
616 }
617 if (add_nb_to_msg(msg, Y) == FALSE) {
618 delete_msg(&msg);
619 return NULL;
620 }
621 if (add_nb_to_msg(msg, vx) == FALSE) {
622 delete_msg(&msg);
623 return NULL;
624 }
625 if (add_nb_to_msg(msg, vy) == FALSE) {
626 delete_msg(&msg);
627 return NULL;
628 }
629 if (add_nb_to_msg(msg, acc_x) == FALSE) {
630 delete_msg(&msg);
631 return NULL;
632 }
633 if (add_nb_to_msg(msg, acc_y) == FALSE) {
634 delete_msg(&msg);
635 return NULL;
636 }
637 if (add_int_to_msg(msg, time_stamp) == FALSE) {
638 delete_msg(&msg);
639 return NULL;
640 }
641
642 tmpstr = make_str_from_msg(msg);
643 delete_msg(&msg);
644 return tmpstr;
645} /* netmsg_make_position_msg() */
646
657N_STR* netmsg_make_string_msg(int id_from, int id_to, N_STR* name, N_STR* chan, N_STR* txt, int color) {
658 NETW_MSG* msg = NULL;
659
660 N_STR* tmpstr = NULL;
661
662 create_msg(&msg);
663
664 __n_assert(msg, return NULL);
665
666 if (add_int_to_msg(msg, NETMSG_STRING) == FALSE) {
667 delete_msg(&msg);
668 return NULL;
669 }
670 if (add_int_to_msg(msg, id_from) == FALSE) {
671 delete_msg(&msg);
672 return NULL;
673 }
674 if (add_int_to_msg(msg, id_to) == FALSE) {
675 delete_msg(&msg);
676 return NULL;
677 }
678 if (add_int_to_msg(msg, color) == FALSE) {
679 delete_msg(&msg);
680 return NULL;
681 }
682
683 if (add_nstrdup_to_msg(msg, name) == FALSE) {
684 delete_msg(&msg);
685 return NULL;
686 }
687 if (add_nstrdup_to_msg(msg, chan) == FALSE) {
688 delete_msg(&msg);
689 return NULL;
690 }
691 if (add_nstrdup_to_msg(msg, txt) == FALSE) {
692 delete_msg(&msg);
693 return NULL;
694 }
695
696 tmpstr = make_str_from_msg(msg);
697 delete_msg(&msg);
698 __n_assert(tmpstr, return NULL);
699
700 return tmpstr;
701
702} /* netmsg_make_string_msg */
703
709 NETW_MSG* msg = NULL;
710 N_STR* tmpstr = NULL;
711
712 create_msg(&msg);
713 __n_assert(msg, return FALSE);
714
715 if (add_int_to_msg(msg, NETMSG_QUIT) == FALSE) {
716 delete_msg(&msg);
717 return NULL;
718 }
719 tmpstr = make_str_from_msg(msg);
720 delete_msg(&msg);
721 __n_assert(tmpstr, return NULL);
722
723 return tmpstr;
724} /* netmsg_make_quit_msg() */
725
732 int32_t tmpnb = 0;
733 char* charptr = NULL;
734
735 __n_assert(msg, return FALSE);
736
737 charptr = msg->data;
738
739 /* here we bypass the number of int, numebr of flt, number of str, (4+4+4) to get the
740 * first number which should be type */
741 charptr += 3 * sizeof(uint32_t);
742 memcpy(&tmpnb, charptr, sizeof(uint32_t));
743 tmpnb = (int32_t)ntohl((uint32_t)tmpnb);
744
745 return tmpnb;
746} /* netw_msg_get_type(...) */
747
757int netw_get_ident(N_STR* msg, int* type, int* ident, N_STR** name, N_STR** passwd) {
758 NETW_MSG* netmsg = NULL;
759
760 __n_assert(msg, return FALSE);
761
762 netmsg = make_msg_from_str(msg);
763
764 __n_assert(netmsg, return FALSE);
765
766 get_int_from_msg(netmsg, &(*type));
767 get_int_from_msg(netmsg, &(*ident));
768
769 if ((*type) != NETMSG_IDENT_REQUEST && (*type) != NETMSG_IDENT_REPLY_OK && (*type) != NETMSG_IDENT_REPLY_NOK) {
770 n_log(LOG_ERR, "unknow (*type) %d", (*type));
771 delete_msg(&netmsg);
772 return FALSE;
773 }
774
775 get_nstr_from_msg(netmsg, &(*name));
776 get_nstr_from_msg(netmsg, &(*passwd));
777
778 delete_msg(&netmsg);
779
780 return TRUE;
781} /* netw_get_ident( ... ) */
782
796int netw_get_position(N_STR* msg, int* id, double* X, double* Y, double* vx, double* vy, double* acc_x, double* acc_y, int* time_stamp) {
797 NETW_MSG* netmsg = NULL;
798
799 int type = 0;
800
801 __n_assert(msg, return FALSE);
802
803 netmsg = make_msg_from_str(msg);
804
805 __n_assert(netmsg, return FALSE);
806
807 get_int_from_msg(netmsg, &type);
808
809 if (type != NETMSG_POSITION) {
810 delete_msg(&netmsg);
811 return FALSE;
812 }
813
814 get_int_from_msg(netmsg, &(*id));
815 get_nb_from_msg(netmsg, &(*X));
816 get_nb_from_msg(netmsg, &(*Y));
817 get_nb_from_msg(netmsg, &(*vx));
818 get_nb_from_msg(netmsg, &(*vy));
819 get_nb_from_msg(netmsg, &(*acc_x));
820 get_nb_from_msg(netmsg, &(*acc_y));
821 get_int_from_msg(netmsg, &(*time_stamp));
822
823 delete_msg(&netmsg);
824
825 return TRUE;
826} /* netw_get_position( ... ) */
827
839int netw_get_string(N_STR* msg, int* id, N_STR** name, N_STR** chan, N_STR** txt, int* color) {
840 NETW_MSG* netmsg = NULL;
841
842 int type = 0;
843
844 __n_assert(msg, return FALSE);
845
846 netmsg = make_msg_from_str(msg);
847
848 __n_assert(netmsg, return FALSE);
849
850 get_int_from_msg(netmsg, &type);
851
852 if (type != NETMSG_STRING) {
853 delete_msg(&netmsg);
854 return FALSE;
855 }
856
857 get_int_from_msg(netmsg, id);
858 get_int_from_msg(netmsg, color);
859
860 get_nstr_from_msg(netmsg, &(*name));
861 get_nstr_from_msg(netmsg, &(*chan));
862 get_nstr_from_msg(netmsg, &(*txt));
863
864 delete_msg(&netmsg);
865
866 return TRUE;
867
868} /* netw_get_string( ... ) */
869
879int netw_get_ping(N_STR* msg, int* type, int* from, int* to, int* time) {
880 NETW_MSG* netmsg;
881
882 __n_assert(msg, return FALSE);
883
884 netmsg = make_msg_from_str(msg);
885
886 __n_assert(netmsg, return FALSE);
887
888 get_int_from_msg(netmsg, &(*type));
889
890 if ((*type) != NETMSG_PING_REQUEST && (*type) != NETMSG_PING_REPLY) {
891 delete_msg(&netmsg);
892 return FALSE;
893 }
894
895 get_int_from_msg(netmsg, &(*from));
896 get_int_from_msg(netmsg, &(*to));
897 get_int_from_msg(netmsg, &(*time));
898
899 delete_msg(&netmsg);
900
901 return TRUE;
902} /* netw_get_ping( ... ) */
903
910 __n_assert(msg, return FALSE);
911
912 NETW_MSG* netmsg = NULL;
913 netmsg = make_msg_from_str(msg);
914 __n_assert(netmsg, return FALSE);
915
916 int type = -1;
917 get_int_from_msg(netmsg, &type);
918 if (type != NETMSG_QUIT) {
919 delete_msg(&netmsg);
920 return FALSE;
921 }
922 delete_msg(&netmsg);
923 return TRUE;
924} /* netw_get_quit( ... ) */
925
926#endif // NO_NETMSG_API
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:258
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:242
void * ptr
void pointer to store
Definition n_list.h:26
LIST_NODE * start
pointer to the start of the list
Definition n_list.h:46
size_t nb_items
number of item currently in the list
Definition n_list.h:41
struct LIST_NODE * next
pointer to the next node
Definition n_list.h:32
#define list_shift(__LIST_, __TYPE_)
Shift macro helper for void pointer casting.
Definition n_list.h:74
int list_empty(LIST *list)
Empty a LIST list of pointers.
Definition n_list.c:471
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:199
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:518
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Definition n_list.h:55
Structure of a generic list node.
Definition n_list.h:24
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_ERR
error conditions
Definition n_log.h:56
size_t written
size of the written data inside the string
Definition n_str.h:45
char * data
the string
Definition n_str.h:41
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:43
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
Definition n_str.c:49
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
Definition n_str.c:670
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:228
A box including a string and his lenght.
Definition n_str.h:39
LIST * tabflt
array of casted double value
LIST * tabstr
array of N_STR
LIST * tabint
array of int
#define NETMSG_IDENT_REQUEST
Network Message is identification request: (int)type , (int)id , (N_STR *)name , (N_STR *)password.
double htond(double value)
If needed swap bytes for a double.
int get_str_from_msg(NETW_MSG *msg, char **value)
Get a string from a message string list.
int add_strdup_to_msg(NETW_MSG *msg, const char *str)
Add a copy of char *str to the string list in the message.
int netw_get_position(N_STR *msg, int *id, double *X, double *Y, double *vx, double *vy, double *acc_x, double *acc_y, int *time_stamp)
Retrieves position from netwmsg.
#define NETMSG_PING_REQUEST
Network Message is ping request: (int)type , (int)id_from , (int)id_to.
#define NETMSG_IDENT_REPLY_NOK
Network Message is identification reply NON OK: (int)type , (int)id , (N_STR *)name ,...
#define NETMSG_STRING
Network Message is string: (int)type , (int)id , (N_STR *)name , (N_STR *)chan , (N_STR *)text ,...
#define NETMSG_PING_REPLY
Network Message is ping reply: (int)type , (int)id_from , (int)id_to.
NETW_MSG * make_msg_from_str(N_STR *str)
Make a single message of the string.
int empty_msg(NETW_MSG **msg)
Empty a NETW_MSG *object.
double double_swap(double value)
Swap bytes endiannes for a double.
N_STR * make_str_from_msg(NETW_MSG *msg)
Make a single string of the message.
int netw_get_ping(N_STR *msg, int *type, int *from, int *to, int *time)
Retrieves a ping travel elapsed time.
int add_nstrptr_to_msg(NETW_MSG *msg, N_STR *str)
Add a string to the string list in the message.
int get_nb_from_msg(NETW_MSG *msg, double *value)
Get a number from a message number list.
#define NETMSG_QUIT
Network asking for exit.
int netw_get_quit(N_STR *msg)
get a formatted NETWMSG_QUIT message from the specified network
int create_msg(NETW_MSG **msg)
Create a NETW_MSG *object.
#define NETMSG_POSITION
Network Message position: (int)type , (int)id , (int)X , (int)Y , (int)x_shift , (int)y_shift ,...
N_STR * netmsg_make_position_msg(int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp)
make a network NETMSG_POSITION message with given parameters
int netw_msg_get_type(N_STR *msg)
Get the type of message without killing the first number. Use with netw_get_XXX.
int get_nstr_from_msg(NETW_MSG *msg, N_STR **value)
Get a string from a message string list.
N_STR * netmsg_make_ident(int type, int id, N_STR *name, N_STR *passwd)
Add a formatted NETWMSG_IDENT message to the specified network.
int add_nstrdup_to_msg(NETW_MSG *msg, N_STR *str)
Add a copy of str to the string list in the message.
int add_int_to_msg(NETW_MSG *msg, int value)
Add an int to the int list int the message.
double ntohd(double value)
If needed swap bytes for a double.
#define NETMSG_IDENT_REPLY_OK
Network Message is identification reply OK : (int)type , (int)id , (N_STR *)name ,...
int netw_get_string(N_STR *msg, int *id, N_STR **name, N_STR **chan, N_STR **txt, int *color)
Retrieves string from netwmsg.
int delete_msg(NETW_MSG **msg)
Delete a NETW_MSG *object.
int add_nb_to_msg(NETW_MSG *msg, double value)
Add an float to the message.
N_STR * netmsg_make_quit_msg(void)
make a generic network NETMSG_QUIT message
N_STR * netmsg_make_ping(int type, int id_from, int id_to, int time)
Make a ping message to send to a network.
int netw_get_ident(N_STR *msg, int *type, int *ident, N_STR **name, N_STR **passwd)
Retrieves identification from netwmsg.
N_STR * netmsg_make_string_msg(int id_from, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color)
make a network NETMSG_STRING message with given parameters
int get_int_from_msg(NETW_MSG *msg, int *value)
Get a number from a message number list.
network message, array of char and int
Common headers and low-level functions & define.
Generic log system.
Network Engine.
Network messages , serialization tools.