From 498e0509eef9ad8695ccac682a260c56fb8a729f Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sun, 4 Nov 2018 09:50:17 +0100 Subject: [PATCH] every public function in the same header file --- core/client.c | 4 +- core/client.h | 25 +----- core/communication.c | 16 ++-- core/communication.h | 43 +--------- core/error.h | 9 +-- core/event.h | 16 +--- core/ipc.h | 182 ++++++++++++++++++++++++++++++++++++++++--- core/message.h | 32 +------- core/usocket.c | 4 +- core/usocket.h | 34 +------- pong/app/ipc-debug.c | 2 +- pong/app/pong.c | 3 +- pong/app/pongd.c | 1 + 13 files changed, 198 insertions(+), 173 deletions(-) diff --git a/core/client.c b/core/client.c index babb9cf..03be6cc 100644 --- a/core/client.c +++ b/core/client.c @@ -30,7 +30,7 @@ void ipc_server_client_gen (struct ipc_client *p p->index = index; } -int32_t ipc_client_add (struct ipc_clients *clients, struct ipc_client *p) +int32_t ipc_clients_add (struct ipc_clients *clients, struct ipc_client *p) { assert(clients != NULL); assert(p != NULL); @@ -47,7 +47,7 @@ int32_t ipc_client_add (struct ipc_clients *clients, struct ipc_client *p) return 0; } -int32_t ipc_client_del (struct ipc_clients *clients, struct ipc_client *p) +int32_t ipc_clients_del (struct ipc_clients *clients, struct ipc_client *p) { assert(clients != NULL); assert(p != NULL); diff --git a/core/client.h b/core/client.h index 44ea289..8366fe0 100644 --- a/core/client.h +++ b/core/client.h @@ -2,30 +2,9 @@ #define __IPC_CLIENT_H__ #include - -struct ipc_client { - uint32_t version; - uint32_t index; - int32_t proc_fd; -}; - -struct ipc_clients { - struct ipc_client **clients; - int32_t size; -}; - -// store and remove only pointers on allocated structures -int32_t ipc_client_add (struct ipc_clients *, struct ipc_client *); -int32_t ipc_client_del (struct ipc_clients *, struct ipc_client *); +#include "ipc.h" void ipc_clients_print (struct ipc_clients *); -void ipc_clients_free (struct ipc_clients *); - -struct ipc_client * ipc_server_client_copy (const struct ipc_client *p); -int32_t ipc_server_client_eq (const struct ipc_client *p1, const struct ipc_client *p2); -// create the service client structure -void ipc_server_client_gen (struct ipc_client *p - , uint32_t index, uint32_t version); - void client_print (struct ipc_client *); + #endif diff --git a/core/communication.c b/core/communication.c index e4eae08..2fd57b1 100644 --- a/core/communication.c +++ b/core/communication.c @@ -212,7 +212,7 @@ int32_t ipc_server_select (struct ipc_clients *clients, struct ipc_service *srv } else { for(j = 0; j < clients->size; j++) { if(i == clients->clients[j]->proc_fd ) { - ipc_client_add (active_clients, clients->clients[j]); + ipc_clients_add (active_clients, clients->clients[j]); } } } @@ -269,7 +269,7 @@ int32_t ipc_application_select (struct ipc_services *services, struct ipc_servic if (FD_ISSET(i, &readf)) { for(j = 0; j < services->size; j++) { if(i == services->services[j]->service_fd ) { - ipc_service_add (active_services, services->services[j]); + ipc_services_add (active_services, services->services[j]); } } } @@ -292,8 +292,8 @@ int32_t handle_new_connection (struct ipc_service *srv printf("new connection\n"); } - if (ipc_client_add (clients, *new_client) < 0) { - handle_error("ipc_client_add < 0"); + if (ipc_clients_add (clients, *new_client) < 0) { + handle_error("ipc_clients_add < 0"); return 1; } @@ -374,8 +374,8 @@ int32_t ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service if (ipc_server_close_client (pc) < 0) { handle_err( "ipc_service_poll_event", "ipc_server_close_client < 0"); } - if (ipc_client_del (clients, pc) < 0) { - handle_err( "ipc_service_poll_event", "ipc_client_del < 0"); + if (ipc_clients_del (clients, pc) < 0) { + handle_err( "ipc_service_poll_event", "ipc_clients_del < 0"); } ipc_message_empty (m); free (m); @@ -480,8 +480,8 @@ int32_t ipc_application_poll_event_ (struct ipc_services *services, struct ipc_e if (ipc_application_close (ps) < 0) { handle_err( "ipc_application_poll_event", "ipc_application_close < 0"); } - if (ipc_service_del (services, ps) < 0) { - handle_err( "ipc_application_poll_event", "ipc_service_del < 0"); + if (ipc_services_del (services, ps) < 0) { + handle_err( "ipc_application_poll_event", "ipc_services_del < 0"); } ipc_message_empty (m); free (m); diff --git a/core/communication.h b/core/communication.h index c0ee326..c98ee75 100644 --- a/core/communication.h +++ b/core/communication.h @@ -5,53 +5,16 @@ #include #include #include // error numbers -#include "message.h" + +#include "ipc.h" #include "client.h" #include "event.h" - -#define COMMUNICATION_VERSION 1 +#include "message.h" #define IPC_WITH_UNIX_SOCKETS #ifdef IPC_WITH_UNIX_SOCKETS #include "usocket.h" #endif -// SERVICE - -// srv->version and srv->index must be already set -// init unix socket + fill srv->spath -int32_t ipc_server_init (char **env - , struct ipc_service *srv, const char *sname); -int32_t ipc_server_close (struct ipc_service *srv); -int32_t ipc_server_close_client (struct ipc_client *p); -int32_t ipc_server_accept (struct ipc_service *srv, struct ipc_client *p); - -// 1 on a recipient socket close -int32_t ipc_server_read (const struct ipc_client *, struct ipc_message *m); -int32_t ipc_server_write (const struct ipc_client *, const struct ipc_message *m); - -int32_t ipc_server_select (struct ipc_clients * clients, struct ipc_service *srv - , struct ipc_clients *active_clients, int32_t *new_connection); - -int32_t ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv - , struct ipc_event *event); - -// APPLICATION - -// Initialize connection with unix socket -// send the connection string to $TMP/ -// fill srv->spath && srv->service_fd -int32_t ipc_application_connection (char **env - , struct ipc_service *, const char *); -int32_t ipc_application_close (struct ipc_service *); - -// 1 on a recipient socket close -int32_t ipc_application_read (struct ipc_service *srv, struct ipc_message *m); -int32_t ipc_application_write (struct ipc_service *, const struct ipc_message *m); - -int32_t ipc_application_select (struct ipc_services *services, struct ipc_services *active_services); - -int32_t ipc_application_poll_event (struct ipc_services *services, struct ipc_event *event); -int32_t ipc_application_peek_event (struct ipc_services *services, struct ipc_event *event); #endif diff --git a/core/error.h b/core/error.h index 41b2c6b..cd1c780 100644 --- a/core/error.h +++ b/core/error.h @@ -1,17 +1,10 @@ #ifndef __IPC_ERROR_H__ #define __IPC_ERROR_H__ -#include "logger.h" - -enum ipc_errors { - IPC_ERROR_NOT_ENOUGH_MEMORY - , IPC_ERROR_WRONG_PARAMETERS - , IPC_ERROR_READ -}; - // #define IPC_WITH_ERRORS 3 #ifdef IPC_WITH_ERRORS +#include "logger.h" #define handle_error(msg) \ do { log_error (msg); exit(EXIT_FAILURE); } while (0) diff --git a/core/event.h b/core/event.h index 9ce46eb..eb97be6 100644 --- a/core/event.h +++ b/core/event.h @@ -1,23 +1,9 @@ #ifndef __IPC_EVENT__ #define __IPC_EVENT__ +#include "ipc.h" #include "message.h" -enum ipc_event_type { - IPC_EVENT_TYPE_NOT_SET - , IPC_EVENT_TYPE_ERROR - , IPC_EVENT_TYPE_STDIN - , IPC_EVENT_TYPE_CONNECTION - , IPC_EVENT_TYPE_DISCONNECTION - , IPC_EVENT_TYPE_MESSAGE -}; - -struct ipc_event { - enum ipc_event_type type; - void* origin; // currently used as an client or service pointer - void* m; // message pointer -}; - #define IPC_EVENT_SET(pevent,type_,message_,origin_) {\ pevent->type = type_; \ pevent->m = message_; \ diff --git a/core/ipc.h b/core/ipc.h index 296f589..bf737f2 100644 --- a/core/ipc.h +++ b/core/ipc.h @@ -2,15 +2,179 @@ #define __IPC_H__ #include +#include +#include +#include +#include +#include // error numbers +#include + +#define RUNDIR "/run/ipc/" +#define PATH_MAX 4096 +#define IPC_HEADER_SIZE 5 +#define IPC_MAX_MESSAGE_SIZE 8000000-IPC_HEADER_SIZE +// #include "queue.h" + +#define IPC_VERSION 1 + + +enum msg_types { + MSG_TYPE_SERVER_CLOSE = 0 + , MSG_TYPE_ERR + , MSG_TYPE_DATA +} message_types; + +enum ipc_event_type { + IPC_EVENT_TYPE_NOT_SET + , IPC_EVENT_TYPE_ERROR + , IPC_EVENT_TYPE_STDIN + , IPC_EVENT_TYPE_CONNECTION + , IPC_EVENT_TYPE_DISCONNECTION + , IPC_EVENT_TYPE_MESSAGE +}; + +enum ipc_errors { + IPC_ERROR_NOT_ENOUGH_MEMORY + , IPC_ERROR_WRONG_PARAMETERS + , IPC_ERROR_READ +}; + +struct ipc_service { + uint32_t version; + uint32_t index; + char spath[PATH_MAX]; + int32_t service_fd; +}; + +struct ipc_services { + struct ipc_service ** services; + int32_t size; +}; + +struct ipc_client { + uint32_t version; + uint32_t index; + int32_t proc_fd; +}; + +struct ipc_clients { + struct ipc_client **clients; + int32_t size; +}; + +struct ipc_message { + char type; + uint32_t length; + char *payload; +}; + +struct ipc_event { + enum ipc_event_type type; + void* origin; // currently used as an client or service pointer + void* m; // message pointer +}; + + + +/* + * SERVICE + * + **/ + +// srv->version and srv->index must be already set +// init unix socket + fill srv->spath +int32_t ipc_server_init (char **env , struct ipc_service *srv, const char *sname); +int32_t ipc_server_close (struct ipc_service *srv); +int32_t ipc_server_close_client (struct ipc_client *p); +int32_t ipc_server_accept (struct ipc_service *srv, struct ipc_client *p); + +// 1 on a recipient socket close +int32_t ipc_server_read (const struct ipc_client *, struct ipc_message *m); +int32_t ipc_server_write (const struct ipc_client *, const struct ipc_message *m); + +int32_t ipc_server_select (struct ipc_clients * clients, struct ipc_service *srv + , struct ipc_clients *active_clients, int32_t *new_connection); + +int32_t ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv + , struct ipc_event *event); + +/** + * SERVICES + */ + +// store and remove only pointers on allocated structures +int32_t ipc_services_add (struct ipc_services *, struct ipc_service *); +int32_t ipc_services_del (struct ipc_services *, struct ipc_service *); + +void ipc_services_free (struct ipc_services *); + +struct ipc_service * ipc_client_server_copy (const struct ipc_service *p); +int32_t ipc_service_eq (const struct ipc_service *p1, const struct ipc_service *p2); +// create the client service structure +void ipc_client_server_gen (struct ipc_service *p, uint32_t index, uint32_t version); + +static inline int32_t ipc_service_empty (struct ipc_service *srv) { srv = srv; return 0 ;}; + + +/* + * APPLICATION + * + **/ + +// Initialize connection with unix socket +// send the connection string to $TMP/ +// fill srv->spath && srv->service_fd +int32_t ipc_application_connection (char **env, struct ipc_service *, const char *); +int32_t ipc_application_close (struct ipc_service *); + +// 1 on a recipient socket close +int32_t ipc_application_read (struct ipc_service *srv, struct ipc_message *m); +int32_t ipc_application_write (struct ipc_service *, const struct ipc_message *m); + +int32_t ipc_application_select (struct ipc_services *services, struct ipc_services *active_services); +int32_t ipc_application_poll_event (struct ipc_services *services, struct ipc_event *event); +int32_t ipc_application_peek_event (struct ipc_services *services, struct ipc_event *event); + + + +/* + * MESSAGE + * + **/ + +// used to create msg structure from buffer +int32_t ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msize); +// used to create buffer from msg structure +int32_t ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t *msize); + +// read a structure msg from fd +int32_t ipc_message_read (int32_t fd, struct ipc_message *m); +// write a structure msg to fd +int32_t ipc_message_write (int32_t fd, const struct ipc_message *m); + +int32_t ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length); +int32_t ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length); +int32_t ipc_message_format_server_close (struct ipc_message *m); + +int32_t ipc_message_empty (struct ipc_message *m); + + + +/* + * CLIENT + * + **/ + +// store and remove only pointers on allocated structures +int32_t ipc_clients_add (struct ipc_clients *, struct ipc_client *); +int32_t ipc_clients_del (struct ipc_clients *, struct ipc_client *); + +void ipc_clients_free (struct ipc_clients *); + +struct ipc_client * ipc_server_client_copy (const struct ipc_client *p); +int32_t ipc_server_client_eq (const struct ipc_client *p1, const struct ipc_client *p2); +// create the service client structure +void ipc_server_client_gen (struct ipc_client *p, uint32_t index, uint32_t version); -#include "client.h" -#include "communication.h" -#include "error.h" -#include "event.h" -#include "logger.h" -#include "message.h" -#include "queue.h" -#include "usocket.h" -#include "utils.h" #endif diff --git a/core/message.h b/core/message.h index 8358966..2aeac2a 100644 --- a/core/message.h +++ b/core/message.h @@ -7,37 +7,7 @@ #include -// the underlying communication must always correctly handled by the system -// (currently: unix sockets) - -enum msg_types { - MSG_TYPE_SERVER_CLOSE = 0 - , MSG_TYPE_ERR - , MSG_TYPE_DATA -} message_types; - -struct ipc_message { - char type; - uint32_t length; - char *payload; -}; - -// used to create msg structure from buffer -int32_t ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msize); -// used to create buffer from msg structure -int32_t ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t *msize); - -// read a structure msg from fd -// 1 on a recipient socket close -int32_t ipc_message_read (int32_t fd, struct ipc_message *m); -// write a structure msg to fd -int32_t ipc_message_write (int32_t fd, const struct ipc_message *m); - -int32_t ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length); -int32_t ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length); -int32_t ipc_message_format_server_close (struct ipc_message *m); - -int32_t ipc_message_empty (struct ipc_message *m); +#include "ipc.h" void ipc_message_print (const struct ipc_message *m); #endif diff --git a/core/usocket.c b/core/usocket.c index 13401f5..d553c66 100644 --- a/core/usocket.c +++ b/core/usocket.c @@ -305,7 +305,7 @@ void ipc_client_server_gen (struct ipc_service *p p->index = index; } -int32_t ipc_service_add (struct ipc_services *services, struct ipc_service *p) +int32_t ipc_services_add (struct ipc_services *services, struct ipc_service *p) { assert(services != NULL); assert(p != NULL); @@ -322,7 +322,7 @@ int32_t ipc_service_add (struct ipc_services *services, struct ipc_service *p) return 0; } -int32_t ipc_service_del (struct ipc_services *services, struct ipc_service *p) +int32_t ipc_services_del (struct ipc_services *services, struct ipc_service *p) { assert(services != NULL); assert(p != NULL); diff --git a/core/usocket.h b/core/usocket.h index ec172b1..370eebb 100644 --- a/core/usocket.h +++ b/core/usocket.h @@ -5,28 +5,11 @@ #include #include +#include "ipc.h" #include #define LISTEN_BACKLOG 128 -#define RUNDIR "/run/ipc/" -#define PATH_MAX 4096 - -#define IPC_HEADER_SIZE 5 -#define IPC_MAX_MESSAGE_SIZE 8000000-IPC_HEADER_SIZE - -struct ipc_service { - uint32_t version; - uint32_t index; - char spath[PATH_MAX]; - int32_t service_fd; -}; - -struct ipc_services { - struct ipc_service ** services; - int32_t size; -}; - /** * for all functions: 0 ok, < 0 not ok */ @@ -58,22 +41,7 @@ int32_t usock_accept (int32_t fd, int32_t *pfd); // same as unlink(2) int32_t usock_remove (const char *path); -static inline int32_t ipc_service_empty (struct ipc_service *srv) { srv = srv; return 0 ;}; - - -// store and remove only pointers on allocated structures -int32_t ipc_service_add (struct ipc_services *, struct ipc_service *); -int32_t ipc_service_del (struct ipc_services *, struct ipc_service *); - void ipc_services_print (struct ipc_services *); -void ipc_services_free (struct ipc_services *); - -struct ipc_service * ipc_client_server_copy (const struct ipc_service *p); -int32_t ipc_service_eq (const struct ipc_service *p1, const struct ipc_service *p2); -// create the client service structure -void ipc_client_server_gen (struct ipc_service *p - , uint32_t index, uint32_t version); - void service_print (struct ipc_service *); #endif diff --git a/pong/app/ipc-debug.c b/pong/app/ipc-debug.c index a22da43..28701a9 100644 --- a/pong/app/ipc-debug.c +++ b/pong/app/ipc-debug.c @@ -37,7 +37,7 @@ void interactive (char * service_name, char *env[]) struct ipc_services services; memset (&services, 0, sizeof (struct ipc_services)); - ipc_service_add (&services, &srv); + ipc_services_add (&services, &srv); int ret = 0; while (1) { diff --git a/pong/app/pong.c b/pong/app/pong.c index f863238..3a458f6 100644 --- a/pong/app/pong.c +++ b/pong/app/pong.c @@ -4,6 +4,7 @@ #include #include "../../core/ipc.h" +#include "../../core/error.h" #define MSG "coucou" #define SERVICE_NAME "pongd" @@ -70,7 +71,7 @@ void interactive (char *env[]) struct ipc_services services; memset (&services, 0, sizeof (struct ipc_services)); - ipc_service_add (&services, &srv); + ipc_services_add (&services, &srv); while (1) { printf ("msg to send: "); diff --git a/pong/app/pongd.c b/pong/app/pongd.c index 0c09780..4128b9e 100644 --- a/pong/app/pongd.c +++ b/pong/app/pongd.c @@ -1,4 +1,5 @@ #include "../../core/ipc.h" +#include "../../core/error.h" #include #include