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

#include <errno.h>
#include <skalibs/buffer.h>
#include <skalibs/stralloc.h>
#include <skalibs/netstring.h>
#include <skalibs/tai.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 } ;
  unsigned int w = 0 ;
  for (;;)
  {
    register 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) ;
  }
}