1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
17 #include "libgame/libgame.h"
23 static int clients = 0;
24 static int onceonly = 0;
26 struct NetworkServerPlayerInfo
31 struct NetworkServerPlayerInfo *next;
34 unsigned char readbuffer[MAX_BUFFER_SIZE];
35 unsigned char writbuffer[MAX_BUFFER_SIZE];
38 boolean action_received;
41 static struct NetworkServerPlayerInfo *first_player = NULL;
43 #define NEXT(player) ((player)->next ? (player)->next : first_player)
45 /* TODO: peer address */
46 static TCPsocket lfd; /* listening TCP socket */
47 static UDPsocket udp; /* listening UDP socket */
48 static SDLNet_SocketSet fds; /* socket set */
50 static unsigned char realbuffer[512], *buffer = realbuffer + 4;
54 static unsigned int ServerFrameCounter = 0;
56 static void addtobuffer(struct NetworkServerPlayerInfo *player,
57 unsigned char *b, int len)
59 if (player->nwrite + len >= MAX_BUFFER_SIZE)
60 Error(ERR_EXIT_NETWORK_SERVER,
61 "internal error: network send buffer overflow");
63 memcpy(player->writbuffer + player->nwrite, b, len);
64 player->nwrite += len;
67 static void flushuser(struct NetworkServerPlayerInfo *player)
71 SDLNet_TCP_Send(player->fd, player->writbuffer, player->nwrite);
77 static void broadcast(struct NetworkServerPlayerInfo *except,
78 int len, int activeonly)
80 struct NetworkServerPlayerInfo *player;
82 realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
83 realbuffer[3] = (unsigned char)len;
84 for (player = first_player; player; player = player->next)
85 if (player != except && player->introduced &&
86 (player->active || !activeonly))
87 addtobuffer(player, realbuffer, 4 + len);
90 static void sendtoone(struct NetworkServerPlayerInfo *to, int len)
92 realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
93 realbuffer[3] = (unsigned char)len;
94 addtobuffer(to, realbuffer, 4 + len);
97 static void RemovePlayer(struct NetworkServerPlayerInfo *player)
99 struct NetworkServerPlayerInfo *v;
102 Error(ERR_NETWORK_SERVER, "dropping client %d (%s)",
103 player->number, player->player_name);
105 if (player == first_player)
106 first_player = player->next;
109 for (v = first_player; v; v = v->next)
111 if (v->next && v->next == player)
113 v->next = player->next;
119 SDLNet_TCP_DelSocket(fds, player->fd);
120 SDLNet_TCP_Close(player->fd);
122 if (player->introduced)
124 buffer[0] = player->number;
125 buffer[1] = OP_PLAYER_DISCONNECTED;
126 broadcast(player, 2, 0);
132 if (onceonly && clients == 0)
136 Error(ERR_NETWORK_SERVER, "no clients left");
137 Error(ERR_NETWORK_SERVER, "aborting");
143 static void AddPlayer(TCPsocket fd)
145 struct NetworkServerPlayerInfo *player, *v;
147 boolean again = TRUE;
149 player = checked_malloc(sizeof (struct NetworkServerPlayerInfo));
152 player->player_name[0] = 0;
153 player->next = first_player;
157 player->introduced = 0;
159 player->action_received = FALSE;
161 SDLNet_TCP_AddSocket(fds, fd);
163 first_player = player;
174 if (v->number == nxn)
185 player->number = nxn;
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) already has # %d",
253 player->number, player->player_name, nr_wanted);
255 Error(ERR_NETWORK_SERVER,
256 "client %d (%s) cannot switch (client %d already 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 leveldir %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 Error(ERR_NETWORK_SERVER, "resetting ServerFrameCounter to 0");
349 /* reset player actions */
350 for (v = first_player; v; v = v->next)
353 v->action_received = FALSE;
356 broadcast(NULL, 10 + strlen((char *)&buffer[10])+1, 0);
359 static void Handle_OP_PAUSE_PLAYING(struct NetworkServerPlayerInfo *player)
362 Error(ERR_NETWORK_SERVER, "client %d (%s) pauses game",
363 player->number, player->player_name);
364 broadcast(NULL, 2, 0);
367 static void Handle_OP_CONTINUE_PLAYING(struct NetworkServerPlayerInfo *player)
370 Error(ERR_NETWORK_SERVER, "client %d (%s) continues game",
371 player->number, player->player_name);
372 broadcast(NULL, 2, 0);
375 static void Handle_OP_STOP_PLAYING(struct NetworkServerPlayerInfo *player)
377 int cause_for_stopping = buffer[2];
380 Error(ERR_NETWORK_SERVER, "client %d (%s) stops game [%d]",
381 player->number, player->player_name, cause_for_stopping);
382 broadcast(NULL, 3, 0);
385 static void Handle_OP_MOVE_PLAYER(struct NetworkServerPlayerInfo *player)
387 struct NetworkServerPlayerInfo *v;
388 int last_client_nr = 0;
391 /* store player action */
392 for (v = first_player; v; v = v->next)
394 if (v->number == player->number)
396 v->action = buffer[2];
397 v->action_received = TRUE;
401 /* check if server received action from each player */
402 for (v = first_player; v; v = v->next)
404 if (!v->action_received)
407 if (v->number > last_client_nr)
408 last_client_nr = v->number;
411 /* initialize all player actions to zero */
412 for (i = 0; i < last_client_nr; i++)
415 /* broadcast actions of all players to all players */
416 for (v = first_player; v; v = v->next)
418 buffer[6 + v->number-1] = v->action;
420 v->action_received = FALSE;
423 buffer[2] = (unsigned char)((ServerFrameCounter >> 24) & 0xff);
424 buffer[3] = (unsigned char)((ServerFrameCounter >> 16) & 0xff);
425 buffer[4] = (unsigned char)((ServerFrameCounter >> 8) & 0xff);
426 buffer[5] = (unsigned char)((ServerFrameCounter >> 0) & 0xff);
428 broadcast(NULL, 6 + last_client_nr, 0);
430 ServerFrameCounter++;
433 void ExitNetworkServer(int exit_value)
435 Error(ERR_NETWORK_SERVER, "exiting network server");
440 /* the following is not used for a standalone server;
441 the pointer points to an integer containing the port-number */
442 int NetworkServerThread(void *ptr)
444 NetworkServer(*((int *) ptr), 0);
446 /* should never be reached */
450 void NetworkServer(int port, int serveronly)
452 struct NetworkServerPlayerInfo *player;
457 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_NEXT)
458 struct sigaction sact;
462 port = DEFAULT_SERVER_PORT;
464 // if only running the network server, exit on Ctrl-C
466 signal(SIGINT, ExitNetworkServer);
471 #if defined(PLATFORM_UNIX)
472 #if defined(PLATFORM_NEXT)
473 signal(SIGPIPE, SIG_IGN);
475 sact.sa_handler = SIG_IGN;
476 sigemptyset(&sact.sa_mask);
478 sigaction(SIGPIPE, &sact, NULL);
482 if (SDLNet_ResolveHost(&ip, NULL, port) == -1)
483 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_ResolveHost() failed: %s",
486 if ((fds = SDLNet_AllocSocketSet(MAX_PLAYERS + 1 + 1)) == NULL)
487 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_AllocSocketSet() failed: %s"),
490 if ((lfd = SDLNet_TCP_Open(&ip)) == NULL)
491 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_Open() failed: %s"),
494 if (SDLNet_TCP_AddSocket(fds, lfd) == -1)
495 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_AddSocket() failed: %s"),
498 if ((udp = SDLNet_UDP_Open(port)) == NULL)
499 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_UDP_Open() failed: %s",
502 if (SDLNet_UDP_AddSocket(fds, udp) == -1)
503 Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_AddSocket() failed: %s"),
508 Error(ERR_NETWORK_SERVER, "started up, listening on port %d", port);
509 Error(ERR_NETWORK_SERVER, "using protocol version %d.%d.%d",
510 PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3);
517 for (player = first_player; player; player = player->next)
520 // wait for 100 ms for activity on open network sockets
521 if (SDLNet_CheckSockets(fds, 100) < 1)
524 /* accept incoming TCP connections */
525 if (SDLNet_SocketReady(lfd))
527 Error(ERR_DEBUG, "got TCP packet");
531 newsock = SDLNet_TCP_Accept(lfd);
537 /* accept incoming UDP packets */
538 if (SDLNet_SocketReady(udp))
540 Error(ERR_DEBUG, "got UDP packet");
542 static UDPpacket packet;
544 int num_packets = SDLNet_UDP_Recv(udp, &packet);
546 if (num_packets == 1)
549 SDLNet_UDP_Send(udp, -1, &packet);
553 player = first_player;
555 while (player && !interrupt)
557 if (SDLNet_SocketReady(player->fd))
559 /* read only 1 byte, because SDLNet blocks when we want more than is
561 r = SDLNet_TCP_Recv(player->fd, player->readbuffer + player->nread, 1);
566 Error(ERR_NETWORK_SERVER, "EOF from client %d (%s)",
567 player->number, player->player_name);
568 RemovePlayer(player);
573 while (player->nread >= 4 && player->nread >= 4 + player->readbuffer[3])
575 len = player->readbuffer[3];
576 if (player->readbuffer[0] || player->readbuffer[1] || player->readbuffer[2])
579 Error(ERR_NETWORK_SERVER, "crap from client %d (%s)",
580 player->number, player->player_name);
581 RemovePlayer(player);
585 memcpy(buffer, &player->readbuffer[4], len);
586 player->nread -= 4 + len;
587 memmove(player->readbuffer, player->readbuffer + 4 + len, player->nread);
589 buffer[0] = player->number;
590 if (!player->introduced && buffer[1] != OP_PLAYER_NAME)
593 Error(ERR_NETWORK_SERVER, "!(client %d)->introduced && buffer[1]==%d (expected OP_PLAYER_NAME)", buffer[0], buffer[1]);
595 RemovePlayer(player);
603 Handle_OP_PLAYER_NAME(player, len);
606 case OP_PROTOCOL_VERSION:
607 Handle_OP_PROTOCOL_VERSION(player, len);
610 case OP_NUMBER_WANTED:
611 Handle_OP_NUMBER_WANTED(player);
614 case OP_START_PLAYING:
615 Handle_OP_START_PLAYING(player);
618 case OP_PAUSE_PLAYING:
619 Handle_OP_PAUSE_PLAYING(player);
622 case OP_CONTINUE_PLAYING:
623 Handle_OP_CONTINUE_PLAYING(player);
626 case OP_STOP_PLAYING:
627 Handle_OP_STOP_PLAYING(player);
631 Handle_OP_MOVE_PLAYER(player);
634 case OP_BROADCAST_MESSAGE:
637 Error(ERR_NETWORK_SERVER, "client %d (%s) sends message: %s",
638 player->number, player->player_name, &buffer[2]);
639 broadcast(player, len, 0);
644 Error(ERR_NETWORK_SERVER,
645 "unknown opcode %d from client %d (%s)",
646 buffer[0], player->number, player->player_name);
651 if (player && !interrupt)
652 player = player->next;