From 3dd3033273c5d52fd1150a3ebf74c5828271306d Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Wed, 21 Dec 2016 02:56:52 +0100 Subject: [PATCH] msg format: fonctionne sur les programmes de test (core-test/app/communication-*) --- core-test/app/Makefile | 2 +- core-test/app/communication-client.c | 32 +++++++++------------- core-test/app/communication-server.c | 38 ++++++++++++++------------ core/communication.c | 7 ----- core/msg.c | 41 ++++++++++++++++++++++------ core/msg.h | 3 +- core/usocket.c | 9 +++--- 7 files changed, 74 insertions(+), 58 deletions(-) diff --git a/core-test/app/Makefile b/core-test/app/Makefile index 6972c25..8ea2a9e 100644 --- a/core-test/app/Makefile +++ b/core-test/app/Makefile @@ -10,7 +10,7 @@ TESTS=$(addsuffix .test, $(EXEC)) all: $(SOURCES) $(EXEC) $(EXEC): $(OBJECTS) $(CFILES) - $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin .c.o: $(CC) -c $(CFLAGS) $< -o $@ diff --git a/core-test/app/communication-client.c b/core-test/app/communication-client.c index 60ca591..348a321 100644 --- a/core-test/app/communication-client.c +++ b/core-test/app/communication-client.c @@ -2,28 +2,20 @@ #include #include +#include "../../core/msg.h" +#include "../../core/error.h" #include "../../core/communication.h" -#define handle_err(fun,msg)\ - fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); - #define MSG "coucou" #define SERVICE_NAME "test" int main (int argc, char *argv[], char *env[]) { - size_t msize = BUFSIZ; - char *buf = NULL; - - if ( (buf = malloc (BUFSIZ)) == NULL) { - handle_err ("main", "malloc"); - return EXIT_FAILURE; - } - memset (buf, 0, BUFSIZ); - + struct msg m; + memset (&m, 0, sizeof (struct msg)); struct service srv; - memset(&srv, 0, sizeof (struct service)); + memset (&srv, 0, sizeof (struct service)); // index and version should be filled srv.index = 0; @@ -36,25 +28,27 @@ int main (int argc, char *argv[], char *env[]) } printf ("msg to send: %s\n", MSG); - if (app_write (&srv, MSG, strlen(MSG)) < 0) { + msg_format_data (&m, MSG, strlen(MSG) +1); + printf ("msg to send in the client: "); + print_msg (&m); + if (app_write (&srv, &m) < 0) { handle_err("main", "app_write < 0"); return EXIT_FAILURE; } + msg_free (&m); - if (app_read (&srv, &buf, &msize) < 0) { + if (app_read (&srv, &m) < 0) { handle_err("main", "app_read < 0"); return EXIT_FAILURE; } - printf ("msg recv: %s\n", buf); + printf ("msg recv: %s\n", m.val); + msg_free (&m); if (app_close (&srv) < 0) { handle_err("main", "app_close < 0"); return EXIT_FAILURE; } - if (buf != NULL) - free (buf); - return EXIT_SUCCESS; } diff --git a/core-test/app/communication-server.c b/core-test/app/communication-server.c index 761e329..28659c9 100644 --- a/core-test/app/communication-server.c +++ b/core-test/app/communication-server.c @@ -2,26 +2,17 @@ #include #include +#include "../../core/error.h" #include "../../core/communication.h" -#define handle_err(fun,msg)\ - fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); - #define MSG "coucou" #define SERVICE_NAME "test" int main (int argc, char *argv[], char *env[]) { - size_t msize = BUFSIZ; - char *buf = NULL; - - if ( (buf = malloc (BUFSIZ)) == NULL) { - handle_err ("main", "malloc"); - return EXIT_FAILURE; - } - memset (buf, 0, BUFSIZ); - + struct msg m; + memset (&m, 0, sizeof (struct msg)); struct service srv; memset(&srv, 0, sizeof (struct service)); @@ -43,17 +34,31 @@ int main (int argc, char *argv[], char *env[]) return EXIT_FAILURE; } - if (srv_read (&p, &buf, &msize) < 0) { + if (srv_read (&p, &m) < 0) { handle_err("main", "srv_read < 0"); return EXIT_FAILURE; } - printf ("msg recv: %s\n", buf); + printf ("msg recv: %s\n", m.val); - if (srv_write (&p, buf, msize) < 0) { + if (srv_write (&p, &m) < 0) { handle_err("main", "srv_write < 0"); return EXIT_FAILURE; } + msg_free (&m); + + // client quits + if (srv_read (&p, &m) < 0) { + handle_err("main", "srv_read < 0"); + return EXIT_FAILURE; + } + if (m.type == MSG_TYPE_DIS) { + printf ("the client quits\n"); + } + else { + fprintf (stderr, "err: should have received the client dis msg\n"); + } + msg_free (&m); if (srv_close_proc (&p) < 0) { handle_err("main", "srv_close_proc < 0"); @@ -65,8 +70,5 @@ int main (int argc, char *argv[], char *env[]) return EXIT_FAILURE; } - if (buf != NULL) - free (buf); - return EXIT_SUCCESS; } diff --git a/core/communication.c b/core/communication.c index 6711978..7f384b1 100644 --- a/core/communication.c +++ b/core/communication.c @@ -69,13 +69,6 @@ int srv_close (struct service *srv) int srv_close_proc (struct process *p) { - struct msg m; - memset (&m, 0, sizeof (struct msg)); - m.type = MSG_TYPE_DIS; - if (msg_write (p->proc_fd, &m) < 0) { - handle_err ("srv_close_proc", "msg_write < 0"); - } - return usock_close (p->proc_fd); } diff --git a/core/msg.c b/core/msg.c index 98eb9ed..5d82d01 100644 --- a/core/msg.c +++ b/core/msg.c @@ -4,12 +4,16 @@ #include +void print_msg (const struct msg *m) +{ + assert (m != NULL); + printf ("msg: type %d len %d\n", m->type, m->valsize); +} + int msg_format_read (struct msg *m, const char *buf, size_t msize) { assert (m != NULL); assert (buf != NULL); - assert (msize >= 3); - assert (msize <= BUFSIZ - 3); if (m == NULL) @@ -19,11 +23,13 @@ int msg_format_read (struct msg *m, const char *buf, size_t msize) memcpy (&m->valsize, buf+1, 2); assert (m->valsize <= BUFSIZ -3); + // printf ("type %d : msize = %ld, valsize = %d\n", m->type, msize, m->valsize); + assert (m->valsize == msize - 3); if (m->val != NULL) free (m->val), m->val = NULL; - if (m->val == NULL) { + if (m->val == NULL && m->valsize > 0) { m->val = malloc (m->valsize); memcpy (m->val, buf+3, m->valsize); } @@ -31,7 +37,7 @@ int msg_format_read (struct msg *m, const char *buf, size_t msize) return 0; } -int msg_format_write (struct msg *m, char **buf, size_t *msize) +int msg_format_write (const struct msg *m, char **buf, size_t *msize) { assert (m != NULL); assert (buf != NULL); @@ -47,11 +53,17 @@ int msg_format_write (struct msg *m, char **buf, size_t *msize) if (msize == NULL) return -3; + if (*buf == NULL) { + *buf = malloc (3 + m->valsize); + } + char *buffer = *buf; buffer[0] = m->type; memcpy (buffer + 1, &m->valsize, 2); - memcpy (buffer + 3, &m->val, (m->valsize <= BUFSIZ) ? m->valsize : BUFSIZ); + memcpy (buffer + 3, m->val, m->valsize); + + *msize = 3 + m->valsize; return 0; } @@ -61,7 +73,7 @@ int msg_read (int fd, struct msg *m) assert (m != NULL); char *buf = NULL; - size_t msize = 0; + size_t msize = BUFSIZ; int ret = usock_recv (fd, &buf, &msize); if (ret < 0) { @@ -74,6 +86,9 @@ int msg_read (int fd, struct msg *m) } free (buf); + // printf ("msg received: "); + // print_msg (m); + return 0; } @@ -81,10 +96,14 @@ int msg_write (int fd, const struct msg *m) { assert (m != NULL); + // printf ("msg to write: "); + // print_msg (m); + char *buf = NULL; size_t msize = 0; msg_format_write (m, &buf, &msize); + // printf ("msg to send, real size %ld\n", msize); int ret = usock_send (fd, buf, msize); if (ret < 0) { handle_err ("msg_write", "usock_send"); @@ -103,7 +122,7 @@ int msg_format (struct msg *m, char type, const char *val, size_t valsize) { assert (m != NULL); assert (valsize + 3 <= BUFSIZ); - assert (valsize == 0 && val == NULL || valsize > 0 && val != NULL); + assert ((valsize == 0 && val == NULL) || (valsize > 0 && val != NULL)); if (valsize + 3 > BUFSIZ) { handle_err ("msg_format_con", "msgsize > BUFSIZ"); @@ -112,6 +131,10 @@ int msg_format (struct msg *m, char type, const char *val, size_t valsize) m->type = type; m->valsize = (short) valsize; + + if (m->val == NULL) + m->val = malloc (valsize); + memcpy (m->val, val, valsize); return 0; } @@ -139,7 +162,9 @@ int msg_free (struct msg *m) return -1; if (m->val != NULL) - free (m->val); + free (m->val), m->val = NULL; + + m->valsize = 0; return 0; } diff --git a/core/msg.h b/core/msg.h index c2ac282..da809e0 100644 --- a/core/msg.h +++ b/core/msg.h @@ -20,7 +20,7 @@ struct msg { // used to create msg structure from buffer int msg_format_read (struct msg *m, const char *buf, size_t msize); // used to create buffer from msg structure -int msg_format_write (struct msg *m, char **buf, size_t *msize); +int msg_format_write (const struct msg *m, char **buf, size_t *msize); // read a structure msg from fd int msg_read (int fd, struct msg *m); @@ -32,5 +32,6 @@ int msg_format_data (struct msg *m, const char *val, size_t valsize); int msg_format_ack (struct msg *m, const char *val, size_t valsize); int msg_free (struct msg *m); +void print_msg (const struct msg *m); #endif diff --git a/core/usocket.c b/core/usocket.c index 57a8b0c..4a6a12b 100644 --- a/core/usocket.c +++ b/core/usocket.c @@ -1,18 +1,16 @@ #include "usocket.h" #include "utils.h" +#include "error.h" + #include #include #include #include #include -#define handle_err(fun,msg)\ - fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); - int usock_send (const int fd, const char *buf, const int msize) { print_hexa ("msg send", (unsigned char *)buf, msize); - printf ("msg to send: (%d) \n", msize); fflush(stdout); int ret = 0; @@ -42,6 +40,8 @@ int usock_recv (const int fd, char **buf, size_t *msize) // do not allocate too much memory if (*msize > BUFSIZ) handle_err ("usock_recv", "msize > BUFSIZ"); + if (*msize == 0) + *msize = BUFSIZ; *buf = malloc ((*msize < BUFSIZ) ? *msize : BUFSIZ); } @@ -54,6 +54,7 @@ int usock_recv (const int fd, char **buf, size_t *msize) } *msize = (size_t) ret; print_hexa ("msg recv", (unsigned char *)*buf, *msize); + fflush(stdout); return ret; }