# HG changeset patch
# User John "Elwin" Edwards <elwin@sdf.org>
# Date 1339431333 25200
# Node ID 423ef87ddc9ba7c92023c4611dbd0c48d1f5cfe3
# Parent  33bdb3d466e32d80630145a018f60aa52f22513e
RLG-Web: delay removing TermSessions until the client is informed.

Add a TermSession.sendq flag that indicates whether a type q message
has been sent to the client.  Don't immediately destroy the TermSession
on child exit if the message hasn't been sent.

This is an ugly hack until the TermSession class is separated into an
EventEmitter to handle the PTY and a listening object that handles
communication with the client.  That will also allow other clients to
watch the game.

diff -r 33bdb3d466e3 -r 423ef87ddc9b rlgwebd.js
--- a/rlgwebd.js	Sat Jun 09 21:42:24 2012 -0700
+++ b/rlgwebd.js	Mon Jun 11 09:15:33 2012 -0700
@@ -72,6 +72,8 @@
   this.key = lkey;
   /* This order seems to best avoid race conditions... */
   this.alive = false;
+  // A kludge until TermSession is rewritten to handle real watching.
+  this.sendq = false;
   this.sessid = randkey(2);
   while (this.sessid in sessions) {
     this.sessid = randkey(2);
@@ -218,7 +220,7 @@
       return;
     ss.record.end();
     /* Give the client a chance to read any leftover data. */
-    if (ss.data.length > 0)
+    if (ss.data.length > 0 || !ss.sendq)
       setTimeout(ss.remove, 8000);
     else
       ss.remove();
@@ -582,6 +584,7 @@
   res.writeHead(200, resheaders);
   res.write(JSON.stringify({"t": "q"}));
   res.end();
+  term.sendq = true;
   return;
 }
 
@@ -653,8 +656,10 @@
       if (term.alive)
         reply.t = "n";
       else {
-        if (allowlogin)
+        if (allowlogin) {
           reply.t = "q";
+          term.sendq = true;
+        }
         else {
           sendError(res, 6, null);
           return;