diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2017-07-16 15:56:05 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2017-07-16 15:56:05 +0000 |
commit | d945bca329f7a13741e1d3afcf9f0f73f6c70ce9 (patch) | |
tree | 8a5018ae89923172361823ae24f737819ed4a9d2 /src | |
parent | f7c2e436ed0144d09cff0e3ac90f510a47f0aeac (diff) | |
download | s6-d945bca329f7a13741e1d3afcf9f0f73f6c70ce9.tar.xz |
Add ftrigr_updateb()
Diffstat (limited to 'src')
-rw-r--r-- | src/include/s6/ftrigr.h | 6 | ||||
-rw-r--r-- | src/libs6/ftrigr_ack.c | 17 | ||||
-rw-r--r-- | src/libs6/ftrigr_update.c | 57 | ||||
-rw-r--r-- | src/libs6/ftrigr_updateb.c | 65 |
4 files changed, 88 insertions, 57 deletions
diff --git a/src/include/s6/ftrigr.h b/src/include/s6/ftrigr.h index 4afbc97..425a689 100644 --- a/src/include/s6/ftrigr.h +++ b/src/include/s6/ftrigr.h @@ -3,6 +3,7 @@ #ifndef FTRIGR_H #define FTRIGR_H +#include <sys/types.h> #include <stdint.h> #include <skalibs/config.h> #include <skalibs/tai.h> @@ -55,10 +56,11 @@ struct ftrigr_s { skaclient_t connection ; genalloc list ; /* array of uint16_t */ + size_t head ; gensetdyn data ; /* set of ftrigr1_t */ skaclient_buffer_t buffers ; } ; -#define FTRIGR_ZERO { .connection = SKACLIENT_ZERO, .list = GENALLOC_ZERO, .data = GENSETDYN_INIT(ftrigr1_t, 2, 0, 1) } +#define FTRIGR_ZERO { .connection = SKACLIENT_ZERO, .list = GENALLOC_ZERO, .head = 0, .data = GENSETDYN_INIT(ftrigr1_t, 2, 0, 1) } extern ftrigr_t const ftrigr_zero ; @@ -74,9 +76,11 @@ extern void ftrigr_end (ftrigr_t *) ; /* Instant primitives for async programming */ #define ftrigr_fd(a) skaclient_fd(&(a)->connection) +extern int ftrigr_updateb (ftrigr_t *) ; extern int ftrigr_update (ftrigr_t *) ; extern int ftrigr_check (ftrigr_t *, uint16_t, char *) ; extern int ftrigr_checksa (ftrigr_t *, uint16_t, stralloc *) ; +extern void ftrigr_ack (ftrigr_t *, size_t) ; /* Synchronous functions with timeouts */ diff --git a/src/libs6/ftrigr_ack.c b/src/libs6/ftrigr_ack.c new file mode 100644 index 0000000..5c9a9f3 --- /dev/null +++ b/src/libs6/ftrigr_ack.c @@ -0,0 +1,17 @@ +/* ISC license. */ + +#include <stdint.h> +#include <skalibs/genalloc.h> +#include <s6/ftrigr.h> + +void ftrigr_ack (ftrigr_t *a, size_t n) +{ + size_t len = genalloc_len(uint16_t, &a->list) ; + a->head += n ; + if (a->head > len) a->head = len ; + if (a->head == len) + { + a->head = 0 ; + genalloc_setlen(uint16_t, &a->list, 0) ; + } +} diff --git a/src/libs6/ftrigr_update.c b/src/libs6/ftrigr_update.c index f97535d..674d8cb 100644 --- a/src/libs6/ftrigr_update.c +++ b/src/libs6/ftrigr_update.c @@ -1,66 +1,11 @@ /* ISC license. */ -#include <sys/types.h> #include <stdint.h> -#include <errno.h> -#include <skalibs/gccattributes.h> -#include <skalibs/error.h> -#include <skalibs/uint16.h> #include <skalibs/genalloc.h> -#include <skalibs/gensetdyn.h> -#include <skalibs/unixmessage.h> -#include <skalibs/skaclient.h> #include <s6/ftrigr.h> -static inline int appears (uint16_t, uint16_t const *, size_t) gccattr_pure ; - -static inline int appears (uint16_t id, uint16_t const *list, size_t len) -{ - while (len) if (id == list[--len]) return 1 ; - return 0 ; -} - -static int msghandler (unixmessage_t const *m, void *context) -{ - ftrigr_t *a = (ftrigr_t *)context ; - ftrigr1_t *p ; - int addit = 1 ; - uint16_t id ; - if (m->len != 4 || m->nfds) return (errno = EPROTO, 0) ; - uint16_unpack_big(m->s, &id) ; - p = GENSETDYN_P(ftrigr1_t, &a->data, id) ; - if (!p) return 1 ; - if (p->state != FR1STATE_LISTENING) return (errno = EINVAL, 0) ; - if (!genalloc_readyplus(uint16_t, &a->list, 1)) return 0 ; - switch (m->s[2]) - { - case 'd' : - if (!stralloc_catb(&p->what, m->s + 3, 1)) return 0 ; - p->state = FR1STATE_WAITACK ; - break ; - case '!' : - if (!stralloc_catb(&p->what, m->s + 3, 1)) return 0 ; - if (p->options & FTRIGR_REPEAT) - { - if (p->what.len > 1 - && appears(id+1, genalloc_s(uint16_t, &a->list), genalloc_len(uint16_t, &a->list))) - addit = 0 ; - } - else p->state = FR1STATE_WAITACKDATA ; - break ; - default : return (errno = EPROTO, 0) ; - } - if (addit) - { - id++ ; genalloc_append(uint16_t, &a->list, &id) ; - } - return 1 ; -} - int ftrigr_update (ftrigr_t *a) { - int r ; genalloc_setlen(uint16_t, &a->list, 0) ; - r = skaclient_update(&a->connection, &msghandler, a) ; - return r < 0 ? r : (int)genalloc_len(uint16_t, &a->list) ; + return ftrigr_updateb(a) ; } diff --git a/src/libs6/ftrigr_updateb.c b/src/libs6/ftrigr_updateb.c new file mode 100644 index 0000000..edf6f2c --- /dev/null +++ b/src/libs6/ftrigr_updateb.c @@ -0,0 +1,65 @@ +/* ISC license. */ + +#include <sys/types.h> +#include <stdint.h> +#include <errno.h> +#include <skalibs/gccattributes.h> +#include <skalibs/error.h> +#include <skalibs/uint16.h> +#include <skalibs/genalloc.h> +#include <skalibs/gensetdyn.h> +#include <skalibs/unixmessage.h> +#include <skalibs/skaclient.h> +#include <s6/ftrigr.h> + +static inline int appears (uint16_t, uint16_t const *, size_t) gccattr_pure ; + +static inline int appears (uint16_t id, uint16_t const *list, size_t len) +{ + while (len) if (id == list[--len]) return 1 ; + return 0 ; +} + +static int msghandler (unixmessage_t const *m, void *context) +{ + ftrigr_t *a = (ftrigr_t *)context ; + ftrigr1_t *p ; + int addit = 1 ; + uint16_t id ; + if (m->len != 4 || m->nfds) return (errno = EPROTO, 0) ; + uint16_unpack_big(m->s, &id) ; + p = GENSETDYN_P(ftrigr1_t, &a->data, id) ; + if (!p) return 1 ; + if (p->state != FR1STATE_LISTENING) return (errno = EINVAL, 0) ; + if (!genalloc_readyplus(uint16_t, &a->list, 1)) return 0 ; + switch (m->s[2]) + { + case 'd' : + if (!stralloc_catb(&p->what, m->s + 3, 1)) return 0 ; + p->state = FR1STATE_WAITACK ; + break ; + case '!' : + if (!stralloc_catb(&p->what, m->s + 3, 1)) return 0 ; + if (p->options & FTRIGR_REPEAT) + { + if (p->what.len > 1 + && appears(id+1, genalloc_s(uint16_t, &a->list), genalloc_len(uint16_t, &a->list))) + addit = 0 ; + } + else p->state = FR1STATE_WAITACKDATA ; + break ; + default : return (errno = EPROTO, 0) ; + } + if (addit) + { + id++ ; genalloc_append(uint16_t, &a->list, &id) ; + } + return 1 ; +} + +int ftrigr_updateb (ftrigr_t *a) +{ + size_t curlen = genalloc_len(uint16_t, &a->list) ; + int r = skaclient_update(&a->connection, &msghandler, a) ; + return r < 0 ? r : (int)(genalloc_len(uint16_t, &a->list) - curlen) ; +} |