summaryrefslogtreecommitdiff
path: root/src/libunixonacid/netstring_timed_get.c
blob: c540a3f75165edc2b19c79c9e88b04df7a74d375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* ISC license. */

#include <errno.h>
#include <skalibs/buffer.h>
#include <skalibs/netstring.h>
#include <skalibs/iopause.h>
#include <skalibs/unix-timed.h>

int netstring_timed_get (buffer *b, stralloc *sa, tain_t const *deadline, tain_t *stamp)
{
  iopause_fd x = { .fd = buffer_fd(b), .events = IOPAUSE_READ } ;
  size_t w = 0 ;
  for (;;)
  {
    int r = netstring_get(b, sa, &w) ;
    if (r > 0) return r ;
    if (r < 0) return 0 ;
    r = iopause_stamp(&x, 1, deadline, stamp) ;
    if (r < 0) return 0 ;
    else if (!r) return (errno = ETIMEDOUT, 0) ;
  }
}