-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockwrap_timeout.c
More file actions
91 lines (81 loc) · 1.9 KB
/
sockwrap_timeout.c
File metadata and controls
91 lines (81 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Sviluppato da Enrico Masala <masala@polito.it> , Mar 2016
*/
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include "errlib.h"
#include "sockwrap.h"
/* reads exactly "n" bytes from a descriptor, waiting a maximum amount of time */
ssize_t readn_timeout (int fd, void *vptr, size_t n, struct timeval *p_tv)
{
size_t nleft;
ssize_t nread;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0)
{
fd_set rset;
FD_ZERO(&rset);
FD_SET(fd, &rset);
if (select(fd+1, &rset, NULL, NULL, p_tv) > 0) {
if ( (nread = read(fd, ptr, nleft)) < 0)
{
if (INTERRUPTED_BY_SIGNAL)
{
nread = 0;
continue; /* and call read() again */
}
else
return -1;
}
else
if (nread == 0)
break; /* EOF */
nleft -= nread;
ptr += nread;
} else {
p_tv->tv_sec = -1; // Set < 0 to detect timeout by caller
p_tv->tv_usec = 0;
return n - nleft; // timeout by caller is detected by checking the remaining time in p_tv
}
}
return n - nleft;
}
/* reads a line (not beyond the \n), waiting a maximum amount of time */
ssize_t readline_unbuffered_timeout (int fd, void *vptr, size_t maxlen, struct timeval *p_tv)
{
int n, rc;
char c, *ptr;
ptr = vptr;
for (n=1; n<maxlen; n++)
{
fd_set rset;
FD_ZERO(&rset);
FD_SET(fd, &rset);
if (select(fd+1, &rset, NULL, NULL, p_tv) > 0) {
if ( (rc = recv(fd,&c,1,0)) == 1)
{
*ptr++ = c;
if (c == '\n')
break; /* newline is stored, like fgets() */
}
else if (rc == 0)
{
if (n == 1)
return 0; /* EOF, no data read */
else
break; /* EOF, some data was read */
}
else
return -1; /* error, errno set by read() */
} else {
p_tv->tv_sec = -1; // Set < 0 to detect timeout by caller
p_tv->tv_usec = 0;
return n; // timeout by caller is detected by checking the remaining time in p_tv
}
}
*ptr = 0; /* null terminate like fgets() */
return n;
}