59 const unsigned char* src = (
const unsigned char*)&value;
60 unsigned char* dst = (
unsigned char*)&swaped;
82#if (BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN)
97#if (BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN)
112 n_log(
LOG_ERR,
"create_msg destination is valid and should be NULL !");
138 if ((*msg)->tabint) {
141 if ((*msg)->tabflt) {
144 if ((*msg)->tabstr) {
179 double* new_val = NULL;
183 Malloc(new_val,
double, 1);
288 if (len > 0) memcpy(blob->
data, bytes, len);
289 blob->
data[len] =
'\0';
354 n_log(
LOG_ERR,
"Previous pointer value %p overriden by pointer %p", (*value), val);
376 n_log(
LOG_ERR,
"Previous pointer value %p overriden by pointer %p", (*value), val);
379 (*value) = val->
data;
402 if (!val)
return FALSE;
414 *out_bytes = val->
data;
427 size_t str_length = 0;
430 N_STR *strptr = NULL,
431 *generated_str = NULL;
433 char* charptr = NULL;
446 str_length +=
sizeof(int32_t) +
sizeof(int32_t) + strptr->
written;
453 Malloc(generated_str->data,
char, str_length + 1);
454 __n_assert(generated_str->data,
Free(generated_str);
return NULL);
456 generated_str->length = str_length + 1;
457 generated_str->written = str_length;
460 charptr = generated_str->data;
464 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in int list of message %p", msg);
469 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in float list of message %p", msg);
474 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in string list of message %p", msg);
485 memcpy(charptr, &nb_int_items,
sizeof(uint32_t));
486 charptr +=
sizeof(uint32_t);
488 memcpy(charptr, &nb_float_items,
sizeof(uint32_t));
489 charptr +=
sizeof(uint32_t);
491 memcpy(charptr, &nb_str_items,
sizeof(uint32_t));
492 charptr +=
sizeof(uint32_t);
497 uint32_t* nbptr = (uint32_t*)node->
ptr;
498 uint32_t val = htonl(nbptr[0]);
499 memcpy(charptr, &val,
sizeof(uint32_t));
500 charptr +=
sizeof(uint32_t);
507 const double* nbptr = (
const double*)node->
ptr;
508 double val =
htond(nbptr[0]);
509 memcpy(charptr, &val,
sizeof(
double));
510 charptr +=
sizeof(double);
519 if (strptr->
length >= UINT32_MAX) {
520 n_log(
LOG_ERR,
"string too long (>=UINT32_MAX) in string list of message %p", msg);
524 uint32_t var = htonl((uint32_t)strptr->
length);
525 memcpy(charptr, &var,
sizeof(uint32_t));
526 charptr +=
sizeof(uint32_t);
528 if (strptr->
written >= UINT32_MAX) {
529 n_log(
LOG_ERR,
"string too long (>=UINT32_MAX) in string list of message %p", msg);
533 var = htonl((uint32_t)strptr->
written);
534 memcpy(charptr, &var,
sizeof(uint32_t));
535 charptr +=
sizeof(uint32_t);
543 return generated_str;
553 N_STR* tmpstr = NULL;
555 char* charptr = NULL;
570 if (str->
written < 3 *
sizeof(uint32_t)) {
571 n_log(
LOG_ERR,
"message too short: %zu bytes, need at least %zu", str->
written, 3 *
sizeof(uint32_t));
579 memcpy(&nb_int, charptr,
sizeof(uint32_t));
580 charptr +=
sizeof(uint32_t);
581 nb_int = ntohl(nb_int);
583 memcpy(&nb_flt, charptr,
sizeof(uint32_t));
584 charptr +=
sizeof(uint32_t);
585 nb_flt = ntohl(nb_flt);
587 memcpy(&nb_str, charptr,
sizeof(uint32_t));
588 charptr +=
sizeof(uint32_t);
589 nb_str = ntohl(nb_str);
593 size_t remaining = str->
written - 3 *
sizeof(uint32_t);
595 if (nb_int > remaining /
sizeof(uint32_t)) {
596 n_log(
LOG_ERR,
"message claims %u ints but only %zu bytes remain", nb_int, remaining);
600 remaining -= (size_t)nb_int *
sizeof(uint32_t);
602 if (nb_flt > remaining /
sizeof(
double)) {
603 n_log(
LOG_ERR,
"message claims %u floats but only %zu bytes remain", nb_flt, remaining);
607 remaining -= (size_t)nb_flt *
sizeof(
double);
610 for (it = 0; it < nb_int; it++) {
611 memcpy(&tmpnb, charptr,
sizeof(uint32_t));
612 tmpnb = (int32_t)ntohl((uint32_t)tmpnb);
613 charptr +=
sizeof(uint32_t);
615 n_log(
LOG_ERR,
"failed to add int %u/%u to message", it, nb_int);
623 for (it = 0; it < nb_flt; it++) {
624 memcpy(&tmpflt, charptr,
sizeof(
double));
625 tmpflt =
ntohd(tmpflt);
626 charptr +=
sizeof(double);
628 n_log(
LOG_ERR,
"failed to add float %u/%u to message", it, nb_flt);
637 if ((
size_t)nb_str > remaining / (2 *
sizeof(uint32_t))) {
638 n_log(
LOG_ERR,
"message claims %u strings but only %zu bytes remain (need at least %zu for headers)",
639 nb_str, remaining, (
size_t)nb_str * 2 *
sizeof(uint32_t));
644 for (it = 0; it < nb_str; it++) {
646 if (remaining < 2 *
sizeof(uint32_t)) {
647 n_log(
LOG_ERR,
"string %u/%u: not enough data for string headers (%zu remaining)", it, nb_str, remaining);
656 memcpy(&var, charptr,
sizeof(uint32_t));
657 charptr +=
sizeof(uint32_t);
658 tmpstr->
length = ntohl(var);
660 memcpy(&var, charptr,
sizeof(uint32_t));
661 charptr +=
sizeof(uint32_t);
663 remaining -= 2 *
sizeof(uint32_t);
667 n_log(
LOG_ERR,
"string %u/%u: written (%zu) >= length (%zu), violates N_STR invariant", it, nb_str, tmpstr->
written, tmpstr->
length);
674 if (tmpstr->
written > remaining) {
675 n_log(
LOG_ERR,
"string %u/%u: written size %zu exceeds remaining %zu bytes", it, nb_str, tmpstr->
written, remaining);
692 __n_assert(tmpstr->
data, { Free(tmpstr); delete_msg(&generated_msg); return NULL; });
708 return generated_msg;
724 N_STR* tmpstr = NULL;
763 N_STR* tmpstr = NULL;
806 N_STR* tmpstr = NULL;
867 N_STR* tmpstr = NULL;
917 N_STR* tmpstr = NULL;
941 char* charptr = NULL;
947 if (msg->
written < 4 *
sizeof(uint32_t))
return -1;
952 memcpy(&nb_int, charptr,
sizeof(uint32_t));
953 nb_int = ntohl(nb_int);
956 n_log(
LOG_ERR,
"netw_msg_get_type: message has no int fields (nb_int=%u), cannot read type", nb_int);
961 charptr += 3 *
sizeof(uint32_t);
962 memcpy(&tmpnb, charptr,
sizeof(uint32_t));
963 tmpnb = (int32_t)ntohl((uint32_t)tmpnb);
988 n_log(
LOG_ERR,
"failed to extract type/ident from ident message");
1001 n_log(
LOG_ERR,
"failed to extract name/passwd from ident message");
1026int 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) {
1038 n_log(
LOG_ERR,
"failed to extract type from position message");
1056 n_log(
LOG_ERR,
"failed to extract fields from position message");
1089 n_log(
LOG_ERR,
"failed to extract type from string message");
1103 n_log(
LOG_ERR,
"failed to extract int fields from string message");
1111 n_log(
LOG_ERR,
"failed to extract string fields from string message");
1144 n_log(
LOG_ERR,
"failed to extract type from ping message");
1157 n_log(
LOG_ERR,
"failed to extract fields from ping message");
#define FreeNoLog(__ptr)
Free Handler without log.
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define Free(__ptr)
Free Handler to get errors.
void * ptr
void pointer to store
LIST_NODE * start
pointer to the start of the list
size_t nb_items
number of item currently in the list
struct LIST_NODE * next
pointer to the next node
#define list_shift(__LIST_, __TYPE_)
Shift macro helper for void pointer casting.
int list_empty(LIST *list)
Empty a LIST list of pointers.
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
int list_destroy(LIST **list)
Empty and Free a list container.
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Structure of a generic list node.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
size_t length
total allocation (in bytes) of the data buffer, padding included
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
A box including a string and his lenght.
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.
void netw_msg_stats_get(long long *created, long long *deleted)
Read lifecycle counters.
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.
void netw_bytes_stats_get(long long *sent, long long *recv)
Read process-wide network byte counters, sum of N_STR.written across every netw_add_msg / netw_get_ms...
int add_bytes_to_msg(NETW_MSG *msg, const void *bytes, size_t len)
Add a binary blob to the message (clean alias for a raw N_STR payload, distinguishes binary data from...
#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.
#define NETW_MSG_MAX_STRING_LENGTH
Maximum allowed length for a single string inside a network message (default: 1 GB).
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.
int get_bytes_from_msg(NETW_MSG *msg, void **out_bytes, size_t *out_len)
Get a binary blob from the message (counterpart of add_bytes_to_msg).
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 an int from a message int list.
network message, array of char and int
Common headers and low-level functions & define.
static long long g_netw_msg_created
long long g_netw_bytes_sent
Add a message to send in aimed NETWORK.
static long long g_netw_msg_deleted
long long g_netw_bytes_recv
Network messages , serialization tools.