2 * A server for a multi-player version of Tetris
4 * Copyright (C) 1996 Roger Espel Llima <roger.espel.llima@pobox.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation. See the file COPYING for details.
18 #include <sys/types.h>
21 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/tcp.h>
26 #include <arpa/inet.h>
35 extern void copydown(char *, char *, int);
36 extern void fatal(char *);
40 static int clients = 0;
42 static int onceonly = 0;
43 static int timetoplay = 0;
44 static int is_daemon = 0;
47 static int paused = 0;
52 unsigned char nick[16];
54 struct user *next, *nextvictim;
57 unsigned char readbuf[MAX_BUFFER_SIZE];
59 unsigned char writbuf[MAX_BUFFER_SIZE];
69 static struct user *user0 = NULL;
71 #define NEXT(u) ((u)->next ? (u)->next : user0)
73 static struct sockaddr_in saddr;
75 static unsigned char realbuf[512], *buf = realbuf + 4;
80 static unsigned long frame_counter = 0;
87 fprintf(stderr, "fatal: %s failed.\n", s);
91 void addtobuffer(struct user *u, unsigned char *b, int len)
93 if (u->nwrite + len >= MAX_BUFFER_SIZE)
94 fatal("Internal error: send buffer overflow");
95 memcpy(u->writbuf + u->nwrite, b, len);
99 void flushuser(struct user *u)
103 write(u->fd, u->writbuf, u->nwrite);
108 void broadcast(struct user *except, int len, int activeonly)
112 realbuf[0] = realbuf[1] = realbuf[2] = 0;
113 realbuf[3] = (unsigned char)len;
114 for (u=user0; u; u=u->next)
115 if (u != except && (u->active || !activeonly) && u->introduced)
116 addtobuffer(u, realbuf, 4 + len);
119 void sendtoone(struct user *to, int len)
121 realbuf[0] = realbuf[1] = realbuf[2] = 0;
122 realbuf[3] = (unsigned char)len;
123 addtobuffer(to, realbuf, 4 + len);
126 void dropuser(struct user *u)
131 printf("RND_SERVER: dropping client %d (%s)\n", u->number, u->nick);
137 for (v=user0; v; v=v->next)
139 if (v->next && v->next == u)
151 buf[1] = OP_PLAYER_DISCONNECTED;
155 for (v=user0; v; v=v->next)
157 if (v->nextvictim == u)
159 for (w=NEXT(v); w!=v; w=NEXT(w))
161 if (w->active && w->playing)
167 if (v->nextvictim == u)
168 v->nextvictim = NULL;
178 if (onceonly && clients == bots)
182 printf("RND_SERVER: no clients left\n");
183 printf("RND_SERVER: aborting\n");
200 for (w=user0; w; w=w->next)
207 w->nextvictim = NULL;
208 for (v=NEXT(w); v!=w; v=NEXT(v))
224 broadcast(NULL, 2, 0);
226 buf[1] = OP_START_PLAYING;
227 broadcast(NULL, 2, 0);
230 /* reset frame counter */
233 /* reset player actions */
234 for (v=user0; v; v=v->next)
237 v->action_received = 0;
240 broadcast(NULL, 10 + strlen(&buf[10])+1, 0);
244 void new_connect(int fd)
249 u = malloc(sizeof (struct user));
251 fatal("Out of memory");
255 u->nextvictim = NULL;
264 u->action_received = 0;
274 if (v->number == nxn)
284 printf("RND_SERVER: client %d connecting from %s\n", nxn, inet_ntoa(saddr.sin_addr));
288 buf[1] = OP_YOUR_NUMBER;
293 void NetworkServer(int port, int onceonly)
296 struct user *u, *v, *w;
300 struct protoent *tcpproto;
305 struct sigaction sact;
311 if ((tcpproto = getprotobyname("tcp")) != NULL)
312 tcp = tcpproto->p_proto;
315 signal(SIGPIPE, SIG_IGN);
317 sact.sa_handler = SIG_IGN;
318 sigemptyset(&sact.sa_mask);
320 sigaction(SIGPIPE, &sact, NULL);
324 lfd = socket(PF_INET, SOCK_STREAM, 0);
325 saddr.sin_family = AF_INET;
326 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
327 saddr.sin_port = htons(port);
333 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
334 if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
341 /* become a daemon, breaking all ties with the controlling terminal */
343 for (i=0; i<255; i++)
354 /* open a fake stdin, stdout, stderr, just in case */
355 open("/dev/null", O_RDONLY);
356 open("/dev/null", O_WRONLY);
357 open("/dev/null", O_WRONLY);
362 printf("rocksndiamonds network server: started up, listening on port %d\n",
364 printf("rocksndiamonds network server: using protocol version %d.%d.%d\n",
365 PROT_VERS_1, PROT_VERS_2, PROT_VERS_3);
372 if (timetoplay && time(NULL) >= timetoplay)
377 printf("RND_SERVER: everyone lost... restarting game\n");
381 for (u=user0; u; u=u->next)
397 if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0)
405 if (clients > 0 && clients == bots)
408 printf("RND_SERVER: only bots left... dropping all bots\n");
417 if (FD_ISSET(lfd, &fds))
421 slen = sizeof(saddr);
422 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
433 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
444 if (FD_ISSET(u->fd, &fds))
446 r = read(u->fd, u->readbuf + u->nread, MAX_BUFFER_SIZE - u->nread);
450 printf("RND_SERVER: EOF from client %d (%s)\n", u->number, u->nick);
456 while (u->nread >= 4 && u->nread >= 4 + u->readbuf[3])
459 if (u->readbuf[0] || u->readbuf[1] || u->readbuf[2])
462 printf("RND_SERVER: crap from client %d (%s)\n", u->number, u->nick);
463 write(u->fd, "\033]50;kanji24\007\033#8\033(0", 19);
468 memcpy(buf, &u->readbuf[4], len);
470 copydown(u->readbuf, u->readbuf + 4 + len, u->nread);
473 if (!u->introduced && buf[1] != OP_NICKNAME)
476 printf("RND_SERVER: !(client %d)->introduced && buf[1]==%d (expected OP_NICKNAME)\n", buf[0], buf[1]);
488 memcpy(u->nick, &buf[2], len-2);
490 for (i=0; i<len-2; i++)
492 if (u->nick[i] < ' ' ||
493 (u->nick[i] > 0x7e && u->nick[i] <= 0xa0))
503 buf[1] = OP_PLAYER_CONNECTED;
508 printf("RND_SERVER: client %d calls itself \"%s\"\n", u->number, u->nick);
509 buf[1] = OP_NICKNAME;
510 broadcast(u, len, 0);
514 for (v=user0; v; v=v->next)
516 if (v != u && v->introduced)
519 buf[1] = OP_PLAYER_CONNECTED;
520 buf[2] = (v->games >> 8);
521 buf[3] = (v->games & 0xff);
523 buf[1] = OP_NICKNAME;
524 memcpy(&buf[2], v->nick, 14);
525 sendtoone(u, 2+strlen(v->nick));
547 for (v=user0; v; v=v->next)
549 if (v->number == buf[2])
557 printf("RND_SERVER: client %d (%s) kills bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
566 printf("RND_SERVER: client %d (%s) attempting to kill non-bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
571 case OP_START_PLAYING:
573 printf("RND_SERVER: client %d (%s) starts game [level %d from levedir %d (%s)]\n",
575 (buf[2] << 8) + buf[3],
576 (buf[4] << 8) + buf[5],
584 int actions_complete = 1;
585 int last_client_nr = 0;
587 /* store player action */
588 for (v=user0; v; v=v->next)
590 if (v->number == u->number)
593 v->action_received = 1;
597 /* check if server received action from each player */
598 for (v=user0; v; v=v->next)
600 if (!v->action_received)
602 actions_complete = 0;
607 if (!actions_complete)
610 /* broadcast actions of all players to all players */
611 for (v=user0; v; v=v->next)
613 buf[6 + v->number-1] = v->action;
615 v->action_received = 0;
617 if (v->number > last_client_nr)
618 last_client_nr = v->number;
621 buf[2] = (unsigned char)((frame_counter >> 24) & 0xff);
622 buf[3] = (unsigned char)((frame_counter >> 16) & 0xff);
623 buf[4] = (unsigned char)((frame_counter >> 8) & 0xff);
624 buf[5] = (unsigned char)((frame_counter >> 0) & 0xff);
626 broadcast(NULL, 6 + last_client_nr, 0);
632 printf("RND_SERVER: frame %d: client %d (%s) moves player [0x%02x]\n",
634 u->number, u->nick, buf[2]);
643 printf("RND_SERVER: client %d (%s) sets mode %d (%s)\n", u->number, u->nick, buf[2], buf[2] == 0 ? "normal" : (buf[2] == 1 ? "fun" : "unknown"));
644 broadcast(NULL, 3, 0);
649 printf("RND_SERVER: client %d (%s) pauses game\n", u->number, u->nick);
650 broadcast(NULL, 2, 0);
656 printf("RND_SERVER: client %d (%s) continues game\n", u->number, u->nick);
657 broadcast(NULL, 2, 0);
666 printf("RND_SERVER: client %d (%s) declares itself to be a bot\n", u->number, u->nick);
672 printf("RND_SERVER: client %d (%s) sets level %d\n", u->number, u->nick, buf[2]);
673 broadcast(NULL, 3, 0);
678 struct user *won = NULL;
681 printf("RND_SERVER: client %d (%s) has lost\n", u->number, u->nick);
685 for (v=user0; v; v=v->next)
687 if (v->nextvictim == u)
689 for (w=NEXT(v); w!=v; w=NEXT(w))
691 if (w->active && w->playing)
697 if (v->nextvictim == u)
698 v->nextvictim = NULL;
701 for (v=user0; v; v=v->next)
711 buf[0] = won->number;
714 broadcast(NULL, 2, 0);
721 broadcast(NULL, 2, 0);
723 if (i < 2 && clients > 1)
724 timetoplay = time(NULL) + 4;
729 broadcast(NULL, 2, 0);
731 printf("RND_SERVER: client %d (%s) resets the game counters\n", u->number, u->nick);
732 for (v=user0; v; v=v->next)
744 printf("RND_SERVER: client %d (%s) sends message: %s\n", u->number, u->nick, &buf[2]);
745 broadcast(u, len, 0);
748 case OP_NUMBER_WANTED:
750 int nr_wanted = buf[2];
754 printf("RND_SERVER: client %d (%s) wants to switch to # %d\n",
755 u->number, u->nick, nr_wanted);
757 for (v=user0; v; v=v->next)
759 if (v->number == nr_wanted)
769 printf("RND_SERVER: client %d (%s) switches to # %d\n",
770 u->number, u->nick, nr_wanted);
771 else if (u->number == nr_wanted)
772 printf("RND_SERVER: client %d (%s) still has # %d\n",
773 u->number, u->nick, nr_wanted);
775 printf("RND_SERVER: client %d (%s) cannot switch (client %d still exists)\n",
776 u->number, u->nick, nr_wanted);
780 u->number = nr_wanted;
783 buf[1] = OP_NUMBER_WANTED;
790 case OP_PROTOCOL_VERSION:
791 if (len != 5 || buf[2] != PROT_VERS_1 || buf[3] != PROT_VERS_2)
794 printf("RND_SERVER: client %d (%s) has wrong protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
798 buf[2] = PROT_VERS_1;
799 buf[3] = PROT_VERS_2;
800 buf[4] = PROT_VERS_3;
810 printf("RND_SERVER: client %d (%s) uses protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
818 printf("RND_SERVER: client %d (%s) sends crap for an OP_LINES\n", u->number, u->nick);
827 printf("RND_SERVER: client %d (%s) sends %d %s to client %d (%s)\n", u->number, u->nick, (int)buf[2], buf[2] == 1 ? "line" : "lines", u->nextvictim->number, u->nextvictim->nick);
828 sendtoone(u->nextvictim, 3);
829 buf[3] = u->nextvictim->number;
831 broadcast(u->nextvictim, 4, 1);
832 for (v=NEXT(u->nextvictim); v!=u->nextvictim; v=NEXT(v))
834 if (v->active && v != u && v->playing)
842 printf("RND_SERVER: client %d (%s) makes %d %s but has no victim\n", u->number, u->nick, (int)buf[2], buf[2] == 1 ? "line" : "lines");
847 printf("RND_SERVER: opcode %d from client %d (%s) not understood\n", buf[0], u->number, u->nick);
855 while (u && !interrupt);