Multiple clients: ok.
parent
a5877101eb
commit
71db43802e
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -165,13 +165,20 @@ struct ipc_error ipc_messages_add (struct ipc_messages *messages, const struct
|
|||
|
||||
// DEEP COPY.
|
||||
messages->messages[messages->size -1] = *message;
|
||||
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;
|
||||
}
|
||||
|
||||
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--;
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Reference in New Issue