aboutsummaryrefslogtreecommitdiffstats
path: root/src/libunixonacid
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2025-03-01 08:51:50 +0000
committerLaurent Bercot <ska@appnovation.com>2025-03-01 08:51:50 +0000
commit9c781a2be58c60101aa96a9606117693204cc1a4 (patch)
tree6738cfa96b6f015444246f016920df6581f37477 /src/libunixonacid
parentb6e920192eee90fa2d403a6616129149c8daba97 (diff)
downloadskalibs-9c781a2be58c60101aa96a9606117693204cc1a4.tar.gz
Add timed_read[v], buffer_timed_[get|put]v
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libunixonacid')
-rw-r--r--src/libunixonacid/buffer_timed_getv.c32
-rw-r--r--src/libunixonacid/buffer_timed_putv.c22
-rw-r--r--src/libunixonacid/timed_read.c34
-rw-r--r--src/libunixonacid/timed_readv.c38
4 files changed, 126 insertions, 0 deletions
diff --git a/src/libunixonacid/buffer_timed_getv.c b/src/libunixonacid/buffer_timed_getv.c
new file mode 100644
index 0000000..ee1de79
--- /dev/null
+++ b/src/libunixonacid/buffer_timed_getv.c
@@ -0,0 +1,32 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+
+#include <skalibs/functypes.h>
+#include <skalibs/buffer.h>
+#include <skalibs/unix-timed.h>
+
+struct blah_s
+{
+ buffer *b ;
+ struct iovec *v ;
+ unsigned int n ;
+ size_t w ;
+} ;
+
+static int getfd (struct blah_s *blah)
+{
+ return buffer_fd(blah->b) ;
+}
+
+static ssize_t get (struct blah_s *blah)
+{
+ return buffer_getvall(blah->b, blah->v, blah->n, &blah->w) ;
+}
+
+size_t buffer_timed_getv (buffer *b, struct iovec *v, unsigned int n, tain const *deadline, tain *stamp)
+{
+ struct blah_s blah = { .b = b, .v = v, .n = n, .w = 0 } ;
+ timed_get(&blah, (init_func_ref)&getfd, (get_func_ref)&get, deadline, stamp) ;
+ return blah.w ;
+}
diff --git a/src/libunixonacid/buffer_timed_putv.c b/src/libunixonacid/buffer_timed_putv.c
new file mode 100644
index 0000000..0dfd56a
--- /dev/null
+++ b/src/libunixonacid/buffer_timed_putv.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+
+#include <skalibs/buffer.h>
+#include <skalibs/siovec.h>
+#include <skalibs/unix-timed.h>
+
+size_t buffer_timed_putv (buffer *b, struct iovec const *v, unsigned int vlen, tain const *deadline, tain *stamp)
+{
+ if (!vlen) return 0 ;
+ size_t tot = siovec_len(v, vlen), w = 0 ;
+ struct iovec vv[vlen] ;
+ for (unsigned int i = 0 ; i < vlen ; i++) vv[i] = v[i] ;
+ while (w < tot)
+ {
+ size_t r = buffer_putvnoflush(b, vv, vlen) ;
+ w += r ; siovec_seek(vv, vlen, r) ;
+ if (!buffer_timed_flush(b, deadline, stamp)) break ;
+ }
+ return w ;
+}
diff --git a/src/libunixonacid/timed_read.c b/src/libunixonacid/timed_read.c
new file mode 100644
index 0000000..0c9a5cf
--- /dev/null
+++ b/src/libunixonacid/timed_read.c
@@ -0,0 +1,34 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/functypes.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/unix-timed.h>
+
+struct blah_s
+{
+ int fd ;
+ char *s ;
+ size_t len ;
+ size_t w ;
+} ;
+
+static int getfd (struct blah_s *blah)
+{
+ return blah->fd ;
+}
+
+static ssize_t get (struct blah_s *blah)
+{
+ ssize_t r = fd_read(blah->fd, blah->s + blah->w, blah->len - blah->w) ;
+ if (r > 0) blah->w += r ;
+ return r ;
+}
+
+size_t timed_read (int fd, char *s, size_t len, tain const *deadline, tain *stamp)
+{
+ struct blah_s blah = { .fd = fd, .s = s, .len = len, .w = 0 } ;
+ timed_get(&blah, (init_func_ref)&getfd, (get_func_ref)&get, deadline, stamp) ;
+ return blah.w ;
+}
diff --git a/src/libunixonacid/timed_readv.c b/src/libunixonacid/timed_readv.c
new file mode 100644
index 0000000..606a70f
--- /dev/null
+++ b/src/libunixonacid/timed_readv.c
@@ -0,0 +1,38 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+
+#include <skalibs/functypes.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/siovec.h>
+#include <skalibs/unix-timed.h>
+
+struct blah_s
+{
+ int fd ;
+ struct iovec *v ;
+ unsigned int vlen ;
+ size_t w ;
+} ;
+
+static int getfd (struct blah_s *blah)
+{
+ return blah->fd ;
+}
+
+static ssize_t get (struct blah_s *blah)
+{
+ ssize_t r = fd_readv(blah->fd, blah->v, blah->vlen) ;
+ if (r > 0) { blah->w += r ; siovec_seek(blah->v, blah->vlen, r) ; }
+ return r ;
+}
+
+size_t timed_readv (int fd, struct iovec *v, unsigned int vlen, tain const *deadline, tain *stamp)
+{
+ if (!vlen || !siovec_len(v, vlen)) return 0 ;
+ struct iovec vv[vlen] ;
+ struct blah_s blah = { .fd = fd, .v = vv, .vlen = vlen, .w = 0 } ;
+ for (unsigned int i = 0 ; i < vlen ; i++) vv[i] = v[i] ;
+ timed_get(&blah, (init_func_ref)&getfd, (get_func_ref)&get, deadline, stamp) ;
+ return blah.w ;
+}