From 3b1aaf36c2aa64575d4348efe23742f367c17c94 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Fri, 3 Jul 2020 22:27:56 +0200 Subject: [PATCH] timer --- README.md | 16 ++++++++++++++++ src/communication.c | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/README.md b/README.md index 1dccfcc..d926e16 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,19 @@ Logs are in one of the following directories: `$XDG_DATA_HOME/ipc/` or `$HOME/.l The log file can be indicated with the `IPC_LOGFILE` environment variable, too. To remove logs: `make LDFLAGS=-DIPC_WITHOUT_ERRORS` + +# Planning for 0.7 + +- `libipc` should have callbacks to use along with switching capabilities, making easier to implement proxies with communication protocols + +# Planning for 0.8 + +- `libipc` should be thread-safe + +# Planning for 0.8 + +- `libipc` should use `libevent` for performance improvments + +# Planning for 1.0 + +- `libipc` should have usable bindings in several languages diff --git a/src/communication.c b/src/communication.c index 94b2ef1..9e94fc5 100644 --- a/src/communication.c +++ b/src/communication.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -467,10 +468,29 @@ struct ipc_error ipc_events_loop (struct ipc_ctx *ctx, struct ipc_event *event, } } + struct timeval tv_1; + memset (&tv_1, 0, sizeof(struct timeval)); + + struct timeval tv_2; + memset (&tv_2, 0, sizeof(struct timeval)); + + gettimeofday(&tv_1, NULL); + if ((n = poll(ctx->pollfd, ctx->size, *timer)) < 0) { IPC_RETURN_ERROR (IPC_ERROR_WAIT_EVENT__POLL); } + gettimeofday(&tv_2, NULL); + + int new_timer = *timer - ((tv_2.tv_sec - tv_1.tv_sec) * 1000000 + (tv_2.tv_usec - tv_1.tv_usec)) / 1000; + // Handle memory fuckery, 'cause low level programming is fun. + if (new_timer >= *timer || new_timer < 0) { + *timer = 0; + } + else { + *timer = new_timer; + } + // Timeout. if (n == 0) { IPC_EVENT_SET (event, IPC_EVENT_TYPE_TIMER, 0, 0, NULL);