From 9b60fa91f075f1b8dba0ade9bf4c85c0e33ccf0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Mon, 10 Aug 2009 21:44:41 +0200 Subject: [PATCH] More fixes for network code (slaves don't block anymore) --- game.c | 25 +++++++------ network.c | 106 +++++++++++++++++++++++++++--------------------------- 2 files changed, 68 insertions(+), 63 deletions(-) diff --git a/game.c b/game.c index 22702a2..ac0cbd1 100644 --- a/game.c +++ b/game.c @@ -836,7 +836,7 @@ static int getaction(void) { static int iterate(void) { int i; - static int deathcount=0; + static int deathcount = 0; mypause(); scaninput(); @@ -845,9 +845,9 @@ static int iterate(void) { clearspritelist(); gfxunlock(); - myaction=getaction(); - if(NETWORK_NONE == network && myaction==ACT_QUIT) return CODE_QUIT; - i=networktraffic(); + myaction = getaction(); + if (NETWORK_NONE == network && myaction==ACT_QUIT) return CODE_QUIT; + i = networktraffic(); if(i<0) gountil=mycount+1; else @@ -855,12 +855,12 @@ static int iterate(void) { while(mycount=ACTIONHIST) // too far behind - return CODE_QUIT; - memcpy(actions,actionblock+i*MAXNETNODES,MAXNETNODES); - if(actions[myslot]==ACT_QUIT) return CODE_QUIT; + if (NETWORK_NONE != network) { + i = gountil - mycount; + if(i >= ACTIONHIST) // too far behind + goto leave_game; + memcpy(actions, actionblock+i*MAXNETNODES, MAXNETNODES); + if (actions[myslot] == ACT_QUIT) return CODE_QUIT; } processbombs(); dodetonations(); @@ -907,6 +907,11 @@ static int iterate(void) { deathcount=0; } return CODE_CONT; + +leave_game: /* client disconnect/timeout: send ACT_QUIT to master */ + myaction = ACT_QUIT; + networktraffic(); + return CODE_QUIT; } void set_game_options(GameOptions *options) { diff --git a/network.c b/network.c index 88196a2..6248d2c 100644 --- a/network.c +++ b/network.c @@ -291,66 +291,66 @@ int networktraffic(void) { case NETWORK_MASTER: memcpy(actions,latestactions,MAXNETNODES); actions[0]=myaction; - now=gtime(); - for(;;) { - if(gtime()-now>15) break; - length=getmsg(5); - if(length>0 && *mesg!=PKT_MYDATA) fprintf(stderr, "Strange packet %d\n", (int) *mesg); -// check for unexpected old packets... -// for example JOIN on frame 0, respond with BEGIN if player already in game -// respond with uninvite INVITE on JOIN from others - if(length<10) continue; - whosent = isvalidmsg_from_slave(); - if(whosent<=0) continue; - count=readuint32(mesg+5); - if(count>latestcounts[whosent]) { - latestcounts[whosent]=count; - actions[whosent]=mesg[9]; + if (myaction == ACT_QUIT) { + for (i = 1; i < MAXNETNODES; ++i) { + if (netnodes[i].used) + actions[i]=ACT_QUIT; + } + } else { + now=gtime(); + for(;;) { + if(gtime()-now>15) break; + length=getmsg(5); + if(length>0 && *mesg!=PKT_MYDATA) fprintf(stderr, "Strange packet %d\n", (int) *mesg); + /* check for unexpected old packets... + * for example JOIN on frame 0, respond with BEGIN if player already in game + * respond with uninvite INVITE on JOIN from others + */ + if (length < 10) continue; + whosent = isvalidmsg_from_slave(); + if (whosent <= 0) continue; + count = readuint32(mesg+5); + if (count > latestcounts[whosent]) { + latestcounts[whosent] = count; + actions[whosent] = mesg[9]; + } } } - if(myaction==ACT_QUIT) { - for(i=1;i=1) - { - lastsent=gtime(); - sendmine(mydatacount); - } - */ - if(latest>=0 && gtime()-now>3) break; - length=getmsg(3); - if(length==MAXNETNODES*ACTIONHIST+9 && - sender.sin_addr.s_addr==mastername.sin_addr.s_addr && - sender.sin_port==mastername.sin_port) { - i=readuint32(mesg+5); - if(i= 0 && cur - now > 3) break; + if (exitflag || cur - now > 30) break; + + length = getmsg(count >= 0 ? 3 : 20); + + if (MAXNETNODES*ACTIONHIST+9 != length) continue; + if (!isvalidmsg_from_master()) continue; + + i = readuint32(mesg+5); + if (i < actioncount) continue; + count = actioncount = i; + memcpy(actionblock,mesg+9,MAXNETNODES*ACTIONHIST); } + return actioncount; } return -1; }