60 const unsigned char* src = (
const unsigned char*)&value;
61 unsigned char* dst = (
unsigned char*)&swaped;
83#if (BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN)
98#if (BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN)
113 n_log(
LOG_ERR,
"create_msg destination is valid and should be NULL !");
139 if ((*msg)->tabint) {
142 if ((*msg)->tabflt) {
145 if ((*msg)->tabstr) {
180 double* new_val = NULL;
184 Malloc(new_val,
double, 1);
289 if (len > 0) memcpy(blob->
data, bytes, len);
290 blob->
data[len] =
'\0';
355 n_log(
LOG_ERR,
"Previous pointer value %p overriden by pointer %p", (*value), val);
377 n_log(
LOG_ERR,
"Previous pointer value %p overriden by pointer %p", (*value), val);
380 (*value) = val->
data;
403 if (!val)
return FALSE;
415 *out_bytes = val->
data;
428 size_t str_length = 0;
431 N_STR *strptr = NULL,
432 *generated_str = NULL;
434 char* charptr = NULL;
447 str_length +=
sizeof(int32_t) +
sizeof(int32_t) + strptr->
written;
454 Malloc(generated_str->data,
char, str_length + 1);
455 __n_assert(generated_str->data,
Free(generated_str);
return NULL);
457 generated_str->length = str_length + 1;
458 generated_str->written = str_length;
461 charptr = generated_str->data;
465 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in int list of message %p", msg);
470 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in float list of message %p", msg);
475 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in string list of message %p", msg);
486 memcpy(charptr, &nb_int_items,
sizeof(uint32_t));
487 charptr +=
sizeof(uint32_t);
489 memcpy(charptr, &nb_float_items,
sizeof(uint32_t));
490 charptr +=
sizeof(uint32_t);
492 memcpy(charptr, &nb_str_items,
sizeof(uint32_t));
493 charptr +=
sizeof(uint32_t);
498 uint32_t* nbptr = (uint32_t*)node->
ptr;
499 uint32_t val = htonl(nbptr[0]);
500 memcpy(charptr, &val,
sizeof(uint32_t));
501 charptr +=
sizeof(uint32_t);
508 const double* nbptr = (
const double*)node->
ptr;
509 double val =
htond(nbptr[0]);
510 memcpy(charptr, &val,
sizeof(
double));
511 charptr +=
sizeof(double);
520 if (strptr->
length >= UINT32_MAX) {
521 n_log(
LOG_ERR,
"string too long (>=UINT32_MAX) in string list of message %p", msg);
525 uint32_t var = htonl((uint32_t)strptr->
length);
526 memcpy(charptr, &var,
sizeof(uint32_t));
527 charptr +=
sizeof(uint32_t);
529 if (strptr->
written >= UINT32_MAX) {
530 n_log(
LOG_ERR,
"string too long (>=UINT32_MAX) in string list of message %p", msg);
534 var = htonl((uint32_t)strptr->
written);
535 memcpy(charptr, &var,
sizeof(uint32_t));
536 charptr +=
sizeof(uint32_t);
544 return generated_str;
554 N_STR* tmpstr = NULL;
556 char* charptr = NULL;
571 if (str->
written < 3 *
sizeof(uint32_t)) {
572 n_log(
LOG_ERR,
"message too short: %zu bytes, need at least %zu", str->
written, 3 *
sizeof(uint32_t));
580 memcpy(&nb_int, charptr,
sizeof(uint32_t));
581 charptr +=
sizeof(uint32_t);
582 nb_int = ntohl(nb_int);
584 memcpy(&nb_flt, charptr,
sizeof(uint32_t));
585 charptr +=
sizeof(uint32_t);
586 nb_flt = ntohl(nb_flt);
588 memcpy(&nb_str, charptr,
sizeof(uint32_t));
589 charptr +=
sizeof(uint32_t);
590 nb_str = ntohl(nb_str);
594 size_t remaining = str->
written - 3 *
sizeof(uint32_t);
596 if (nb_int > remaining /
sizeof(uint32_t)) {
597 n_log(
LOG_ERR,
"message claims %u ints but only %zu bytes remain", nb_int, remaining);
601 remaining -= (size_t)nb_int *
sizeof(uint32_t);
603 if (nb_flt > remaining /
sizeof(
double)) {
604 n_log(
LOG_ERR,
"message claims %u floats but only %zu bytes remain", nb_flt, remaining);
608 remaining -= (size_t)nb_flt *
sizeof(
double);
611 for (it = 0; it < nb_int; it++) {
612 memcpy(&tmpnb, charptr,
sizeof(uint32_t));
613 tmpnb = (int32_t)ntohl((uint32_t)tmpnb);
614 charptr +=
sizeof(uint32_t);
616 n_log(
LOG_ERR,
"failed to add int %u/%u to message", it, nb_int);
624 for (it = 0; it < nb_flt; it++) {
625 memcpy(&tmpflt, charptr,
sizeof(
double));
626 tmpflt =
ntohd(tmpflt);
627 charptr +=
sizeof(double);
629 n_log(
LOG_ERR,
"failed to add float %u/%u to message", it, nb_flt);
638 if ((
size_t)nb_str > remaining / (2 *
sizeof(uint32_t))) {
639 n_log(
LOG_ERR,
"message claims %u strings but only %zu bytes remain (need at least %zu for headers)",
640 nb_str, remaining, (
size_t)nb_str * 2 *
sizeof(uint32_t));
645 for (it = 0; it < nb_str; it++) {
647 if (remaining < 2 *
sizeof(uint32_t)) {
648 n_log(
LOG_ERR,
"string %u/%u: not enough data for string headers (%zu remaining)", it, nb_str, remaining);
657 memcpy(&var, charptr,
sizeof(uint32_t));
658 charptr +=
sizeof(uint32_t);
659 tmpstr->
length = ntohl(var);
661 memcpy(&var, charptr,
sizeof(uint32_t));
662 charptr +=
sizeof(uint32_t);
664 remaining -= 2 *
sizeof(uint32_t);
668 n_log(
LOG_ERR,
"string %u/%u: written (%zu) >= length (%zu), violates N_STR invariant", it, nb_str, tmpstr->
written, tmpstr->
length);
675 if (tmpstr->
written > remaining) {
676 n_log(
LOG_ERR,
"string %u/%u: written size %zu exceeds remaining %zu bytes", it, nb_str, tmpstr->
written, remaining);
693 __n_assert(tmpstr->
data, { Free(tmpstr); delete_msg(&generated_msg); return NULL; });
709 return generated_msg;
725 N_STR* tmpstr = NULL;
764 N_STR* tmpstr = NULL;
807 N_STR* tmpstr = NULL;
868 N_STR* tmpstr = NULL;
918 N_STR* tmpstr = NULL;
942 char* charptr = NULL;
948 if (msg->
written < 4 *
sizeof(uint32_t))
return -1;
953 memcpy(&nb_int, charptr,
sizeof(uint32_t));
954 nb_int = ntohl(nb_int);
957 n_log(
LOG_ERR,
"netw_msg_get_type: message has no int fields (nb_int=%u), cannot read type", nb_int);
962 charptr += 3 *
sizeof(uint32_t);
963 memcpy(&tmpnb, charptr,
sizeof(uint32_t));
964 tmpnb = (int32_t)ntohl((uint32_t)tmpnb);
989 n_log(
LOG_ERR,
"failed to extract type/ident from ident message");
1002 n_log(
LOG_ERR,
"failed to extract name/passwd from ident message");
1027int 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) {
1039 n_log(
LOG_ERR,
"failed to extract type from position message");
1057 n_log(
LOG_ERR,
"failed to extract fields from position message");
1090 n_log(
LOG_ERR,
"failed to extract type from string message");
1104 n_log(
LOG_ERR,
"failed to extract int fields from string message");
1112 n_log(
LOG_ERR,
"failed to extract string fields from string message");
1145 n_log(
LOG_ERR,
"failed to extract type from ping message");
1158 n_log(
LOG_ERR,
"failed to extract fields from ping message");
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
#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.
long long g_netw_bytes_sent
Add a message to send in aimed NETWORK.
long long g_netw_bytes_recv
static long long g_netw_msg_created
static long long g_netw_msg_deleted
Network messages , serialization tools.