summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorJesper Jensen <jesper@slashwin.dk>2021-10-02 00:25:37 +0200
committerJesper Jensen <jesper@slashwin.dk>2021-10-02 00:26:52 +0200
commite3eb0d2eda0bb8c4decf9bc56b025157e70387c2 (patch)
tree5e2594399953f01e2725546193d9ed84c4527655 /src/main.c
parentc46781095ec64fbc4f8cf6097ca70b594a68c583 (diff)
Add timeout handling
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index 7534e76..5094c8c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -35,12 +35,27 @@ int main(int argc, char** argv) {
bool timedout = false;
if(!dht.pause){
+ time_t next = 0;
+
struct entry* oldest;
routing_oldest(&oldest);
- if(oldest != NULL) {
- dbg("Set timeout to %ld", oldest->expire - time(NULL));
+ if(oldest != NULL)
+ next = oldest->expire;
+
+ for(int i = 0; i < MAX_INFLIGHT; i++) {
+ if(!dht.reqalloc[i])
+ continue;
+
+ time_t timeout = dht.requestdata[i].timeout;
+ if(next == 0 || (timeout != 0 && difftime(timeout, next) < 0))
+ next = timeout;
+ }
+
+ if(next != 0) {
+ time_t sleepfor = next - time(NULL);
+ dbg("Set timeout to %ld", sleepfor);
struct timeval tv = {
- .tv_sec = oldest->expire - time(NULL),
+ .tv_sec = sleepfor,
.tv_usec = 0,
};
if(tv.tv_sec <= 0) {
@@ -48,7 +63,10 @@ int main(int argc, char** argv) {
} else {
setsockopt(dht.sfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
}
+ } else {
+ fatal("We have to wait some amount of time right now");
}
+
} else {
printf("DHT timeout is paused");
}