Use PING command to detect stale connection to server
This is a very simple PING/PONG implementation for detecting stale server connections. PINGS will be send 120 seconds after the last user/server traffic arrived. As PONG counts everything that comes back from the server.
This commit is contained in:
parent
09eef156ab
commit
6da9075130
33
ii.c
33
ii.c
@ -38,6 +38,8 @@ struct Channel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static int irc;
|
static int irc;
|
||||||
|
#define PING_TIMEOUT 300
|
||||||
|
static time_t last_response;
|
||||||
static Channel *channels = nil;
|
static Channel *channels = nil;
|
||||||
static char *host = "irc.freenode.net";
|
static char *host = "irc.freenode.net";
|
||||||
static char nick[32]; /* might change while running */
|
static char nick[32]; /* might change while running */
|
||||||
@ -423,7 +425,10 @@ static void run()
|
|||||||
Channel *c;
|
Channel *c;
|
||||||
int r, maxfd;
|
int r, maxfd;
|
||||||
fd_set rd;
|
fd_set rd;
|
||||||
|
struct timeval tv;
|
||||||
|
char ping_msg[512];
|
||||||
|
|
||||||
|
snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host);
|
||||||
for(;;) {
|
for(;;) {
|
||||||
FD_ZERO(&rd);
|
FD_ZERO(&rd);
|
||||||
maxfd = irc;
|
maxfd = irc;
|
||||||
@ -434,19 +439,29 @@ static void run()
|
|||||||
FD_SET(c->fd, &rd);
|
FD_SET(c->fd, &rd);
|
||||||
}
|
}
|
||||||
|
|
||||||
r = select(maxfd + 1, &rd, 0, 0, 0);
|
tv.tv_sec = 120;
|
||||||
if(r == -1 && errno == EINTR)
|
tv.tv_usec = 0;
|
||||||
continue;
|
r = select(maxfd + 1, &rd, 0, 0, &tv);
|
||||||
if(r < 0) {
|
if(r < 0) {
|
||||||
|
if(errno == EINTR)
|
||||||
|
continue;
|
||||||
perror("ii: error on select()");
|
perror("ii: error on select()");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
} else if(r > 0) {
|
} else if(r == 0) {
|
||||||
if(FD_ISSET(irc, &rd))
|
if(time(NULL) - last_response >= PING_TIMEOUT) {
|
||||||
handle_server_output();
|
print_out(NULL, "-!- ii shutting down: ping timeout");
|
||||||
for(c = channels; c; c = c->next)
|
exit(EXIT_FAILURE);
|
||||||
if(FD_ISSET(c->fd, &rd))
|
}
|
||||||
handle_channels_input(c);
|
write(irc, ping_msg, strlen(ping_msg));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
if(FD_ISSET(irc, &rd)) {
|
||||||
|
handle_server_output();
|
||||||
|
last_response = time(NULL);
|
||||||
|
}
|
||||||
|
for(c = channels; c; c = c->next)
|
||||||
|
if(FD_ISSET(c->fd, &rd))
|
||||||
|
handle_channels_input(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user