Re: buffer usage

From: Vincent de RIBOU via skaware <skaware_at_list.skarnet.org>
Date: Wed, 24 Aug 2016 09:39:22 +0000 (UTC)

Laurent,
maybe I was not clear enough.
in my buffer CB, set at init, I used 'fd_readsv' instead of my 'listener_read' CB and it works.but it uses 'readv' instead of my 'recvfrom' syscall. not so painfull but in this way I can't know the ID of peer.

furthermore, the part of code you mentionned is exactly what 'buffer_get' does. Why reimplmeenting the same thing?

what your advice?regards, Vincent de RIBOU région Ouest - France belzo2005-dolphin_at_yahoo.fr

    Le Mercredi 24 août 2016 11h23, Laurent Bercot <ska-skaware_at_skarnet.org> a écrit :
 

> 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:39:22 UTC

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