diff --git a/src/communication.c b/src/communication.c index a5f8ca8..6d8f563 100644 --- a/src/communication.c +++ b/src/communication.c @@ -15,6 +15,8 @@ // print structures #include "message.h" +struct ipc_error ipc_write_fd (int fd, const struct ipc_message *m); + struct ipc_error service_path (char *path, const char *sname) { @@ -106,8 +108,6 @@ struct ipc_error ipc_server_init (struct ipc_ctx **ctx, const char *sname) IPC_RETURN_NO_ERROR; } -struct ipc_error ipc_write_fd (int fd, const struct ipc_message *m); - // when networkd is not working properly (or do not retrieve the service): srv->fd = 0 struct ipc_error ipc_contact_networkd (int *pfd, const char *sname) { @@ -263,8 +263,6 @@ struct ipc_error ipc_read (const struct ipc_ctx *ctx, uint32_t index, struct ipc struct ipc_error ipc_write_fd (int fd, const struct ipc_message *m) { - T_R ((m == NULL), IPC_ERROR_WRITE__NO_MESSAGE_PARAM); - char *buf = NULL; size_t msize = 0; ipc_message_format_write (m, &buf, &msize); @@ -281,10 +279,10 @@ struct ipc_error ipc_write_fd (int fd, const struct ipc_message *m) IPC_RETURN_NO_ERROR; } -// TODO: high level API, should be buffered, socket may not be usable for now. -struct ipc_error ipc_write (const struct ipc_ctx *ctx, uint32_t index, const struct ipc_message *m) +// Put the message in the list of messages to send. +struct ipc_error ipc_write (struct ipc_ctx *ctx, const struct ipc_message *m) { - return ipc_write_fd (ctx->pollfd[index].fd, m); + return ipc_messages_add (&ctx->tx, m); } /** @@ -388,11 +386,33 @@ struct ipc_error ipc_del_fd (struct ipc_ctx *ctx, int fd) // TODO struct ipc_error handle_writing_message (struct ipc_event *event, struct ipc_ctx *ctx, uint32_t index); + struct ipc_error handle_writing_message (struct ipc_event *event, struct ipc_ctx *ctx, uint32_t index) { + // struct ipc_message { + // char type; + // char user_type; + // uint32_t length; + // char *payload; + // int fd; // File descriptor concerned about this message. + // }; + // TODO + int txfd = ctx->pollfd[index].fd; + int mfd; + struct ipc_message *m; for (size_t i = 0; ctx->tx.size ; i++) { - // TODO + m = &ctx->tx.messages[i]; + mfd = m->fd; + printf ("Something to send on fd %d\n", mfd); + if (txfd == mfd) { + printf ("The fd %d is available!\n", txfd); + + printf ("TODO: write!\n"); + + printf ("Removing the message\n"); + ipc_messages_del (&ctx->tx, i); // remove the message indexed by i + } } IPC_EVENT_SET (event, IPC_EVENT_TYPE_TX, index, ctx->pollfd[index].fd, NULL); diff --git a/src/ipc.h b/src/ipc.h index 1f8ea8b..3e1632c 100644 --- a/src/ipc.h +++ b/src/ipc.h @@ -185,6 +185,9 @@ enum ipc_error_code { , IPC_ERROR_CTX_INIT__MALLOC_POLLFD = 97 , IPC_ERROR_CONTACT_NETWORKD__NO_FD_PARAM = 98 , IPC_ERROR_HANDLE_NEW_CONNECTION__INCONSISTENT_INDEX = 99 + , IPC_ERROR_DEL_MESSAGE_TO_SEND__NO_PARAM_MESSAGES = 100 + , IPC_ERROR_MESSAGE_DEL__INDEX_ERROR = 101 + , IPC_ERROR_MESSAGE_DEL__EMPTY_LIST = 102 }; struct ipc_error { @@ -226,6 +229,9 @@ struct ipc_messages { size_t size; }; +struct ipc_error ipc_messages_del (struct ipc_messages *messages, uint32_t index); +struct ipc_error ipc_message_free (struct ipc_message *message); + /** * Context of the whole networking state. */ @@ -296,7 +302,7 @@ struct ipc_error ipc_close (struct ipc_ctx *ctx, uint32_t index); struct ipc_error ipc_close_all (struct ipc_ctx *ctx); struct ipc_error ipc_read (const struct ipc_ctx *, uint32_t index, struct ipc_message *m); -struct ipc_error ipc_write (const struct ipc_ctx *, uint32_t index, const struct ipc_message *m); +struct ipc_error ipc_write (struct ipc_ctx *, const struct ipc_message *m); struct ipc_error ipc_wait_event (struct ipc_ctx *, struct ipc_event *, int *timer); @@ -326,7 +332,7 @@ struct ipc_error ipc_message_format_data (struct ipc_message *m, char utype, con struct ipc_error ipc_message_format_server_close (struct ipc_message *m); struct ipc_error ipc_message_empty (struct ipc_message *m); -struct ipc_error ipc_messages_add (struct ipc_messages *, struct ipc_message *); +struct ipc_error ipc_messages_add (struct ipc_messages *, const struct ipc_message *); void ipc_messages_free (struct ipc_messages *); // Switch cases macros diff --git a/src/message.c b/src/message.c index c9d7ae2..5cc9d63 100644 --- a/src/message.c +++ b/src/message.c @@ -147,10 +147,10 @@ struct ipc_error ipc_message_empty (struct ipc_message *m) } // store and remove only pointers on allocated structures -struct ipc_error ipc_messages_add (struct ipc_messages *messages, struct ipc_message *message) +struct ipc_error ipc_messages_add (struct ipc_messages *messages, const struct ipc_message *message) { - T_R (( messages == NULL), IPC_ERROR_ADD_MESSAGE_TO_SEND__NO_PARAM_MESSAGES); - T_R (( message == NULL), IPC_ERROR_ADD_MESSAGE_TO_SEND__NO_PARAM_MESSAGE); + T_R ((messages == NULL), IPC_ERROR_ADD_MESSAGE_TO_SEND__NO_PARAM_MESSAGES); + T_R ((message == NULL), IPC_ERROR_ADD_MESSAGE_TO_SEND__NO_PARAM_MESSAGE); messages->size++; if (messages->size == 1 && messages->messages == NULL) { @@ -168,6 +168,31 @@ struct ipc_error ipc_messages_add (struct ipc_messages *messages, struct ipc_me IPC_RETURN_NO_ERROR; } +struct ipc_error ipc_message_free (struct ipc_message *message) +{ + if (message->payload != NULL) { + free (message->payload); + message->payload = NULL; + } + IPC_RETURN_NO_ERROR; +} + +// Remove only pointers on allocated structures. +struct ipc_error ipc_messages_del (struct ipc_messages *messages, uint32_t index) +{ + T_R ((messages == NULL), IPC_ERROR_DEL_MESSAGE_TO_SEND__NO_PARAM_MESSAGES); + 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); + + IPC_RETURN_NO_ERROR; +} + void ipc_messages_free (struct ipc_messages *messages) { if (messages != NULL)