X-Git-Url: http://git.shiar.nl/netris.git/blobdiff_plain/b5a9cdfd671a10cd5b5e9f1f53105e9993414247..dad72a6bf82c2f6d9f857499711827e425e1e279:/server.c diff --git a/server.c b/server.c index 7d63e02..c33a541 100644 --- a/server.c +++ b/server.c @@ -15,8 +15,6 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * $Id: game.c,v 1.39 1999/05/16 06:56:27 mhw Exp $ */ #define NOEXT @@ -38,18 +36,18 @@ #define HEADER_SIZE sizeof(netint4[3]) static struct option options[] = { - { "wait", 0, 0, 'w' }, - { "port", 1, 0, 'p' }, - { "quadra", 1, 0, 'q' }, - { "min-players",1, 0, 'm' }, - { "max-players",1, 0, 'x' }, - { "continuous", 1, 0, 'c' }, - { "speed", 1, 0, 'i' }, - { "seed", 1, 0, 's' }, - { "verbose", 0, 0, 'v' }, - { "info", 0, 0, 'H' }, - { "help", 0, 0, 'h' }, - { 0, 0, 0, 0 } + { "wait", 0, 0, 'w' }, + { "port", 1, 0, 'p' }, + { "quadra", 1, 0, 'q' }, + { "min-players", 1, 0, 'm' }, + { "max-players", 1, 0, 'x' }, + { "continuous", 1, 0, 'c' }, + { "speed", 1, 0, 'i' }, + { "seed", 1, 0, 's' }, + { "verbose", 0, 0, 'v' }, + { "info", 0, 0, 'H' }, + { "help", 0, 0, 'h' }, + { 0, 0, 0, 0 } }; static char minplayers = 2; @@ -61,15 +59,18 @@ struct sockaddr_in addr; MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event); static EventGenRec netGen[MAX_SCREENS] = { - { NULL, 0, FT_read, -1, NetGenFunc, EM_net, 0, "\0", 0, HEADER_SIZE } }; + { NULL, 0, FT_read, -1, NetGenFunc, EM_net, 0, "\0", 0, HEADER_SIZE } +}; static MyEventType AlarmGenFunc(EventGenRec *gen, MyEvent *event); -static EventGenRec alarmGen = - { &alarmGen, 0, FT_read, -1, AlarmGenFunc, EM_alarm }; +static EventGenRec alarmGen = { + &alarmGen, 0, FT_read, -1, AlarmGenFunc, EM_alarm +}; static MyEventType ConnGenFunc(EventGenRec *gen, MyEvent *event); -static EventGenRec connGen = - { NULL, 0, FT_read, -1, ConnGenFunc, EM_connect }; +static EventGenRec connGen = { + NULL, 0, FT_read, -1, ConnGenFunc, EM_connect +}; static EventGenRec *nextGen = &alarmGen; @@ -91,12 +92,12 @@ void SendPacketTo(short playa, short uid, NetPacketType type, int size, void *da if (size > 0 && data && MyWrite(netGen[playa].fd, data, size) != size) die("write"); } -} //SendPacketTo +} static MyEventType AlarmGenFunc(EventGenRec *gen, MyEvent *event) { return E_alarm; -} //AlarmGenFunc +} void SCloseNet(short playa) { //kick some connection's ass! @@ -105,14 +106,14 @@ void SCloseNet(short playa) if (netGen[playa].fd >= 0) { if (Players[playa].alive >= 0) { SendPacketTo(playa, 0, NP_endConn, 0, NULL); - do{} while (WaitMyEvent(&event, EM_net) != E_lostConn); + do {} while (WaitMyEvent(&event, EM_net) != E_lostConn); } //say bye to player close(netGen[playa].fd); netGen[playa].fd = -1; } if (netGen[playa].next) RemoveEventGen(&netGen[playa]); -} //SCloseNet +} void CloseNets(void) { //nou oogjes dicht en snaveltjes toe @@ -122,7 +123,7 @@ void CloseNets(void) for (i = 1; i < MAX_SCREENS; i++) SCloseNet(i); //bye everybuddy fprintf(stderr, "* All Done\n\n"); -} //CloseNets +} MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event) { //receive @@ -158,7 +159,7 @@ MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event) return E_lostConn; } //client sent quit signal return E_net; -} //NetGenFunc +} static MyEventType ConnGenFunc(EventGenRec *gen, MyEvent *event) @@ -188,7 +189,7 @@ static MyEventType ConnGenFunc(EventGenRec *gen, MyEvent *event) sprintf(Players[new].host, "%s", inet_ntoa(addr.sin_addr)); if (addr.sin_family == AF_INET) { host = gethostbyaddr((void *)&addr.sin_addr, - sizeof(struct in_addr), AF_INET); + sizeof(struct in_addr), AF_INET); if (host) { strncpy(Players[new].host, host->h_name, sizeof(Players[new].host) - 1); @@ -197,7 +198,7 @@ static MyEventType ConnGenFunc(EventGenRec *gen, MyEvent *event) } } //E_connect return E_connect; -} //ConnGenFunc +} void CountPlayers(void) { //count number of players/teams @@ -211,7 +212,7 @@ void CountPlayers(void) } //player of same team counted before playercount++; } //player alive -} //CountPlayers +} int StartServer(void) { @@ -220,8 +221,10 @@ int StartServer(void) int playersReady = 0; int paused = 1; int i; - char teams[10][7] = { "", "Green", "Cyan", "Blue", "Purple", - "Red", "Grey", "White", "*Orange" }; + char teams[10][7] = { + "", "Green", "Cyan", "Blue", "Purple", + "Red", "Grey", "White", "*Orange" + }; do { switch (WaitMyEvent(&event, EM_any)) { @@ -251,7 +254,7 @@ int StartServer(void) major = ntoh4(versiondata[0]); protocolVersion = ntoh4(versiondata[1]); if (major != MAJOR_VERSION - || protocolVersion != PROTOCOL_VERSION) { + || protocolVersion != PROTOCOL_VERSION) { snprintf(data, sizeof(data), "Version mismatch: received %d.%d", major, protocolVersion); @@ -365,7 +368,7 @@ int StartServer(void) sendtoall: // if (event.u.net.type >= NP_pause) if (event.u.net.type >= NP_rotright - && Game.started < 2) + && Game.started < 2) break; for (i = 1; i < MAX_SCREENS; i++) if (i != event.u.net.sender) @@ -418,33 +421,33 @@ int StartServer(void) } //game (ready to) start(ed) } while (1); fprintf(stderr, "* Exiting server\n"); -} //StartServer +} void SHeader(void) { fprintf(stderr, - "NETRIS Server %s\t(c) 2002 Shiar \n\n", - version_string); + "NETRIS Server %s\t(c) 2002 Shiar \n\n", + version_string); } void SUsage(void) { SHeader(); fprintf(stderr, - "Usage: netris \n" - "\n" - " -h, --help\t\tPrint this usage information\n" - " -H, --info\t\tShow distribution and warranty information\n" - "\n" - " -p, --port \tSet port number (default is %d)\n" - "\n" - " -s, --seed \tStart with given random seed\n" - " -i, --speed \tSet the initial step-down interval, in seconds\n" - " -m, --min-players <2>\tNumber of players required before starting the game\n" - " -x, --max-players <8>\tMaximum number of players allowed in the game\n" - " -c, --continuous\tDon'n quit the game\n" - "\n", DEFAULT_PORT); + "Usage: netris \n" + "\n" + " -h, --help\t\tPrint this usage information\n" + " -H, --info\t\tShow distribution and warranty information\n" + "\n" + " -p, --port \tSet port number (default is %d)\n" + "\n" + " -s, --seed \tStart with given random seed\n" + " -i, --speed \tSet the initial step-down interval, in seconds\n" + " -m, --min-players <2>\tNumber of players required before starting the game\n" + " -x, --max-players <8>\tMaximum number of players allowed in the game\n" + " -c, --continuous\tDon'n quit the game\n" + "\n", DEFAULT_PORT); } void WriteConf(void) @@ -461,46 +464,46 @@ void WriteConf(void) fclose(file_out); fprintf(stderr, "Wrote new game configuration to %s\n", CONFIG_FILE); -} //WriteConf +} void HandleOption(char tag, char *value) { switch (tag) { - case 'v': //verbose + case 'v': //verbose verbose = 1; break; - case 'p': //port + case 'p': //port port = atoi(value); break; - case 'c': //min-players + case 'c': //min-players Game.continuous = atoi(value); break; - case 'm': //min-players + case 'm': //min-players minplayers = atoi(value); break; - case 'x': //max-players + case 'x': //max-players maxplayers = atoi(value); if (maxplayers >= MAX_SCREENS) maxplayers = MAX_SCREENS; break; - case 'q': //quadra-style gravity + case 'q': //quadra-style gravity Game.gravity ^= 1; break; - case 'i': //speed (of level 1) + case 'i': //speed (of level 1) Game.initspeed = atof(value) * 1e6; break; - case 's': //seed + case 's': //seed Game.seed = atoi(value); break; - case 'H': //info + case 'H': //info SHeader(); DistInfo(); exit(0); - case 'h': //help + case 'h': //help SUsage(); exit(0); default: break; } -} //HandleParam +} void ReadConf(char *filename) { @@ -516,8 +519,8 @@ void ReadConf(char *filename) if ((ch = strchr(buf, '#'))) *ch = '\0'; // truncate string from # char for (i = strlen(buf)-1; i >= 0; i--) - if (buf[i] == ' ' || buf[i] == '\t' - || buf[i] == '\n' || buf[i] == 13) + if (buf[i] == ' ' || buf[i] == '\t' + || buf[i] == '\n' || buf[i] == 13) buf[i] = '\0'; else break; @@ -535,7 +538,7 @@ void ReadConf(char *filename) fprintf(stderr, "Unable to open config file %s.\n", filename); } //defaults -} //ReadConf +} void CatchInt(int sig) { @@ -564,8 +567,9 @@ int main(int argc, char **argv) // ReadConf(optarg); // else ReadConf(CONFIG_FILE); - while ((ch = getopt_long(argc, argv, - "hHvqp:i:s:c:m:x:", options, NULL)) != -1) + while ((ch = getopt_long( + argc, argv, "hHvqp:i:s:c:m:x:", options, NULL + )) != -1) HandleOption(ch, optarg); if (optind < argc) { SUsage(); @@ -595,7 +599,7 @@ int main(int argc, char **argv) die("socket"); val1 = 1; setsockopt(connGen.fd, SOL_SOCKET, SO_REUSEADDR, - (void *)&val1, sizeof(val1)); + (void *)&val1, sizeof(val1)); if (bind(connGen.fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) die("bind"); if (listen(connGen.fd, 1) < 0) @@ -609,7 +613,3 @@ int main(int argc, char **argv) return 0; } -/* - * vi: ts=4 ai - * vim: noai si - */