comparison rlgwebd.js @ 49:423ef87ddc9b

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.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 11 Jun 2012 09:15:33 -0700
parents 27b7f0c8b9f0
children 96815eae4ebe
comparison
equal deleted inserted replaced
48:33bdb3d466e3 49:423ef87ddc9b
70 } 70 }
71 this.player = String(user); 71 this.player = String(user);
72 this.key = lkey; 72 this.key = lkey;
73 /* This order seems to best avoid race conditions... */ 73 /* This order seems to best avoid race conditions... */
74 this.alive = false; 74 this.alive = false;
75 // A kludge until TermSession is rewritten to handle real watching.
76 this.sendq = false;
75 this.sessid = randkey(2); 77 this.sessid = randkey(2);
76 while (this.sessid in sessions) { 78 while (this.sessid in sessions) {
77 this.sessid = randkey(2); 79 this.sessid = randkey(2);
78 } 80 }
79 /* Grab a spot in the sessions table. */ 81 /* Grab a spot in the sessions table. */
216 /* Call this when the child is dead. */ 218 /* Call this when the child is dead. */
217 if (ss.alive) 219 if (ss.alive)
218 return; 220 return;
219 ss.record.end(); 221 ss.record.end();
220 /* Give the client a chance to read any leftover data. */ 222 /* Give the client a chance to read any leftover data. */
221 if (ss.data.length > 0) 223 if (ss.data.length > 0 || !ss.sendq)
222 setTimeout(ss.remove, 8000); 224 setTimeout(ss.remove, 8000);
223 else 225 else
224 ss.remove(); 226 ss.remove();
225 }; 227 };
226 this.remove = function () { 228 this.remove = function () {
580 term.close(); 582 term.close();
581 var resheaders = {'Content-Type': 'application/json'}; 583 var resheaders = {'Content-Type': 'application/json'};
582 res.writeHead(200, resheaders); 584 res.writeHead(200, resheaders);
583 res.write(JSON.stringify({"t": "q"})); 585 res.write(JSON.stringify({"t": "q"}));
584 res.end(); 586 res.end();
587 term.sendq = true;
585 return; 588 return;
586 } 589 }
587 590
588 function findTermSession(formdata) { 591 function findTermSession(formdata) {
589 if (typeof(formdata) != "object") 592 if (typeof(formdata) != "object")
651 var result = term.read(); 654 var result = term.read();
652 if (result == null) { 655 if (result == null) {
653 if (term.alive) 656 if (term.alive)
654 reply.t = "n"; 657 reply.t = "n";
655 else { 658 else {
656 if (allowlogin) 659 if (allowlogin) {
657 reply.t = "q"; 660 reply.t = "q";
661 term.sendq = true;
662 }
658 else { 663 else {
659 sendError(res, 6, null); 664 sendError(res, 6, null);
660 return; 665 return;
661 } 666 }
662 } 667 }