Re: buffer usage

From: Laurent Bercot <ska-skaware_at_skarnet.org>
Date: Wed, 24 Aug 2016 11:23:28 +0200

> static int listener_read(int fd, siovec_t const *io, unsigned int nb)

  You should not be using a siovec_t and fiddling with fd_readsv if your
goal is to use the buffer.h API. siovec_t is used in the *implementation*
of the buffer primitives (because the buffer.h buffers are now circular)
but that should be entirely transparent to you.

  In order to use buffer.h to read stuff in an asynchronous loop, the
primitive you want is buffer_getall().

char buf[n] ;
unsigned int w = 0 ;
for (;;)
{
   ... iopause that checks readability on fd ...
   int r = buffer_getall(fd, buf, n, &w) ;
   if (r < 0) { /* EOF if errno == EPIPE, error otherwise /* }
   else if (!r) { /* no data on fd after all, go do something else */
   else { /* buf is now filled with n bytes of data, do your job */ }
}

  buf is asynchronously filled across loop iterations (state is kept
in the w variable), and when buffer_getall returns a positive number,
you have all the n bytes of data you asked for, and *now* you can call
your callback function. Just give it buf and n as arguments, a
char const * and an unsigned int: a regular chunk of memory.

  Let the buffer primitives do the low-level work, that's what they're here
for.

-- 
  Laurent
Received on Wed Aug 24 2016 - 09:23:28 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:38:49 UTC