#include "bomber.h" #include "announce.h" #include "game.h" #include "menu.h" #include "network.h" #include "utils.h" #include "draw.h" #define MAXMSG 4096 int udpsocket; const unsigned char gameversion[4]={0xda,0x01,0x00,0x05}; struct netnode netnodes[MAXNETNODES]; static int informsize; static unsigned char regpacket[64]; static struct sockaddr_in myname={0},mastername={0}; static socklen_t senderlength; static struct sockaddr_in sender={0}; static unsigned char mesg[MAXMSG]=""; uchar needwhole=0; int mydatacount; int myslot; network_type network = NETWORK_NONE; int actioncount; unsigned char actionblock[ACTIONHIST*MAXNETNODES]; int myaction; unsigned char actions[MAXNETNODES]; unsigned char latestactions[MAXNETNODES]; long latestcounts[MAXNETNODES]; enum network_packet_types { PKT_ACK, /* perfect copy of packet received */ /* join / host game */ /* slave -> master packets */ PKT_JOIN, /* 4 bytes version #, 4 bytes joinunique #, 16 bytes name */ PKT_QUIT, /* 4 bytes unique # */ /* master -> slave packets */ PKT_INVITE, /* 4 bytes unique #, 1 byte your slot (0xff for kick, no data following it) #, any # of 1:slot,16:name sets (-1 end) */ PKT_BEGIN, /* clone of INVITE */ PKT_CONFIG, /* 4 bytes unique #, config */ PKT_ACCEPT, /* 4 bytes join unique #, 132 bytes seed + unique #, config, slot info (see invite) */ PKT_REJECT, /* 4 bytes join unique #, 4 bytes version #, 1: reason */ /* ingame actions */ /* slave -> master packets */ PKT_MYDATA, /* 4 bytes unique #,4 bytes frame #, 1 byte data */ /* master -> slave packets */ PKT_STEP, /* 4 bytes unique #, 4 bytes frame #, history x MAXNETNODES bytes ACT_* */ PKT_INVALID = 0xff }; enum reject_reason { REJECT_FULL, REJECT_VERSION /* TODO: password? */ }; #define _REJECT_LAST REJECT_VERSION const char *reject_reason_str[] = { "Server full", "Version mismatch" }; /* all bytes stored MSB first */ /* game startup: Master: send REGISTER to matcher with optional password, wait for ack. If timout, resend. matcher: Wait for REGISTER packet, when received maintain database. respond to sender with ACK. REGISTER packet can close a game also. The REGISTER packet sent by the master has a unique word to be used to avoid confusion. REGISTER packet also contains a game version # After master registers game and receives ACK, just waits for slaves to contact. slave: send QUERY to matcher with optional password, wait for INFO, if timeout, resend. matcher: respond to QUERY with INFO packet. matcher need not maintain any database for slave requests. INFO packet contains IP addr and port for each master machine that matches the QUERY spec (ALL or password). Only a certain MAX # of entries are sent if there are too many to choose from. slave: send JOIN to master, wait for INVITE. If timeout, resend. JOIN packet contains the unique word the master created. JOIN also contains username. master: Respond to JOIN with INVITE. INVITE contains unique word from JOIN packet. INVITE either contains NO meaning game no longer exists or is closed or player is not invited. IF yes, INVITE contains info on other players already in the game (username and slot # for each). Master allocates the slots and avoids confusion based on IP addr and port #. INVITE also contains game options structure. Whenever a new player JOINS and is admitted, master sends updated INVITE packets to everyone already in the JOIN list. Whenever master changes game options, master sends out another set of INVITES Duplicate JOINS are answered with updated INVITE but nothing changes as far as allocation. Master player launches game after he's satisfied everyone has joined. Master sends BEGIN packet to everyone. BEGIN is identical to INVITE except that the data is final. Slave must respond with its first MYDATA packet with frame # of 0. If master times out waiting, master sends duplicate BEGIN to wayward slaves. Once master has received MYDATA from everyone, game starts. Within game slave sends MYDATA to master and waits for STEP packet. If timeout, slave sends duplicate MYDATA. If master times out waiting for a slave's MYDATA, slave gets dropped. MYDATAs received will be answered with PKT_QUIT. */ /* Network I/O, building/checking packets */ #if defined (TEST_LATENCY) #define NUMQ 512 struct message { int time; struct sockaddr_in *to; int tosize; unsigned char msg[512]; int len; } message[NUMQ]={0}; outmsgs() { int i; for(i=0;i>24L; p[1]=i>>16L; p[2]=i>>8L; p[3]=i; return p+4; } static Uint32 readuint32(unsigned char *p) { return (p[0]<<24L) | (p[1]<<16L) | (p[2]<<8) | p[3]; } static unsigned char* write_unique(unsigned char *p) { memcpy(p, &network_unique, 4); return p + 4; } static unsigned char* write_version(unsigned char *p) { memcpy(p, &gameversion, 4); return p + 4; } static int isvalidmsg_from_slave() { int i; void *host; void *port; if (!isvalidunique(mesg+1)) return -1; host=&sender.sin_addr.s_addr; port=&sender.sin_port; for(i=1;i15) 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= MAXNETNODES) return 0; netnodes[i].used = 1; memcpy(netnodes[i].name, buf, 16); buf += 16; size -= 16; } *psize = size; *pbuf = buf; return 1; } static void read_config(unsigned char* buf) { GameOptions opts; memset(&opts, 0, sizeof(opts)); opts.density = *buf++; opts.flames = *buf++; opts.bombs = *buf++; opts.generosity = *buf++; set_game_options(&opts); } /* returns 0=ignore packet,1=we're rejected,2=INVITE/CONFIG,3=BEGIN */ int scaninvite(int msec) { int size; unsigned char *take; size = getmsg(msec); if (size < 6) return 0; if (*mesg!=PKT_INVITE && *mesg!=PKT_BEGIN && *mesg!=PKT_CONFIG) return 0; if (!isvalidmsg_from_master()) return 0; if (!isvalidunique(mesg+1)) return 0; take = mesg+5; size -= 5; switch (*mesg) { case PKT_INVITE: case PKT_BEGIN: if (!read_inform(&take, &size)) return 0; if (0xff == myslot) return 1; /* master closed game */ break; case PKT_CONFIG: if (size < 4) return 0; read_config(take); break; } if (*mesg == PKT_BEGIN) { network = NETWORK_SLAVE; return 3; } else { return 2; } } int send_join(struct sockaddr_in *netname, char playername[16]) { int size; long now; Uint32 join_unique = gtime(); unsigned char *buf; mastername = *netname; *regpacket=PKT_JOIN; write_version(regpacket + 1); writeuint32(regpacket+5, join_unique); memcpy(regpacket+9, playername, 16); now=longtime(); putmsg(&mastername,regpacket,1+4+4+16); while(longtime()-now < 3) { if (0 == (size = getmsg(1000))) { /* got no message, send join again */ putmsg(&mastername,regpacket,1+4+4+16); continue; } if (size < 5) continue; if (readuint32(mesg+1) != join_unique) continue; switch (*mesg) { case PKT_ACCEPT: if (size < 1+4+132+4+2) continue; read_seed_unique(mesg + 5); read_config(mesg+137); buf = mesg+141; size -= 141; if (!read_inform(&buf,&size)) return 0; return 2; case PKT_REJECT: if (size < 10 || mesg[9] > _REJECT_LAST) { failure("Couldn't connect"); } else { failure("Couldn't connect: %s", reject_reason_str[mesg[9]]); } return 0; default: break; } } failure("Could not connect - Timeout"); return 0; } void send_quit() { long now; int size; *regpacket = PKT_QUIT; write_unique(regpacket+1); now=longtime(); while(longtime()-now<10) { putmsg(&mastername,regpacket,5); size=getmsg(1000); if(size<6) continue; if(mesg[0] != PKT_ACK || mesg[1] != PKT_QUIT) continue; if (isvalidunique(mesg+2)) break; } }