Archived
3
0

every public function in the same header file

This commit is contained in:
Philippe PITTOLI 2018-11-04 09:50:17 +01:00
parent d4caca0fe9
commit 498e0509ee
13 changed files with 198 additions and 173 deletions

View File

@ -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);

View File

@ -2,30 +2,9 @@
#define __IPC_CLIENT_H__
#include <stdint.h>
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

View File

@ -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);

View File

@ -5,53 +5,16 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h> // 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/<service>
// 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

View File

@ -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)

View File

@ -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_; \

View File

@ -2,15 +2,179 @@
#define __IPC_H__
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> // error numbers
#include <time.h>
#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/<service>
// 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

View File

@ -7,37 +7,7 @@
#include <stdint.h>
// 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

View File

@ -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);

View File

@ -5,28 +5,11 @@
#include <sys/socket.h>
#include <sys/un.h>
#include "ipc.h"
#include <stdint.h>
#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

View File

@ -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) {

View File

@ -4,6 +4,7 @@
#include <unistd.h>
#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: ");

View File

@ -1,4 +1,5 @@
#include "../../core/ipc.h"
#include "../../core/error.h"
#include <signal.h>
#include <sys/socket.h>