From d2c0ab7a62b7a2c99c4c491ce55466859429571b Mon Sep 17 00:00:00 2001 From: rodri Date: Fri, 5 May 2023 22:35:14 +0000 Subject: handle player quits gracefully. also fixed a bug whereby a player would never be freed if no party was happening, and another one where deleting a player from the player queue would cause a nil pointer deref. --- musw.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'musw.c') diff --git a/musw.c b/musw.c index 0703393..edd3826 100644 --- a/musw.c +++ b/musw.c @@ -231,6 +231,26 @@ initconn(void) netconn.state = NCSConnecting; } +void +buhbye(void) +{ + Frame *frame; + int i, naptime; + + if(netconn.state != NCSConnected) + return; + + i = 10; + naptime = 2000/i; + while(i--){ + frame = newframe(nil, NCbuhbye, netconn.lastseq+1, 0, 0, nil); + signframe(frame, netconn.dh.priv); + sendp(egress, frame); + + sleep(naptime); + } +} + void sendkeys(ulong kdown) { @@ -273,8 +293,12 @@ kbdproc(void *) } if(buf[0] == 'c'){ if(utfrune(buf, Kdel)){ +defenestrate: close(fd); threadexitsall(nil); + }else if(utfrune(buf, 'q')){ + buhbye(); + goto defenestrate; } } if(buf[0] != 'k' && buf[0] != 'K') @@ -414,6 +438,15 @@ threadnetppu(void *) sendp(egress, newf); + break; + case NSawol: + weplaying = 0; + + newf = newframe(nil, NCawol, frame->seq+1, frame->seq, 0, nil); + signframe(newf, netconn.dh.priv); + + sendp(egress, newf); + break; case NSbuhbye: weplaying = 0; @@ -607,6 +640,8 @@ State *matching_δ(State *s, void*) State *playing_δ(State *s, void*) { + if(!weplaying) + return &gamestates[GSMatching]; return s; } -- cgit v1.2.3