More fixes for network code (slaves don't block anymore)

This commit is contained in:
Stefan Bühler 2009-08-10 21:44:41 +02:00
parent 36a55544f9
commit 9b60fa91f0
2 changed files with 68 additions and 63 deletions

25
game.c
View File

@ -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<gountil) {
++mycount;
if(NETWORK_NONE != network) {
i=gountil-mycount;
if(i>=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) {

View File

@ -291,66 +291,66 @@ int networktraffic(void) {
case NETWORK_MASTER:
memcpy(actions,latestactions,MAXNETNODES);
actions[0]=myaction;
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;
/* 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 (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;
}
addactions();
for(i=1;i<MAXNETNODES;++i)
if(netnodes[i].used)
sendactions(i);
for(i=1;i<MAXNETNODES;++i)
if(netnodes[i].used && actions[i]==ACT_QUIT)
netnodes[i].used=0;
addactions(); /* update action history block */
for (i = 1; i < MAXNETNODES; ++i) {
if(netnodes[i].used) {
sendactions(i); /* send actions to every active node */
if (actions[i]==ACT_QUIT)
netnodes[i].used=0; /* remove disconnected clients */
}
}
return actioncount;
case NETWORK_SLAVE:
{
long latest=-1;
long lastsent;
count = -1; /* set to actioncount if we got at least one packet */
now = gtime();
lastsent=now=gtime();
++mydatacount;
sendmine(mydatacount);
for(;;) {
/*
if(gtime()-lastsent>=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<latest) continue;
latest=i;
memmove(actionblock,mesg+9,MAXNETNODES*ACTIONHIST);
}
}
return latest;
/* if we got already one packet we only wait 3msec, otherwise 30msec */
long cur = gtime();
if (count >= 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;
}