From 71db43802e00727c9b1383ef78b796327f4ce40a Mon Sep 17 00:00:00 2001 From: Karchnu Date: Wed, 1 Jul 2020 12:47:04 +0200 Subject: [PATCH] Multiple clients: ok. --- src/communication.c | 16 +++++++--- src/message.c | 32 +++++++++++++------ tests/func_01_connection_establishmentd.c | 4 +-- tests/func_02_pong.c | 2 +- tests/func_02_pongd.c | 2 +- .../func_03_multiple-communications-client.c | 2 +- .../func_03_multiple-communications-server.c | 2 +- 7 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/communication.c b/src/communication.c index b31ab88..0230ef0 100644 --- a/src/communication.c +++ b/src/communication.c @@ -249,6 +249,11 @@ struct ipc_error ipc_add ( struct ipc_ctx *ctx, struct ipc_connection_info *p, s // Memory reallocation. ipc_ctx_new_alloc (ctx); + if (ctx->size <= 0) { + printf ("ERROR: new alloc <= 0\n"); + exit(1); + } + ctx->cinfos[ctx->size - 1] = *p; ctx->pollfd[ctx->size - 1] = *pollfd; @@ -259,13 +264,12 @@ struct ipc_error ipc_del (struct ipc_ctx *ctx, uint32_t index) { T_R ((ctx == NULL), IPC_ERROR_DEL__NO_CLIENTS_PARAM); T_R ((ctx->cinfos == NULL || ctx->pollfd == NULL), IPC_ERROR_DEL__EMPTY_LIST); + T_R ((index >= ctx->size), IPC_ERROR_DEL__CANNOT_FIND_CLIENT); - if (index >= ctx->size) { - IPC_RETURN_ERROR (IPC_ERROR_DEL__CANNOT_FIND_CLIENT); - } - - if (ctx->cinfos[index].spath != NULL) + if (ctx->cinfos[index].spath != NULL) { free (ctx->cinfos[index].spath); + ctx->cinfos[index].spath = NULL; + } ctx->size--; @@ -336,6 +340,8 @@ struct ipc_error handle_writing_message (struct ipc_event *event, struct ipc_ctx ipc_message_empty (m); printf ("Removing the message\n"); ipc_messages_del (&ctx->tx, i); // remove the message indexed by i + + break; // The message has been sent } } diff --git a/src/message.c b/src/message.c index 35a238d..00aa4bd 100644 --- a/src/message.c +++ b/src/message.c @@ -165,13 +165,20 @@ struct ipc_error ipc_messages_add (struct ipc_messages *messages, const struct // DEEP COPY. messages->messages[messages->size -1] = *message; - messages->messages[messages->size -1].payload = malloc(message->length * sizeof (char)); - strncpy(messages->messages[messages->size -1].payload, message->payload, message->length); + if (message->length > 0 && message->payload != NULL) { + messages->messages[messages->size -1].payload = malloc(message->length * sizeof (char)); + strncpy(messages->messages[messages->size -1].payload, message->payload, message->length); + } + else { + messages->messages[messages->size -1].payload = NULL; + } - printf ("first message payload (%d): %s\n", message->length, message->payload); - printf ("second message payload (%d): %s\n" - , messages->messages[messages->size - 1].length - , messages->messages[messages->size - 1].payload); + if (message->length > 0 && message->payload != NULL) { + printf ("first message payload (%d): %s\n", message->length, message->payload); + printf ("second message payload (%d): %s\n" + , messages->messages[messages->size - 1].length + , messages->messages[messages->size - 1].payload); + } IPC_RETURN_NO_ERROR; } @@ -182,11 +189,16 @@ struct ipc_error ipc_messages_del (struct ipc_messages *messages, uint32_t inde T_R ((messages->size == 0 || index >= messages->size), IPC_ERROR_MESSAGE_DEL__INDEX_ERROR); // NOT A DEEP COPY. - messages->messages[index] = messages->messages[messages->size - 1]; messages->size--; - messages->messages = realloc (messages->messages, sizeof (struct ipc_message) * messages->size); - - T_R ((messages->messages == NULL), IPC_ERROR_MESSAGE_DEL__EMPTY_LIST); + if (messages->size == 0) { + free (messages->messages); + messages->messages = NULL; + } + else { + messages->messages[index] = messages->messages[messages->size]; + messages->messages = realloc (messages->messages, sizeof (struct ipc_message) * messages->size); + T_R ((messages->messages == NULL), IPC_ERROR_MESSAGE_DEL__EMPTY_LIST); + } IPC_RETURN_NO_ERROR; } diff --git a/tests/func_01_connection_establishmentd.c b/tests/func_01_connection_establishmentd.c index 06f5b3f..d466745 100644 --- a/tests/func_01_connection_establishmentd.c +++ b/tests/func_01_connection_establishmentd.c @@ -6,7 +6,7 @@ #define SERVICE_NAME "pong" -int main(int argc, char * argv[], char **env) +int main(int argc, char * argv[]) { argc = (int) argc; argv = (char **) argv; @@ -52,7 +52,7 @@ int main(int argc, char * argv[], char **env) TEST_IPC_Q(ipc_close_all(&ctx), EXIT_FAILURE); printf ("func 01 - closing ctx...\n"); - ipc_connections_free (&ctx); + ipc_ctx_free (&ctx); return EXIT_SUCCESS; } diff --git a/tests/func_02_pong.c b/tests/func_02_pong.c index 11c9b35..00c2cdc 100644 --- a/tests/func_02_pong.c +++ b/tests/func_02_pong.c @@ -75,7 +75,7 @@ void interactive () TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE); - ipc_connections_free (&ctx); + ipc_ctx_free (&ctx); exit (EXIT_SUCCESS); } diff --git a/tests/func_02_pongd.c b/tests/func_02_pongd.c index 539985b..6d2e697 100644 --- a/tests/func_02_pongd.c +++ b/tests/func_02_pongd.c @@ -99,7 +99,7 @@ void exit_program(int signal) TEST_IPC_Q(ipc_close_all (ctx), EXIT_FAILURE); // free remaining ctx - ipc_connections_free (ctx); + ipc_ctx_free (ctx); free (ctx); exit(EXIT_SUCCESS); diff --git a/tests/func_03_multiple-communications-client.c b/tests/func_03_multiple-communications-client.c index f75a421..20e5bd8 100644 --- a/tests/func_03_multiple-communications-client.c +++ b/tests/func_03_multiple-communications-client.c @@ -52,7 +52,7 @@ void read_message (struct ipc_connection_info *ci) break; } - ipc_connections_free (&clients); + ipc_ctx_free (&clients); #else SECURE_DECLARATION (struct ipc_message, m); diff --git a/tests/func_03_multiple-communications-server.c b/tests/func_03_multiple-communications-server.c index 2fce533..ff8a910 100644 --- a/tests/func_03_multiple-communications-server.c +++ b/tests/func_03_multiple-communications-server.c @@ -57,7 +57,7 @@ int main_loop(int argc, char * argv[], char **env) } printf ("func 03 - closing clients...\n"); - ipc_connections_free (&clients); + ipc_ctx_free (&clients); printf ("func 03 - closing server...\n"); TEST_IPC_Q (ipc_server_close(&srv), EXIT_FAILURE);