summaryrefslogtreecommitdiff
path: root/clientd.c
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-08-02 18:19:21 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-08-02 18:21:15 +0200
commit134bec17a10030fc7d1b2060b57bf19799f6c780 (patch)
tree287deb14de65bd1676e04aa6bd40b72ef785c6af /clientd.c
parent8ff7ed58020b46d0bcb3b6dcbc0c5b02e85275a8 (diff)
Proper event-driven icmpd recv
Diffstat (limited to 'clientd.c')
-rw-r--r--clientd.c29
1 files changed, 18 insertions, 11 deletions
diff --git a/clientd.c b/clientd.c
index a5e4d75..2462669 100644
--- a/clientd.c
+++ b/clientd.c
@@ -15,35 +15,42 @@
int main(void) {
struct icmpd *d = icmpd_create_client(inet_addr("127.0.0.1"));
+ int d_fd = icmpd_get_select_fd(d);
while (true) {
- if (icmpd_peek(d)) {
- struct icmpd_received msg = icmpd_recv(d);
- printf("Recv: %zu\n", msg.length);
- xxd(msg.data, msg.length);
- }
-
fd_set inset;
FD_ZERO(&inset);
FD_SET(0, &inset);
- struct timeval tv;
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- int ret = select(1, &inset, NULL, NULL, &tv);
+ FD_SET(d_fd, &inset);
+
+ int ret = select(d_fd + 1, &inset, NULL, NULL, NULL);
if (ret < 0) {
if (errno == EINTR) continue;
perror("select");
return 1;
}
+
if (ret == 0) continue; // timeout
+
if (FD_ISSET(0, &inset)) {
char *line = NULL;
size_t linelen = 0;
+ errno = 0;
ssize_t nr = getline(&line, &linelen, stdin);
- assert(nr >= 0);
+ if (nr < 0) {
+ if (errno == 0) break; // EOF
+ perror("getline");
+ exit(1);
+ }
if (nr > 0) nr--;
icmpd_send(d, line, nr);
free(line);
}
+
+ if (FD_ISSET(d_fd, &inset) && icmpd_peek(d)) {
+ struct icmpd_received msg = icmpd_recv(d);
+ printf("Recv: %zu\n", msg.length);
+ xxd(msg.data, msg.length);
+ }
}
}