Skip to content

Commit

Permalink
timeout when waiting for new instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
justanhduc committed Feb 15, 2022
1 parent 2d10f68 commit 54b2145
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,12 @@ static void server_loop(int ls) {
int maxfd;
int keep_loop = 1;
int newjob;
struct timeval tv;
int res;

/* wait up to 30 secs before checking whether a job can run */
tv.tv_sec = 30;
tv.tv_usec = 0;
while (keep_loop) {
FD_ZERO(&readset);
maxfd = 0;
Expand All @@ -248,28 +253,30 @@ static void server_loop(int ls) {
if (client_cs[i].socket > maxfd)
maxfd = client_cs[i].socket;
}
select(maxfd + 1, &readset, NULL, NULL, NULL);
if (FD_ISSET(ls, &readset)) {
int cs;
cs = accept(ls, NULL, NULL);
if (cs == -1)
error("Accepting from %i", ls);
client_cs[nconnections].hasjob = 0;
client_cs[nconnections].socket = cs;
++nconnections;
}
for (i = 0; i < nconnections; ++i) {
if (FD_ISSET(client_cs[i].socket, &readset)) {
enum Break b;
b = client_read(i);
/* Check if we should break */
if (b == CLOSE) {
warning("Closing");
/* On unknown message, we close the client,
or it may hang waiting for an answer */
clean_after_client_disappeared(client_cs[i].socket, i);
} else if (b == BREAK)
keep_loop = 0;
res = select(maxfd + 1, &readset, NULL, NULL, &tv);
if (res != -1) {
if (FD_ISSET(ls, &readset)) {
int cs;
cs = accept(ls, NULL, NULL);
if (cs == -1)
error("Accepting from %i", ls);
client_cs[nconnections].hasjob = 0;
client_cs[nconnections].socket = cs;
++nconnections;
}
for (i = 0; i < nconnections; ++i) {
if (FD_ISSET(client_cs[i].socket, &readset)) {
enum Break b;
b = client_read(i);
/* Check if we should break */
if (b == CLOSE) {
warning("Closing");
/* On unknown message, we close the client,
or it may hang waiting for an answer */
clean_after_client_disappeared(client_cs[i].socket, i);
} else if (b == BREAK)
keep_loop = 0;
}
}
}

Expand Down

0 comments on commit 54b2145

Please sign in to comment.