1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
14 #include "libgame/platform.h"
16 #if defined(NETWORK_AVALIABLE)
23 #if defined(TARGET_SDL)
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <netinet/tcp.h>
29 #include <arpa/inet.h>
31 #include <sys/select.h> /* apparently needed for OS/2 port */
34 #include "libgame/libgame.h"
38 static int clients = 0;
39 static int onceonly = 0;
41 struct NetworkServerPlayerInfo
43 #if defined(TARGET_SDL)
51 struct NetworkServerPlayerInfo *next;
54 unsigned char readbuffer[MAX_BUFFER_SIZE];
55 unsigned char writbuffer[MAX_BUFFER_SIZE];
58 boolean action_received;
61 static struct NetworkServerPlayerInfo *first_player = NULL;
63 #define NEXT(player) ((player)->next ? (player)->next : first_player)
65 #if defined(TARGET_SDL)
66 /* TODO: peer address */
67 static TCPsocket lfd; /* listening socket */
68 static SDLNet_SocketSet fds; /* socket set */
70 static struct sockaddr_in saddr;
71 static int lfd; /* listening socket */
72 static fd_set fds; /* socket set */
76 static unsigned char realbuffer[512], *buffer = realbuffer + 4;
80 static unsigned long ServerFrameCounter = 0;
82 static void addtobuffer(struct NetworkServerPlayerInfo *player,
83 unsigned char *b, int len)
85 if (player->nwrite + len >= MAX_BUFFER_SIZE)
86 Error(ERR_EXIT_NETWORK_SERVER,
87 "internal error: network send buffer overflow");
89 memcpy(player->writbuffer + player->nwrite, b, len);
90 player->nwrite += len;
93 static void flushuser(struct NetworkServerPlayerInfo *player)
97 #if defined(TARGET_SDL)
98 SDLNet_TCP_Send(player->fd, player->writbuffer, player->nwrite);
100 write(player->fd, player->writbuffer, player->nwrite);
106 static void broadcast(struct NetworkServerPlayerInfo *except,
107 int len, int activeonly)
109 struct NetworkServerPlayerInfo *player;
111 realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
112 realbuffer[3] = (unsigned char)len;
113 for (player = first_player; player; player = player->next)
114 if (player != except && player->introduced &&
115 (player->active || !activeonly))
116 addtobuffer(player, realbuffer, 4 + len);
119 static void sendtoone(struct NetworkServerPlayerInfo *to, int len)
121 realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
122 realbuffer[3] = (unsigned char)len;
123 addtobuffer(to, realbuffer, 4 + len);
126 static void RemovePlayer(struct NetworkServerPlayerInfo *player)
128 struct NetworkServerPlayerInfo *v;
131 Error(ERR_NETWORK_SERVER, "dropping client %d (%s)",
132 player->number, player->player_name);
134 if (player == first_player)
135 first_player = player->next;
138 for (v = first_player; v; v = v->next)
140 if (v->next && v->next == player)
142 v->next = player->next;
148 #if defined(TARGET_SDL)
149 SDLNet_TCP_DelSocket(fds, player->fd);
150 SDLNet_TCP_Close(player->fd);
155 if (player->introduced)
157 buffer[0] = player->number;
158 buffer[1] = OP_PLAYER_DISCONNECTED;
159 broadcast(player, 2, 0);
165 if (onceonly && clients == 0)
169 Error(ERR_NETWORK_SERVER, "no clients left");
170 Error(ERR_NETWORK_SERVER, "aborting");
176 #if defined(TARGET_SDL)
177 static void AddPlayer(TCPsocket fd)
179 static void AddPlayer(int fd)
182 struct NetworkServerPlayerInfo *player, *v;
184 boolean again = TRUE;
186 player = checked_malloc(sizeof (struct NetworkServerPlayerInfo));
189 player->player_name[0] = 0;
190 player->next = first_player;
194 player->introduced = 0;
196 player->action_received = FALSE;
198 #if defined(TARGET_SDL)
199 SDLNet_TCP_AddSocket(fds, fd);
202 first_player = player;
214 if (v->number == nxn)
229 if (v->number == nxn)
238 player->number = nxn;
239 #if !defined(TARGET_SDL)
241 Error(ERR_NETWORK_SERVER, "client %d connecting from %s",
242 nxn, inet_ntoa(saddr.sin_addr));
247 buffer[1] = OP_YOUR_NUMBER;
248 buffer[2] = player->number;
249 sendtoone(player, 3);
252 static void Handle_OP_PROTOCOL_VERSION(struct NetworkServerPlayerInfo *player,
256 buffer[2] != PROTOCOL_VERSION_1 ||
257 buffer[3] != PROTOCOL_VERSION_2)
260 Error(ERR_NETWORK_SERVER,
261 "client %d (%s) has wrong protocol version %d.%d.%d",
262 player->number, player->player_name, buffer[2], buffer[3], buffer[4]);
265 buffer[1] = OP_BAD_PROTOCOL_VERSION;
266 buffer[2] = PROTOCOL_VERSION_1;
267 buffer[3] = PROTOCOL_VERSION_2;
268 buffer[4] = PROTOCOL_VERSION_3;
269 sendtoone(player, 5);
272 RemovePlayer(player);
278 Error(ERR_NETWORK_SERVER,
279 "client %d (%s) uses protocol version %d.%d.%d",
280 player->number, player->player_name, buffer[2], buffer[3], buffer[4]);
284 static void Handle_OP_NUMBER_WANTED(struct NetworkServerPlayerInfo *player)
286 struct NetworkServerPlayerInfo *v;
287 int client_nr = player->number;
288 int nr_wanted = buffer[2];
292 Error(ERR_NETWORK_SERVER, "client %d (%s) wants to switch to # %d",
293 player->number, player->player_name, nr_wanted);
295 for (v = first_player; v; v = v->next)
297 if (v->number == nr_wanted)
307 Error(ERR_NETWORK_SERVER, "client %d (%s) switches to # %d",
308 player->number, player->player_name, nr_wanted);
309 else if (player->number == nr_wanted)
310 Error(ERR_NETWORK_SERVER, "client %d (%s) still has # %d",
311 player->number, player->player_name, nr_wanted);
313 Error(ERR_NETWORK_SERVER,
314 "client %d (%s) cannot switch (client %d still exists)",
315 player->number, player->player_name, nr_wanted);
319 player->number = nr_wanted;
321 buffer[0] = client_nr;
322 buffer[1] = OP_NUMBER_WANTED;
323 buffer[2] = nr_wanted;
324 buffer[3] = player->number;
327 sendtoone(player, 4);
330 broadcast(NULL, 4, 0);
333 static void Handle_OP_PLAYER_NAME(struct NetworkServerPlayerInfo *player,
336 struct NetworkServerPlayerInfo *v;
341 memcpy(player->player_name, &buffer[2], len-2);
342 player->player_name[len-2] = 0;
343 for (i = 0; i < len - 2; i++)
345 if (player->player_name[i] < ' ' ||
346 ((unsigned char)(player->player_name[i]) > 0x7e &&
347 (unsigned char)(player->player_name[i]) <= 0xa0))
349 player->player_name[i] = 0;
354 if (!player->introduced)
356 buffer[0] = player->number;
357 buffer[1] = OP_PLAYER_CONNECTED;
358 broadcast(player, 2, 0);
362 Error(ERR_NETWORK_SERVER, "client %d calls itself \"%s\"",
363 player->number, player->player_name);
364 buffer[1] = OP_PLAYER_NAME;
365 broadcast(player, len, 0);
367 if (!player->introduced)
369 for (v = first_player; v; v = v->next)
371 if (v != player && v->introduced)
373 buffer[0] = v->number;
374 buffer[1] = OP_PLAYER_CONNECTED;
375 sendtoone(player, 2);
376 buffer[1] = OP_PLAYER_NAME;
377 memcpy(&buffer[2], v->player_name, 14);
378 sendtoone(player, 2+strlen(v->player_name));
383 player->introduced = 1;
386 static void Handle_OP_START_PLAYING(struct NetworkServerPlayerInfo *player)
388 struct NetworkServerPlayerInfo *v, *w;
391 Error(ERR_NETWORK_SERVER,
392 "client %d (%s) starts game [level %d from levedir %d (%s)]",
393 player->number, player->player_name,
394 (buffer[2] << 8) + buffer[3],
395 (buffer[4] << 8) + buffer[5],
398 for (w = first_player; w; w = w->next)
402 /* reset frame counter */
403 ServerFrameCounter = 0;
405 /* reset player actions */
406 for (v = first_player; v; v = v->next)
409 v->action_received = FALSE;
412 broadcast(NULL, 10 + strlen((char *)&buffer[10])+1, 0);
415 static void Handle_OP_PAUSE_PLAYING(struct NetworkServerPlayerInfo *player)
418 Error(ERR_NETWORK_SERVER, "client %d (%s) pauses game",
419 player->number, player->player_name);
420 broadcast(NULL, 2, 0);
423 static void Handle_OP_CONTINUE_PLAYING(struct NetworkServerPlayerInfo *player)
426 Error(ERR_NETWORK_SERVER, "client %d (%s) continues game",
427 player->number, player->player_name);
428 broadcast(NULL, 2, 0);
431 static void Handle_OP_STOP_PLAYING(struct NetworkServerPlayerInfo *player)
434 Error(ERR_NETWORK_SERVER, "client %d (%s) stops game",
435 player->number, player->player_name);
436 broadcast(NULL, 2, 0);
439 static void Handle_OP_MOVE_PLAYER(struct NetworkServerPlayerInfo *player)
441 struct NetworkServerPlayerInfo *v;
442 int last_client_nr = 0;
445 /* store player action */
446 for (v = first_player; v; v = v->next)
448 if (v->number == player->number)
450 v->action = buffer[2];
451 v->action_received = TRUE;
455 /* check if server received action from each player */
456 for (v = first_player; v; v = v->next)
458 if (!v->action_received)
461 if (v->number > last_client_nr)
462 last_client_nr = v->number;
465 /* initialize all player actions to zero */
466 for (i = 0; i < last_client_nr; i++)
469 /* broadcast actions of all players to all players */
470 for (v = first_player; v; v = v->next)
472 buffer[6 + v->number-1] = v->action;
474 v->action_received = FALSE;
477 buffer[2] = (unsigned char)((ServerFrameCounter >> 24) & 0xff);
478 buffer[3] = (unsigned char)((ServerFrameCounter >> 16) & 0xff);
479 buffer[4] = (unsigned char)((ServerFrameCounter >> 8) & 0xff);
480 buffer[5] = (unsigned char)((ServerFrameCounter >> 0) & 0xff);
482 broadcast(NULL, 6 + last_client_nr, 0);
484 ServerFrameCounter++;
487 #if defined(TARGET_SDL)
488 /* the following is not used for a standalone server;
489 the pointer points to an integer containing the port-number */
490 int NetworkServerThread(void *ptr)
492 NetworkServer(*((int *) ptr), 0);
494 /* should never be reached */
499 void NetworkServer(int port, int serveronly)
502 struct NetworkServerPlayerInfo *player;
505 #if defined(TARGET_SDL)
510 struct protoent *tcpproto;
515 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_NEXT)
516 struct sigaction sact;
520 port = DEFAULT_SERVER_PORT;
525 #if !defined(TARGET_SDL)
526 if ((tcpproto = getprotobyname("tcp")) != NULL)
527 tcp = tcpproto->p_proto;
530 #if defined(PLATFORM_UNIX)
531 #if defined(PLATFORM_NEXT)
532 signal(SIGPIPE, SIG_IGN);
534 sact.sa_handler = SIG_IGN;
535 sigemptyset(&sact.sa_mask);
537 sigaction(SIGPIPE, &sact, NULL);
541 #if defined(TARGET_SDL)
543 /* assume that SDL is already initialized */
545 if (SDLNet_Init() == -1)
546 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_Init() failed");
550 if (SDLNet_ResolveHost(&ip, NULL, port) == -1)
551 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_ResolveHost() failed");
553 lfd = SDLNet_TCP_Open(&ip);
555 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_Open() failed");
557 fds = SDLNet_AllocSocketSet(MAX_PLAYERS+1);
558 SDLNet_TCP_AddSocket(fds, lfd);
562 if ((lfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
563 Error(ERR_EXIT_NETWORK_SERVER, "socket() failed");
565 saddr.sin_family = AF_INET;
566 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
567 saddr.sin_port = htons(port);
571 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
572 if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
573 Error(ERR_EXIT_NETWORK_SERVER, "bind() failed");
578 #if !defined(TARGET_SDL)
581 /* become a daemon, breaking all ties with the controlling terminal */
582 options.verbose = FALSE;
583 for (i = 0; i < 255; i++)
596 /* open a fake stdin, stdout, stderr, just in case */
597 open("/dev/null", O_RDONLY);
598 open("/dev/null", O_WRONLY);
599 open("/dev/null", O_WRONLY);
605 Error(ERR_NETWORK_SERVER, "started up, listening on port %d", port);
606 Error(ERR_NETWORK_SERVER, "using protocol version %d.%d.%d",
607 PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3);
614 for (player = first_player; player; player = player->next)
617 #if defined(TARGET_SDL)
618 if ((sl = SDLNet_CheckSockets(fds, 500000)) < 1)
620 Error(ERR_NETWORK_SERVER, SDLNet_GetError());
621 perror("SDLNet_CheckSockets");
628 player = first_player;
631 FD_SET(player->fd, &fds);
632 if (player->fd > mfd)
634 player = player->next;
639 if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0)
642 Error(ERR_EXIT_NETWORK_SERVER, "select() failed");
654 /* accept incoming connections */
655 #if defined(TARGET_SDL)
656 if (SDLNet_SocketReady(lfd))
660 newsock = SDLNet_TCP_Accept(lfd);
668 if (FD_ISSET(lfd, &fds))
672 slen = sizeof(saddr);
673 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
677 Error(ERR_EXIT_NETWORK_SERVER, "accept() failed");
684 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
692 player = first_player;
696 #if defined(TARGET_SDL)
697 if (SDLNet_SocketReady(player->fd))
699 if (FD_ISSET(player->fd, &fds))
702 #if defined(TARGET_SDL)
703 /* read only 1 byte, because SDLNet blocks when we want more than is
705 r = SDLNet_TCP_Recv(player->fd, player->readbuffer + player->nread, 1);
707 r = read(player->fd, player->readbuffer + player->nread,
708 MAX_BUFFER_SIZE - player->nread);
714 Error(ERR_NETWORK_SERVER, "EOF from client %d (%s)",
715 player->number, player->player_name);
716 RemovePlayer(player);
721 while (player->nread >= 4 && player->nread >= 4 + player->readbuffer[3])
723 len = player->readbuffer[3];
724 if (player->readbuffer[0] || player->readbuffer[1] || player->readbuffer[2])
727 Error(ERR_NETWORK_SERVER, "crap from client %d (%s)",
728 player->number, player->player_name);
729 RemovePlayer(player);
733 memcpy(buffer, &player->readbuffer[4], len);
734 player->nread -= 4 + len;
735 memmove(player->readbuffer, player->readbuffer + 4 + len, player->nread);
737 buffer[0] = player->number;
738 if (!player->introduced && buffer[1] != OP_PLAYER_NAME)
741 Error(ERR_NETWORK_SERVER, "!(client %d)->introduced && buffer[1]==%d (expected OP_PLAYER_NAME)", buffer[0], buffer[1]);
743 RemovePlayer(player);
751 Handle_OP_PLAYER_NAME(player, len);
754 case OP_PROTOCOL_VERSION:
755 Handle_OP_PROTOCOL_VERSION(player, len);
758 case OP_NUMBER_WANTED:
759 Handle_OP_NUMBER_WANTED(player);
762 case OP_START_PLAYING:
763 Handle_OP_START_PLAYING(player);
766 case OP_PAUSE_PLAYING:
767 Handle_OP_PAUSE_PLAYING(player);
770 case OP_CONTINUE_PLAYING:
771 Handle_OP_CONTINUE_PLAYING(player);
774 case OP_STOP_PLAYING:
775 Handle_OP_STOP_PLAYING(player);
779 Handle_OP_MOVE_PLAYER(player);
782 case OP_BROADCAST_MESSAGE:
785 Error(ERR_NETWORK_SERVER, "client %d (%s) sends message: %s",
786 player->number, player->player_name, &buffer[2]);
787 broadcast(player, len, 0);
792 Error(ERR_NETWORK_SERVER,
793 "unknown opcode %d from client %d (%s)",
794 buffer[0], player->number, player->player_name);
799 if (player && !interrupt)
800 player = player->next;
802 while (player && !interrupt);
806 #endif /* NETWORK_AVALIABLE */