ii

git clone https://orangeshoelaces.net/git/ii.git

6da90751305682b48f4412f2e132d2e930029e4c

Author: kai@dings.kai on 03/16/2006

Committer: kai@dings.kai on 03/16/2006

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.

Stats

ii.c | 33 ++++++--
1 file changed, 24 insertions(+), 9 deletions(-)

Patch

diff --git a/ii.c b/ii.c
index 247d138..4e12138 100644
--- a/ii.c
+++ b/ii.c
@@ -38,6 +38,8 @@ struct Channel {
 };
 
 static int irc;
+#define PING_TIMEOUT 300
+static time_t last_response;
 static Channel *channels = nil;
 static char *host = "irc.freenode.net";
 static char nick[32];			/* might change while running */
@@ -423,7 +425,10 @@ static void run()
 	Channel *c;
 	int r, maxfd;
 	fd_set rd;
+	struct timeval tv;
+	char ping_msg[512];
 
+	snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host);
 	for(;;) {
 		FD_ZERO(&rd);
 		maxfd = irc;
@@ -434,19 +439,29 @@ static void run()
 			FD_SET(c->fd, &rd);
 		}
 
-		r = select(maxfd + 1, &rd, 0, 0, 0);
-		if(r == -1 && errno == EINTR)
-			continue;
+		tv.tv_sec = 120;
+		tv.tv_usec = 0;
+		r = select(maxfd + 1, &rd, 0, 0, &tv);
 		if(r < 0) {
+			if(errno == EINTR)
+				continue;
 			perror("ii: error on select()");
 			exit(EXIT_FAILURE);
-		} else if(r > 0) {
-			if(FD_ISSET(irc, &rd))
-				handle_server_output();
-			for(c = channels; c; c = c->next)
-				if(FD_ISSET(c->fd, &rd))
-					handle_channels_input(c);
+		} else if(r == 0) {
+			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();
+			last_response = time(NULL);
 		}
+		for(c = channels; c; c = c->next)
+			if(FD_ISSET(c->fd, &rd))
+				handle_channels_input(c);
 	}
 }