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:
kai@dings.kai 2006-03-16 16:54:00 +02:00
parent 09eef156ab
commit 6da9075130

27
ii.c
View File

@ -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,21 +439,31 @@ 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) {
print_out(NULL, "-!- ii shutting down: ping timeout");
exit(EXIT_FAILURE);
}
write(irc, ping_msg, strlen(ping_msg));
continue;
}
if(FD_ISSET(irc, &rd)) {
handle_server_output(); handle_server_output();
last_response = time(NULL);
}
for(c = channels; c; c = c->next) for(c = channels; c; c = c->next)
if(FD_ISSET(c->fd, &rd)) if(FD_ISSET(c->fd, &rd))
handle_channels_input(c); handle_channels_input(c);
} }
} }
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {