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 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;
84 static void syserr(char *s)
87 fprintf(stderr, "fatal: %s failed.\n", s);
91 static 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 static void flushuser(struct user *u)
103 write(u->fd, u->writbuf, u->nwrite);
108 static 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 static 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 static 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");
196 static void new_connect(int fd)
201 u = malloc(sizeof (struct user));
203 fatal("Out of memory");
207 u->nextvictim = NULL;
216 u->action_received = 0;
226 if (v->number == nxn)
236 printf("RND_SERVER: client %d connecting from %s\n", nxn, inet_ntoa(saddr.sin_addr));
240 buf[1] = OP_YOUR_NUMBER;
245 static void Handle_OP_PROTOCOL_VERSION(struct user *u, unsigned int len)
247 if (len != 5 || buf[2] != PROT_VERS_1 || buf[3] != PROT_VERS_2)
250 printf("RND_SERVER: client %d (%s) has wrong protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
254 buf[2] = PROT_VERS_1;
255 buf[3] = PROT_VERS_2;
256 buf[4] = PROT_VERS_3;
266 printf("RND_SERVER: client %d (%s) uses protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]);
270 static void Handle_OP_NUMBER_WANTED(struct user *u)
273 int nr_wanted = buf[2];
277 printf("RND_SERVER: client %d (%s) wants to switch to # %d\n",
278 u->number, u->nick, nr_wanted);
280 for (v=user0; v; v=v->next)
282 if (v->number == nr_wanted)
292 printf("RND_SERVER: client %d (%s) switches to # %d\n",
293 u->number, u->nick, nr_wanted);
294 else if (u->number == nr_wanted)
295 printf("RND_SERVER: client %d (%s) still has # %d\n",
296 u->number, u->nick, nr_wanted);
298 printf("RND_SERVER: client %d (%s) cannot switch (client %d still exists)\n",
299 u->number, u->nick, nr_wanted);
303 u->number = nr_wanted;
306 buf[1] = OP_NUMBER_WANTED;
312 static void Handle_OP_NICKNAME(struct user *u, unsigned int len)
319 memcpy(u->nick, &buf[2], len-2);
321 for (i=0; i<len-2; i++)
323 if (u->nick[i] < ' ' ||
324 (u->nick[i] > 0x7e && u->nick[i] <= 0xa0))
334 buf[1] = OP_PLAYER_CONNECTED;
339 printf("RND_SERVER: client %d calls itself \"%s\"\n", u->number, u->nick);
340 buf[1] = OP_NICKNAME;
341 broadcast(u, len, 0);
345 for (v=user0; v; v=v->next)
347 if (v != u && v->introduced)
350 buf[1] = OP_PLAYER_CONNECTED;
351 buf[2] = (v->games >> 8);
352 buf[3] = (v->games & 0xff);
354 buf[1] = OP_NICKNAME;
355 memcpy(&buf[2], v->nick, 14);
356 sendtoone(u, 2+strlen(v->nick));
377 static void Handle_OP_START_PLAYING(struct user *u)
382 printf("RND_SERVER: client %d (%s) starts game [level %d from levedir %d (%s)]\n",
384 (buf[2] << 8) + buf[3],
385 (buf[4] << 8) + buf[5],
389 for (w=user0; w; w=w->next)
396 w->nextvictim = NULL;
397 for (v=NEXT(w); v!=w; v=NEXT(v))
413 broadcast(NULL, 2, 0);
415 buf[1] = OP_START_PLAYING;
416 broadcast(NULL, 2, 0);
419 /* reset frame counter */
422 /* reset player actions */
423 for (v=user0; v; v=v->next)
426 v->action_received = 0;
429 broadcast(NULL, 10 + strlen(&buf[10])+1, 0);
432 static void Handle_OP_PAUSE_PLAYING(struct user *u)
435 printf("RND_SERVER: client %d (%s) pauses game\n", u->number, u->nick);
436 broadcast(NULL, 2, 0);
440 static void Handle_OP_CONTINUE_PLAYING(struct user *u)
443 printf("RND_SERVER: client %d (%s) continues game\n", u->number, u->nick);
444 broadcast(NULL, 2, 0);
448 static void Handle_OP_STOP_PLAYING(struct user *u)
451 printf("RND_SERVER: client %d (%s) stops game\n", u->number, u->nick);
452 broadcast(NULL, 2, 0);
455 static void Handle_OP_MOVE_FIGURE(struct user *u)
458 int actions_complete = 1;
459 int last_client_nr = 0;
461 /* store player action */
462 for (v=user0; v; v=v->next)
464 if (v->number == u->number)
467 v->action_received = 1;
471 /* check if server received action from each player */
472 for (v=user0; v; v=v->next)
474 if (!v->action_received)
476 actions_complete = 0;
481 if (!actions_complete)
484 /* broadcast actions of all players to all players */
485 for (v=user0; v; v=v->next)
487 buf[6 + v->number-1] = v->action;
489 v->action_received = 0;
491 if (v->number > last_client_nr)
492 last_client_nr = v->number;
495 buf[2] = (unsigned char)((frame_counter >> 24) & 0xff);
496 buf[3] = (unsigned char)((frame_counter >> 16) & 0xff);
497 buf[4] = (unsigned char)((frame_counter >> 8) & 0xff);
498 buf[5] = (unsigned char)((frame_counter >> 0) & 0xff);
500 broadcast(NULL, 6 + last_client_nr, 0);
506 printf("RND_SERVER: frame %d: client %d (%s) moves player [0x%02x]\n",
508 u->number, u->nick, buf[2]);
512 void NetworkServer(int port, int serveronly)
515 struct user *u, *v, *w;
519 struct protoent *tcpproto;
524 struct sigaction sact;
533 if ((tcpproto = getprotobyname("tcp")) != NULL)
534 tcp = tcpproto->p_proto;
537 signal(SIGPIPE, SIG_IGN);
539 sact.sa_handler = SIG_IGN;
540 sigemptyset(&sact.sa_mask);
542 sigaction(SIGPIPE, &sact, NULL);
546 lfd = socket(PF_INET, SOCK_STREAM, 0);
547 saddr.sin_family = AF_INET;
548 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
549 saddr.sin_port = htons(port);
555 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
556 if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
563 /* become a daemon, breaking all ties with the controlling terminal */
565 for (i=0; i<255; i++)
578 /* open a fake stdin, stdout, stderr, just in case */
579 open("/dev/null", O_RDONLY);
580 open("/dev/null", O_WRONLY);
581 open("/dev/null", O_WRONLY);
586 printf("rocksndiamonds network server: started up, listening on port %d\n",
588 printf("rocksndiamonds network server: using protocol version %d.%d.%d\n",
589 PROT_VERS_1, PROT_VERS_2, PROT_VERS_3);
597 if (timetoplay && time(NULL) >= timetoplay)
602 printf("RND_SERVER: everyone lost... restarting game\n");
607 for (u=user0; u; u=u->next)
623 if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0)
631 if (clients > 0 && clients == bots)
634 printf("RND_SERVER: only bots left... dropping all bots\n");
643 if (FD_ISSET(lfd, &fds))
647 slen = sizeof(saddr);
648 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
659 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
670 if (FD_ISSET(u->fd, &fds))
672 r = read(u->fd, u->readbuf + u->nread, MAX_BUFFER_SIZE - u->nread);
676 printf("RND_SERVER: EOF from client %d (%s)\n", u->number, u->nick);
682 while (u->nread >= 4 && u->nread >= 4 + u->readbuf[3])
685 if (u->readbuf[0] || u->readbuf[1] || u->readbuf[2])
688 printf("RND_SERVER: crap from client %d (%s)\n", u->number, u->nick);
689 write(u->fd, "\033]50;kanji24\007\033#8\033(0", 19);
694 memcpy(buf, &u->readbuf[4], len);
696 copydown(u->readbuf, u->readbuf + 4 + len, u->nread);
699 if (!u->introduced && buf[1] != OP_NICKNAME)
702 printf("RND_SERVER: !(client %d)->introduced && buf[1]==%d (expected OP_NICKNAME)\n", buf[0], buf[1]);
712 Handle_OP_NICKNAME(u, len);
715 case OP_PROTOCOL_VERSION:
716 Handle_OP_PROTOCOL_VERSION(u, len);
719 case OP_NUMBER_WANTED:
720 Handle_OP_NUMBER_WANTED(u);
723 case OP_START_PLAYING:
724 Handle_OP_START_PLAYING(u);
727 case OP_PAUSE_PLAYING:
728 Handle_OP_PAUSE_PLAYING(u);
731 case OP_CONTINUE_PLAYING:
732 Handle_OP_CONTINUE_PLAYING(u);
735 case OP_STOP_PLAYING:
736 Handle_OP_STOP_PLAYING(u);
740 Handle_OP_MOVE_FIGURE(u);
744 for (v=user0; v; v=v->next)
746 if (v->number == buf[2])
754 printf("RND_SERVER: client %d (%s) kills bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
763 printf("RND_SERVER: client %d (%s) attempting to kill non-bot %d (%s)\n", u->number, u->nick, v->number, v->nick);
771 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"));
772 broadcast(NULL, 3, 0);
780 printf("RND_SERVER: client %d (%s) declares itself to be a bot\n", u->number, u->nick);
786 printf("RND_SERVER: client %d (%s) sets level %d\n", u->number, u->nick, buf[2]);
787 broadcast(NULL, 3, 0);
792 struct user *won = NULL;
795 printf("RND_SERVER: client %d (%s) has lost\n", u->number, u->nick);
799 for (v=user0; v; v=v->next)
801 if (v->nextvictim == u)
803 for (w=NEXT(v); w!=v; w=NEXT(w))
805 if (w->active && w->playing)
811 if (v->nextvictim == u)
812 v->nextvictim = NULL;
815 for (v=user0; v; v=v->next)
825 buf[0] = won->number;
828 broadcast(NULL, 2, 0);
835 broadcast(NULL, 2, 0);
837 if (i < 2 && clients > 1)
838 timetoplay = time(NULL) + 4;
843 broadcast(NULL, 2, 0);
845 printf("RND_SERVER: client %d (%s) resets the game counters\n", u->number, u->nick);
846 for (v=user0; v; v=v->next)
858 printf("RND_SERVER: client %d (%s) sends message: %s\n", u->number, u->nick, &buf[2]);
859 broadcast(u, len, 0);
866 printf("RND_SERVER: client %d (%s) sends crap for an OP_LINES\n", u->number, u->nick);
875 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);
876 sendtoone(u->nextvictim, 3);
877 buf[3] = u->nextvictim->number;
879 broadcast(u->nextvictim, 4, 1);
880 for (v=NEXT(u->nextvictim); v!=u->nextvictim; v=NEXT(v))
882 if (v->active && v != u && v->playing)
890 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");
895 printf("RND_SERVER: opcode %d from client %d (%s) not understood\n", buf[0], u->number, u->nick);
903 while (u && !interrupt);