skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

socket_ioloop.c (756B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 
      5 #include <skalibs/error.h>
      6 #include <skalibs/iopause.h>
      7 #include <skalibs/socket.h>
      8 
      9 ssize_t socket_ioloop (int s, char *buf, size_t len, char *ip, uint16_t *port, socket_io_func_ref f, int w, tain const *deadline, tain *stamp)
     10 {
     11   iopause_fd x = { .fd = s, .events = w ? IOPAUSE_WRITE : IOPAUSE_READ, .revents = 0 } ;
     12   for (;;)
     13   {
     14     ssize_t r = iopause_stamp(&x, 1, deadline, stamp) ;
     15     if (r < 0) return -1 ;
     16     if (!r) return (errno = ETIMEDOUT, -1) ;
     17     if (x.revents & IOPAUSE_EXCEPT) return (errno = EIO, -1) ;
     18     if (x.revents & x.events)
     19     {
     20       r = (*f)(s, buf, len, ip, port) ;
     21       if (r < 0)
     22       {
     23         if (!error_isagain(errno)) return -1 ;
     24       }
     25       else return r ;
     26     }
     27   }
     28 }
     29