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(PLATFORM_UNIX)
21 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/tcp.h>
26 #include <arpa/inet.h>
29 #include "libgame/libgame.h"
33 static int clients = 0;
34 static int onceonly = 0;
36 struct NetworkServerPlayerInfo
41 struct NetworkServerPlayerInfo *next;
44 unsigned char readbuffer[MAX_BUFFER_SIZE];
45 unsigned char writbuffer[MAX_BUFFER_SIZE];
48 boolean action_received;
51 static struct NetworkServerPlayerInfo *first_player = NULL;
53 #define NEXT(player) ((player)->next ? (player)->next : first_player)
55 static struct sockaddr_in saddr;
57 static unsigned char realbuffer[512], *buffer = realbuffer + 4;
62 static unsigned long ServerFrameCounter = 0;
66 static void addtobuffer(struct NetworkServerPlayerInfo *player,
67 unsigned char *b, int len)
69 if (player->nwrite + len >= MAX_BUFFER_SIZE)
70 Error(ERR_EXIT_NETWORK_SERVER,
71 "internal error: network send buffer overflow");
73 memcpy(player->writbuffer + player->nwrite, b, len);
74 player->nwrite += len;
77 static void flushuser(struct NetworkServerPlayerInfo *player)
81 write(player->fd, player->writbuffer, player->nwrite);
86 static void broadcast(struct NetworkServerPlayerInfo *except,
87 int len, int activeonly)
89 struct NetworkServerPlayerInfo *player;
91 realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
92 realbuffer[3] = (unsigned char)len;
93 for (player=first_player; player; player=player->next)
94 if (player != except && (player->active || !activeonly) && player->introduced)
95 addtobuffer(player, realbuffer, 4 + len);
98 static void sendtoone(struct NetworkServerPlayerInfo *to, int len)
100 realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
101 realbuffer[3] = (unsigned char)len;
102 addtobuffer(to, realbuffer, 4 + len);
105 static void RemovePlayer(struct NetworkServerPlayerInfo *player)
107 struct NetworkServerPlayerInfo *v;
110 Error(ERR_NETWORK_SERVER, "dropping client %d (%s)",
111 player->number, player->player_name);
113 if (player == first_player)
114 first_player = player->next;
117 for (v=first_player; v; v=v->next)
119 if (v->next && v->next == player)
121 v->next = player->next;
128 if (player->introduced)
130 buffer[0] = player->number;
131 buffer[1] = OP_PLAYER_DISCONNECTED;
132 broadcast(player, 2, 0);
138 if (onceonly && clients == 0)
142 Error(ERR_NETWORK_SERVER, "no clients left");
143 Error(ERR_NETWORK_SERVER, "aborting");
149 static void AddPlayer(int fd)
151 struct NetworkServerPlayerInfo *player, *v;
154 player = checked_malloc(sizeof (struct NetworkServerPlayerInfo));
157 player->player_name[0] = 0;
158 player->next = first_player;
162 player->introduced = 0;
164 player->action_received = FALSE;
166 first_player = player;
174 if (v->number == nxn)
182 player->number = nxn;
184 Error(ERR_NETWORK_SERVER, "client %d connecting from %s",
185 nxn, inet_ntoa(saddr.sin_addr));
189 buffer[1] = OP_YOUR_NUMBER;
190 buffer[2] = player->number;
191 sendtoone(player, 3);
194 static void Handle_OP_PROTOCOL_VERSION(struct NetworkServerPlayerInfo *player,
198 buffer[2] != PROTOCOL_VERSION_1 ||
199 buffer[3] != PROTOCOL_VERSION_2)
202 Error(ERR_NETWORK_SERVER,
203 "client %d (%s) has wrong protocol version %d.%d.%d",
204 player->number, player->player_name, buffer[2], buffer[3], buffer[4]);
207 buffer[1] = OP_BAD_PROTOCOL_VERSION;
208 buffer[2] = PROTOCOL_VERSION_1;
209 buffer[3] = PROTOCOL_VERSION_2;
210 buffer[4] = PROTOCOL_VERSION_3;
211 sendtoone(player, 5);
214 RemovePlayer(player);
220 Error(ERR_NETWORK_SERVER,
221 "client %d (%s) uses protocol version %d.%d.%d",
222 player->number, player->player_name, buffer[2], buffer[3], buffer[4]);
226 static void Handle_OP_NUMBER_WANTED(struct NetworkServerPlayerInfo *player)
228 struct NetworkServerPlayerInfo *v;
229 int client_nr = player->number;
230 int nr_wanted = buffer[2];
234 Error(ERR_NETWORK_SERVER, "client %d (%s) wants to switch to # %d",
235 player->number, player->player_name, nr_wanted);
237 for (v=first_player; v; v=v->next)
239 if (v->number == nr_wanted)
249 Error(ERR_NETWORK_SERVER, "client %d (%s) switches to # %d",
250 player->number, player->player_name, nr_wanted);
251 else if (player->number == nr_wanted)
252 Error(ERR_NETWORK_SERVER, "client %d (%s) still has # %d",
253 player->number, player->player_name, nr_wanted);
255 Error(ERR_NETWORK_SERVER,
256 "client %d (%s) cannot switch (client %d still exists)",
257 player->number, player->player_name, nr_wanted);
261 player->number = nr_wanted;
263 buffer[0] = client_nr;
264 buffer[1] = OP_NUMBER_WANTED;
265 buffer[2] = nr_wanted;
266 buffer[3] = player->number;
269 sendtoone(player, 4);
272 broadcast(NULL, 4, 0);
275 static void Handle_OP_PLAYER_NAME(struct NetworkServerPlayerInfo *player,
278 struct NetworkServerPlayerInfo *v;
283 memcpy(player->player_name, &buffer[2], len-2);
284 player->player_name[len-2] = 0;
285 for (i=0; i<len-2; i++)
287 if (player->player_name[i] < ' ' ||
288 ((unsigned char)(player->player_name[i]) > 0x7e &&
289 (unsigned char)(player->player_name[i]) <= 0xa0))
291 player->player_name[i] = 0;
296 if (!player->introduced)
298 buffer[0] = player->number;
299 buffer[1] = OP_PLAYER_CONNECTED;
300 broadcast(player, 2, 0);
304 Error(ERR_NETWORK_SERVER, "client %d calls itself \"%s\"",
305 player->number, player->player_name);
306 buffer[1] = OP_PLAYER_NAME;
307 broadcast(player, len, 0);
309 if (!player->introduced)
311 for (v=first_player; v; v=v->next)
313 if (v != player && v->introduced)
315 buffer[0] = v->number;
316 buffer[1] = OP_PLAYER_CONNECTED;
317 sendtoone(player, 2);
318 buffer[1] = OP_PLAYER_NAME;
319 memcpy(&buffer[2], v->player_name, 14);
320 sendtoone(player, 2+strlen(v->player_name));
325 player->introduced = 1;
328 static void Handle_OP_START_PLAYING(struct NetworkServerPlayerInfo *player)
330 struct NetworkServerPlayerInfo *v, *w;
333 Error(ERR_NETWORK_SERVER,
334 "client %d (%s) starts game [level %d from levedir %d (%s)]",
335 player->number, player->player_name,
336 (buffer[2] << 8) + buffer[3],
337 (buffer[4] << 8) + buffer[5],
340 for (w=first_player; w; w=w->next)
344 /* reset frame counter */
345 ServerFrameCounter = 0;
347 /* reset player actions */
348 for (v=first_player; v; v=v->next)
351 v->action_received = FALSE;
354 broadcast(NULL, 10 + strlen((char *)&buffer[10])+1, 0);
357 static void Handle_OP_PAUSE_PLAYING(struct NetworkServerPlayerInfo *player)
360 Error(ERR_NETWORK_SERVER, "client %d (%s) pauses game",
361 player->number, player->player_name);
362 broadcast(NULL, 2, 0);
365 static void Handle_OP_CONTINUE_PLAYING(struct NetworkServerPlayerInfo *player)
368 Error(ERR_NETWORK_SERVER, "client %d (%s) continues game",
369 player->number, player->player_name);
370 broadcast(NULL, 2, 0);
373 static void Handle_OP_STOP_PLAYING(struct NetworkServerPlayerInfo *player)
376 Error(ERR_NETWORK_SERVER, "client %d (%s) stops game",
377 player->number, player->player_name);
378 broadcast(NULL, 2, 0);
381 static void Handle_OP_MOVE_FIGURE(struct NetworkServerPlayerInfo *player)
383 struct NetworkServerPlayerInfo *v;
384 int last_client_nr = 0;
387 /* store player action */
388 for (v=first_player; v; v=v->next)
390 if (v->number == player->number)
392 v->action = buffer[2];
393 v->action_received = TRUE;
397 /* check if server received action from each player */
398 for (v=first_player; v; v=v->next)
400 if (!v->action_received)
403 if (v->number > last_client_nr)
404 last_client_nr = v->number;
407 /* initialize all player actions to zero */
408 for (i=0; i<last_client_nr; i++)
411 /* broadcast actions of all players to all players */
412 for (v=first_player; v; v=v->next)
414 buffer[6 + v->number-1] = v->action;
416 v->action_received = FALSE;
419 buffer[2] = (unsigned char)((ServerFrameCounter >> 24) & 0xff);
420 buffer[3] = (unsigned char)((ServerFrameCounter >> 16) & 0xff);
421 buffer[4] = (unsigned char)((ServerFrameCounter >> 8) & 0xff);
422 buffer[5] = (unsigned char)((ServerFrameCounter >> 0) & 0xff);
424 broadcast(NULL, 6 + last_client_nr, 0);
426 ServerFrameCounter++;
429 void NetworkServer(int port, int serveronly)
432 struct NetworkServerPlayerInfo *player;
436 struct protoent *tcpproto;
441 struct sigaction sact;
445 port = DEFAULT_SERVER_PORT;
450 if ((tcpproto = getprotobyname("tcp")) != NULL)
451 tcp = tcpproto->p_proto;
454 signal(SIGPIPE, SIG_IGN);
456 sact.sa_handler = SIG_IGN;
457 sigemptyset(&sact.sa_mask);
459 sigaction(SIGPIPE, &sact, NULL);
463 if ((lfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
464 Error(ERR_EXIT_NETWORK_SERVER, "socket() failed");
466 saddr.sin_family = AF_INET;
467 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
468 saddr.sin_port = htons(port);
472 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int));
473 if (bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
474 Error(ERR_EXIT_NETWORK_SERVER, "bind() failed");
480 /* become a daemon, breaking all ties with the controlling terminal */
481 options.verbose = FALSE;
482 for (i=0; i<255; i++)
495 /* open a fake stdin, stdout, stderr, just in case */
496 open("/dev/null", O_RDONLY);
497 open("/dev/null", O_WRONLY);
498 open("/dev/null", O_WRONLY);
503 Error(ERR_NETWORK_SERVER, "started up, listening on port %d", port);
504 Error(ERR_NETWORK_SERVER, "using protocol version %d.%d.%d",
505 PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3);
512 for (player=first_player; player; player=player->next)
517 player = first_player;
520 FD_SET(player->fd, &fds);
521 if (player->fd > mfd)
523 player = player->next;
528 if ((sl = select(mfd + 1, &fds, NULL, NULL, &tv)) < 0)
531 Error(ERR_EXIT_NETWORK_SERVER, "select() failed");
542 if (FD_ISSET(lfd, &fds))
546 slen = sizeof(saddr);
547 newfd = accept(lfd, (struct sockaddr *)&saddr, &slen);
551 Error(ERR_EXIT_NETWORK_SERVER, "accept() failed");
558 setsockopt(newfd, tcp, TCP_NODELAY, (char *)&on, sizeof(int));
565 player = first_player;
569 if (FD_ISSET(player->fd, &fds))
571 r = read(player->fd, player->readbuffer + player->nread, MAX_BUFFER_SIZE - player->nread);
575 Error(ERR_NETWORK_SERVER, "EOF from client %d (%s)",
576 player->number, player->player_name);
577 RemovePlayer(player);
582 while (player->nread >= 4 && player->nread >= 4 + player->readbuffer[3])
584 len = player->readbuffer[3];
585 if (player->readbuffer[0] || player->readbuffer[1] || player->readbuffer[2])
588 Error(ERR_NETWORK_SERVER, "crap from client %d (%s)",
589 player->number, player->player_name);
590 RemovePlayer(player);
594 memcpy(buffer, &player->readbuffer[4], len);
595 player->nread -= 4 + len;
596 memmove(player->readbuffer, player->readbuffer + 4 + len, player->nread);
598 buffer[0] = player->number;
599 if (!player->introduced && buffer[1] != OP_PLAYER_NAME)
602 Error(ERR_NETWORK_SERVER, "!(client %d)->introduced && buffer[1]==%d (expected OP_PLAYER_NAME)", buffer[0], buffer[1]);
604 RemovePlayer(player);
612 Handle_OP_PLAYER_NAME(player, len);
615 case OP_PROTOCOL_VERSION:
616 Handle_OP_PROTOCOL_VERSION(player, len);
619 case OP_NUMBER_WANTED:
620 Handle_OP_NUMBER_WANTED(player);
623 case OP_START_PLAYING:
624 Handle_OP_START_PLAYING(player);
627 case OP_PAUSE_PLAYING:
628 Handle_OP_PAUSE_PLAYING(player);
631 case OP_CONTINUE_PLAYING:
632 Handle_OP_CONTINUE_PLAYING(player);
635 case OP_STOP_PLAYING:
636 Handle_OP_STOP_PLAYING(player);
640 Handle_OP_MOVE_FIGURE(player);
643 case OP_BROADCAST_MESSAGE:
646 Error(ERR_NETWORK_SERVER, "client %d (%s) sends message: %s",
647 player->number, player->player_name, &buffer[2]);
648 broadcast(player, len, 0);
653 Error(ERR_NETWORK_SERVER,
654 "unknown opcode %d from client %d (%s)",
655 buffer[0], player->number, player->player_name);
660 if (player && !interrupt)
661 player = player->next;
663 while (player && !interrupt);
667 #endif /* PLATFORM_UNIX */