Skip to content

Table Management By Dummies

March 14, 2015

To track the games of multiple players I have a table of 32 entries. Each entry carries an ip address and the secret number that ip is trying to guess. For each transaction, I read the IP address of the incoming session and scan the table to find its slot. If I don’t find a slot I assign a new one sequentially, eventually wrapping around the table. This has the possibility of overwriting a table entry of someone who’s actively playing but it works and i haven’t thought of a better plan that i believe i can code.

To actually test this, I cut the number of slots to 7 and hit the server from all the IP addresses I could manage. The table wrap/overrun worked as coded so Yay but it may be a bit worse than I had envisioned. Say we fill the table and then wrap around. If the IP that was in the first slot comes back, he will for sure overwrite another player’s data(I do detect when someone’s data is lost and start a new game so it’s not fatal). I’m not sure how this will work in practice.

The first screenshot below shows the table full the ip ending in 191 is in slot 0. In the second image 191 has been overwritten by 116.  In the third image 191 has come back and overwritten another entry.
15-03-14 pre-overrun15-03-14 post-overrun15-03-14 overrun-again

//14-07-22 reduced to a single routine to find a slot or make one
int getipslot(union IPaddr whichip){//see if this ip is registered and register it if not
	int i=0, tslot;
	while(whichip.l!=regip[i].l && i<numslots){
		i++;
	}
	if (i<numslots){
		return i;
	}else{
		tslot=nextslot++;
		if (nextslot>=numslots){
			nextslot=0;
		}
		regip[tslot]=thisip; //register the ip
		strcpy((char*)secrets[tslot],"999");//supply a dummy secret on initialization
		ipgames[tslot]=0; ipturns[tslot]=0;//reset stats
		return tslot;
	}
}

Advertisements

From → web server

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: