-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_poll.cpp
More file actions
35 lines (32 loc) · 760 Bytes
/
safe_poll.cpp
File metadata and controls
35 lines (32 loc) · 760 Bytes
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
#include "safe_poll.h"
#include <cstdlib>
#include <cerrno>
#include <error.h>
#include <unistd.h>
#include <cstdio>
void try_poll(struct pollfd *fds, nfds_t nfds, int timeout) {
int status = poll(fds, nfds, timeout);
if (status < 0) {
error(EXIT_FAILURE, errno, "Polling failed");
return;
}
if (status == 0) {
// error(EXIT_FAILURE, errno, "Polling finished with timeout %d", timeout);
return;
}
}
int try_read(int fd, void *buf, size_t nbytes) {
ssize_t len = read(fd, buf, nbytes);
if (len == -1) {
//reading failed
error(EXIT_FAILURE, errno, "Error during read from %d\n", fd);
return -1;
}
if (len == 0) {
//socket closed on the other side
close(fd);
fprintf(stderr, "End of stream %d\n", fd);
return 0;
}
return len;
}