commit d945bca329f7a13741e1d3afcf9f0f73f6c70ce9
parent f7c2e436ed0144d09cff0e3ac90f510a47f0aeac
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Sun, 16 Jul 2017 15:56:05 +0000
Add ftrigr_updateb()
Diffstat:
5 files changed, 113 insertions(+), 61 deletions(-)
diff --git a/doc/libs6/ftrigr.html b/doc/libs6/ftrigr.html
@@ -227,7 +227,7 @@ int ftrigr_fd (ftrigr_t const *a)
</p>
<pre>
-int ftrigr_update (ftrigr_t *a)
+int ftrigr_updateb (ftrigr_t *a)
</pre>
<p>
@@ -239,11 +239,14 @@ which something happened.
</p>
<p>
- When <tt>ftrigr_update</tt> returns,
+ When <tt>ftrigr_updateb</tt> returns,
<tt>genalloc_s(uint16_t, &a->list)</tt> points to an array of
<tt>genalloc_len(uint16_t, &a->list)</tt> 16-bit unsigned
integers. Those integers are ids waiting to be passed to
-<tt>ftrigr_check</tt>.
+<tt>ftrigr_check</tt> or <tt>ftrigr_checksa</tt>.
+The number of ids already acknowledged is stored in
+<tt>a->head</tt>, so the first unacknowledged id is
+<tt>genalloc_s(uint16_t, &a->list)[a->head]</tt>.
</p>
<pre>
@@ -252,7 +255,7 @@ int ftrigr_check (ftrigr_t *a, uint16_t id, char *what)
<p>
Checks whether an event happened to <em>id</em>. Use after a
-call to <tt>ftrigr_update()</tt>.
+call to <tt>ftrigr_updateb()</tt>.
</p>
<ul>
@@ -287,5 +290,23 @@ to <tt>ftrigr_check()</tt>. Each character is the one that triggered
a notification. The function then returns 1. </li>
</ul>
+<pre>
+int ftrigr_ack (ftrigr_t *a, size_t n)
+</pre>
+
+<p>
+ Acknowledges reading <em>n</em> ids from the id list updated by
+<tt>ftrigr_updateb</tt>.
+</p>
+
+<pre>
+int ftrigr_update (ftrigr_t *a)
+</pre>
+
+<p>
+ Acknowledges all the pending ids (i.e. clears the stored id list)
+then calls <tt>ftrigr_updateb()</tt>.
+</p>
+
</body>
</html>
diff --git 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
@@ -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
@@ -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
@@ -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) ;
+}