From ef2c05fc25544cfe6409e1ef0ee6e6a645c85c6d Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sat, 26 Oct 2019 18:17:20 +0200 Subject: [PATCH] v0.4.0: ipc_wait_event* functions now receive a timeout value --- Makefile | 24 ++++++++++++------------ project.zsh | 2 +- src/communication.c | 27 +++++++++++++++++++++++---- src/ipc.h | 9 ++++++--- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 6e9bd63..df5f962 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PACKAGE = 'ipc' -VERSION = '0.1.0' +VERSION = '0.4.0' PREFIX := /usr/local BINDIR := $(PREFIX)/bin @@ -62,25 +62,25 @@ libipc.so: src/communication.o src/error.o src/logger.o src/message.o src/networ $(Q)$(CC) -o libipc.so -shared $(LDFLAGS) src/communication.o src/error.o src/logger.o src/message.o src/network.o src/usocket.o src/utils.o libipc.so.install: libipc.so - @echo ' IN > $(LIBDIR)/libipc.so.0.1.0' + @echo ' IN > $(LIBDIR)/libipc.so.0.4.0' $(Q)mkdir -p '$(DESTDIR)$(LIBDIR)' - $(Q)install -m0755 libipc.so $(DESTDIR)$(LIBDIR)/libipc.so.0.1.0 - @echo ' LN > $(LIBDIR)/libipc.so.0.1' - $(Q)ln -sf '$(LIBDIR)/libipc.so.0.1.0' '$(DESTDIR)/$(LIBDIR)/libipc.so.0.1' + $(Q)install -m0755 libipc.so $(DESTDIR)$(LIBDIR)/libipc.so.0.4.0 + @echo ' LN > $(LIBDIR)/libipc.so.0.4' + $(Q)ln -sf '$(LIBDIR)/libipc.so.0.4.0' '$(DESTDIR)/$(LIBDIR)/libipc.so.0.4' @echo ' LN > $(LIBDIR)/libipc.so.0' - $(Q)ln -sf '$(LIBDIR)/libipc.so.0.1.0' '$(DESTDIR)/$(LIBDIR)/libipc.so.0' + $(Q)ln -sf '$(LIBDIR)/libipc.so.0.4.0' '$(DESTDIR)/$(LIBDIR)/libipc.so.0' @echo ' LN > $(LIBDIR)/libipc.so' - $(Q)ln -sf '$(LIBDIR)/libipc.so.0.1.0' '$(DESTDIR)/$(LIBDIR)/libipc.so' + $(Q)ln -sf '$(LIBDIR)/libipc.so.0.4.0' '$(DESTDIR)/$(LIBDIR)/libipc.so' libipc.so.clean: @echo ' RM > libipc.so' $(Q)rm -f libipc.so libipc.so.uninstall: - @echo ' RM > $(LIBDIR)/libipc.so.0.1.0' - $(Q)rm -f '$(DESTDIR)$(LIBDIR)/libipc.so.0.1.0' - @echo ' RM > $(LIBDIR)/libipc.so.0.1' - $(Q)rm -f '$(DESTDIR)$(LIBDIR)/libipc.so.0.1' + @echo ' RM > $(LIBDIR)/libipc.so.0.4.0' + $(Q)rm -f '$(DESTDIR)$(LIBDIR)/libipc.so.0.4.0' + @echo ' RM > $(LIBDIR)/libipc.so.0.4' + $(Q)rm -f '$(DESTDIR)$(LIBDIR)/libipc.so.0.4' @echo ' RM > $(LIBDIR)/libipc.so.0' $(Q)rm -f '$(DESTDIR)$(LIBDIR)/libipc.so.0' @echo ' RM > $(LIBDIR)/libipc.so' @@ -296,7 +296,7 @@ $(PACKAGE)-$(VERSION).tar.bz2: distdir $(PACKAGE)-$(VERSION)/src/utils.h help: - @echo ' :: ipc-0.1.0' + @echo ' :: ipc-0.4.0' @echo '' @echo 'Generic targets:' @echo ' - help  Prints this help message.' diff --git a/project.zsh b/project.zsh index ce7d365..406f707 100644 --- a/project.zsh +++ b/project.zsh @@ -1,6 +1,6 @@ package=ipc -version=0.3.0 +version=0.4.0 CFLAGS="-Wall -Wextra -g" diff --git a/src/communication.c b/src/communication.c index 0206d61..4f89cf0 100644 --- a/src/communication.c +++ b/src/communication.c @@ -381,7 +381,8 @@ enum ipc_errors handle_message (struct ipc_event *event enum ipc_errors ipc_wait_event_networkd (struct ipc_connection_infos *cinfos , struct ipc_connection_info *cinfo // NULL for clients , struct ipc_event *event - , struct ipc_switchings *switchdb) + , struct ipc_switchings *switchdb + , long *timer /** TODO: timers */) { T_R ((cinfos == NULL), IPC_ERROR_WAIT_EVENT__NO_CLIENTS_PARAM); T_R ((event == NULL), IPC_ERROR_WAIT_EVENT__NO_EVENT_PARAM); @@ -419,7 +420,24 @@ enum ipc_errors ipc_wait_event_networkd (struct ipc_connection_infos *cinfos } readf = master; - T_PERROR_R ((select(fdmax+1, &readf, NULL, NULL, NULL) == -1), "select", IPC_ERROR_WAIT_EVENT__SELECT); + + struct timeval *ptimeout = NULL; + SECURE_DECLARATION (struct timeval, timeout); + + if (timer != NULL && *timer > 0) { + timeout.tv_sec = *timer; + ptimeout = &timeout; + } + + T_PERROR_R ((select(fdmax+1, &readf, NULL, NULL, ptimeout) == -1), "select", IPC_ERROR_WAIT_EVENT__SELECT); + + if (ptimeout != NULL) { + *timer = timeout.tv_sec; + if (*timer == 0) { + IPC_EVENT_SET(event, IPC_EVENT_TYPE_TIMER, NULL, NULL); + return IPC_ERROR_NONE; + } + } for (i = 0; i <= (size_t) fdmax; i++) { if (FD_ISSET(i, &readf)) { @@ -440,9 +458,10 @@ enum ipc_errors ipc_wait_event_networkd (struct ipc_connection_infos *cinfos enum ipc_errors ipc_wait_event (struct ipc_connection_infos *cinfos , struct ipc_connection_info *cinfo // NULL for clients - , struct ipc_event *event) + , struct ipc_event *event + , long *timer) { - return ipc_wait_event_networkd (cinfos, cinfo, event, NULL); + return ipc_wait_event_networkd (cinfos, cinfo, event, NULL, timer); } // store and remove only pointers on allocated structures diff --git a/src/ipc.h b/src/ipc.h index 3f069f2..d1515da 100644 --- a/src/ipc.h +++ b/src/ipc.h @@ -18,7 +18,7 @@ #define IPC_HEADER_SIZE 6 #define IPC_MAX_MESSAGE_SIZE 8000000-IPC_HEADER_SIZE -#define IPC_VERSION 3 +#define IPC_VERSION 4 #if ! defined(IPC_WITHOUT_ERRORS) && ! defined(IPC_WITH_ERRORS) #define IPC_WITH_ERRORS 2 @@ -123,6 +123,7 @@ enum ipc_event_type { , IPC_EVENT_TYPE_DISCONNECTION = 5 , IPC_EVENT_TYPE_MESSAGE = 6 , IPC_EVENT_TYPE_LOOKUP = 7 + , IPC_EVENT_TYPE_TIMER = 8 }; enum ipc_errors { @@ -310,7 +311,8 @@ enum ipc_errors ipc_write (const struct ipc_connection_info *, const struct ipc_ enum ipc_errors ipc_wait_event (struct ipc_connection_infos *clients , struct ipc_connection_info *srv - , struct ipc_event *event); + , struct ipc_event *event + , long *timer); // store and remove only pointers on allocated structures enum ipc_errors ipc_add (struct ipc_connection_infos *, struct ipc_connection_info *); @@ -390,7 +392,8 @@ struct networkd { enum ipc_errors ipc_wait_event_networkd (struct ipc_connection_infos *cinfos , struct ipc_connection_info *cinfo // NULL for clients , struct ipc_event *event - , struct ipc_switchings *switchdb); + , struct ipc_switchings *switchdb + , long *timer); void ipc_switching_add (struct ipc_switchings *is, int orig, int dest);