shithub: irc.myr

Download patch

ref: d993f492a7374c719ec879c0659e3224ec1b7fd3
parent: 0934d75d1b09f18d953cbd819c86bc88f01a1753
author: Ori Bernstein <ori@markovcorp.com>
date: Fri Mar 9 06:50:13 EST 2018

Ping the server every now and then.

	If we lose the connection, we should reconnect.

--- a/irc.myr
+++ b/irc.myr
@@ -20,6 +20,7 @@
 	const cmd	: (irc : irc#, ln : byte[:] -> void)
 	const getstr	: (chars : char[:] -> byte[:])
 	const puthist	: (irc : irc#, chan : chan#, ent : (std.time, hist) -> void)
+	const ping	: (irc : irc#, srv : server# -> void)
 ;;
 
 const io = {irc, fd
@@ -29,9 +30,11 @@
 	srv = fd2srv(irc, fd)
 
 	match std.read(srv.fd, srv.buf[srv.nbuf:])
-	| `std.Ok 0:	closed(irc, srv, "eof")
-	| `std.Ok n:	srv.nbuf += n
 	| `std.Err e:	closed(irc, srv, "error reading")
+	| `std.Ok 0:	closed(irc, srv, "eof")
+	| `std.Ok n:
+		srv.nbuf += n
+		srv.pong = std.now()
 	;;
 
 	while true
@@ -66,10 +69,17 @@
 		| "PING":	send(irc, srv, "PONG :{}\r\n", args[0])
 		| "JOIN":	joined(irc, srv, src, args)
 		| "NICK":	renamed(irc, srv, src, args)
+		| "PONG":	/* ignore; we update the time in the read */
 		| c:		status(irc, irc.self, "unknown server command {}", ln)
 		;;
 		std.slfree(args)
 		std.slfree(ln)
+	;;
+}
+
+const ping = {irc, srv
+	if srv.pong - std.now() > 60*std.Sec
+		send(irc, srv, "PING irc.myr")
 	;;
 }
 
--- a/main.myr
+++ b/main.myr
@@ -87,7 +87,7 @@
 			.revents=0,
 		])
 	;;
-	if sys.poll(pfd, -1) >= 0
+	if sys.poll(pfd, 60_000) >= 0
 		/* randomize so that we try to cover all fds evenly */
 		start = std.rand(0, pfd.len)
 		for var j = 0; j != pfd.len; j++
@@ -96,6 +96,9 @@
 				fd = (pfd[i].fd : std.fd)
 				out = false
 			;;
+		;;
+		for s : irc.srv
+			ping(irc, s)
 		;;
 	;;
 	std.slfree(pfd)
--- a/types.myr
+++ b/types.myr
@@ -31,6 +31,7 @@
 		chan	: chan#[:]
 		focus	: std.size
 		death	: std.time
+		pong	: std.time
 	;;
 
 	type chan = struct
--