1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
14 #include "libgame/libgame.h"
16 #if defined(PLATFORM_UNIX)
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
24 #include <arpa/inet.h>
28 #include "libgame/libgame.h"
39 struct NetworkClientPlayerInfo
42 char name[MAX_PLAYER_NAME_LEN + 1];
43 struct NetworkClientPlayerInfo *next;
46 static struct NetworkClientPlayerInfo first_player =
56 static byte realbuffer[512];
57 static byte readbuffer[MAX_BUFFER_SIZE], writbuffer[MAX_BUFFER_SIZE];
58 static byte *buffer = realbuffer + 4;
59 static int nread = 0, nwrite = 0;
61 static void SendBufferToServer(int size)
66 realbuffer[0] = realbuffer[1] = realbuffer[2] = 0;
67 realbuffer[3] = (byte)size;
70 if (nwrite + 4 + size >= MAX_BUFFER_SIZE)
71 Error(ERR_EXIT, "internal error: network send buffer overflow");
73 memcpy(writbuffer + nwrite, realbuffer, 4 + size);
76 /* directly send the buffer to the network server */
77 write(sfd, writbuffer, nwrite);
81 struct NetworkClientPlayerInfo *getNetworkPlayer(int player_nr)
83 struct NetworkClientPlayerInfo *player = NULL;
85 for (player = &first_player; player; player = player->next)
86 if (player->nr == player_nr)
89 if (player == NULL) /* should not happen */
90 Error(ERR_EXIT, "protocol error: reference to non-existing player %d",
96 char *getNetworkPlayerName(int player_nr)
98 struct NetworkClientPlayerInfo *player;
101 return("the network game server");
102 else if (player_nr == first_player.nr)
105 for (player=&first_player; player; player=player->next)
106 if (player->nr == player_nr && player->name && strlen(player->name))
107 return(player->name);
109 return(EMPTY_PLAYER_NAME);
112 static void StartNetworkServer(int port)
117 NetworkServer(port, options.serveronly);
124 "cannot create network server process - no network playing");
125 options.network = FALSE;
129 /* we are parent process -- resume normal operation */
134 boolean ConnectToServer(char *hostname, int port)
136 struct sockaddr_in s;
137 struct protoent *tcpproto;
142 if ((s.sin_addr.s_addr = inet_addr(hostname)) == -1)
144 struct hostent *host;
146 if ((host = gethostbyname(hostname)) == NULL)
147 Error(ERR_EXIT, "cannot locate host '%s'", hostname);
149 s.sin_addr = *(struct in_addr *)(host->h_addr_list[0]);
153 s.sin_addr.s_addr = inet_addr("127.0.0.1"); /* localhost */
156 port = DEFAULT_SERVER_PORT;
158 s.sin_port = htons(port);
159 s.sin_family = AF_INET;
161 sfd = socket(PF_INET, SOCK_STREAM, 0);
163 Error(ERR_EXIT, "out of file descriptors");
165 if ((tcpproto = getprotobyname("tcp")) != NULL)
166 setsockopt(sfd, tcpproto->p_proto, TCP_NODELAY, (char *)&on, sizeof(int));
168 if (connect(sfd, (struct sockaddr *)&s, sizeof(s)) == 0) /* connected */
171 if (hostname) /* connect to specified server failed */
174 printf("No rocksndiamonds server on localhost - starting up one ...\n");
175 StartNetworkServer(port);
177 /* wait for server to start up and try connecting several times */
180 Delay(500); /* wait 500 ms == 0.5 seconds */
183 sfd = socket(PF_INET, SOCK_STREAM, 0);
185 Error(ERR_EXIT, "out of file descriptors");
187 setsockopt(sfd, tcpproto->p_proto, TCP_NODELAY, (char *)&on, sizeof(int));
189 if (connect(sfd, (struct sockaddr *)&s, sizeof(s)) >= 0) /* connected */
193 /* when reaching this point, connect to newly started server has failed */
197 void SendToServer_PlayerName(char *player_name)
199 int len_player_name = strlen(player_name);
201 buffer[1] = OP_PLAYER_NAME;
202 memcpy(&buffer[2], player_name, len_player_name);
203 SendBufferToServer(2 + len_player_name);
204 Error(ERR_NETWORK_CLIENT, "you set your player name to \"%s\"", player_name);
207 void SendToServer_ProtocolVersion()
209 buffer[1] = OP_PROTOCOL_VERSION;
210 buffer[2] = PROTOCOL_VERSION_1;
211 buffer[3] = PROTOCOL_VERSION_2;
212 buffer[4] = PROTOCOL_VERSION_3;
214 SendBufferToServer(5);
217 void SendToServer_NrWanted(int nr_wanted)
219 buffer[1] = OP_NUMBER_WANTED;
220 buffer[2] = nr_wanted;
222 SendBufferToServer(3);
225 void SendToServer_StartPlaying()
227 unsigned long new_random_seed = InitRND(NEW_RANDOMIZE);
229 int dummy = 0; /* !!! HAS NO MEANING ANYMORE !!! */
230 /* the name of the level must be enough */
232 buffer[1] = OP_START_PLAYING;
233 buffer[2] = (byte)(level_nr >> 8);
234 buffer[3] = (byte)(level_nr & 0xff);
235 buffer[4] = (byte)(dummy >> 8);
236 buffer[5] = (byte)(dummy & 0xff);
238 buffer[6] = (unsigned char)((new_random_seed >> 24) & 0xff);
239 buffer[7] = (unsigned char)((new_random_seed >> 16) & 0xff);
240 buffer[8] = (unsigned char)((new_random_seed >> 8) & 0xff);
241 buffer[9] = (unsigned char)((new_random_seed >> 0) & 0xff);
243 strcpy((char *)&buffer[10], leveldir_current->filename);
245 SendBufferToServer(10 + strlen(leveldir_current->filename) + 1);
248 void SendToServer_PausePlaying()
250 buffer[1] = OP_PAUSE_PLAYING;
252 SendBufferToServer(2);
255 void SendToServer_ContinuePlaying()
257 buffer[1] = OP_CONTINUE_PLAYING;
259 SendBufferToServer(2);
262 void SendToServer_StopPlaying()
264 buffer[1] = OP_STOP_PLAYING;
266 SendBufferToServer(2);
269 void SendToServer_MovePlayer(byte player_action)
271 buffer[1] = OP_MOVE_FIGURE;
272 buffer[2] = player_action;
274 SendBufferToServer(3);
277 static void Handle_OP_BAD_PROTOCOL_VERSION()
279 Error(ERR_WARN, "protocol version mismatch");
280 Error(ERR_EXIT, "server expects %d.%d.x instead of %d.%d.%d",
281 buffer[2], buffer[3],
282 PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3);
285 static void Handle_OP_YOUR_NUMBER()
287 int new_client_nr = buffer[2];
288 int new_index_nr = new_client_nr - 1;
289 struct PlayerInfo *old_local_player = local_player;
290 struct PlayerInfo *new_local_player = &stored_player[new_index_nr];
292 printf("OP_YOUR_NUMBER: %d\n", buffer[0]);
293 first_player.nr = new_client_nr;
295 if (old_local_player != new_local_player)
297 /* copy existing player settings and change to new player */
299 *new_local_player = *old_local_player;
300 old_local_player->connected = FALSE;
301 local_player = new_local_player;
304 if (first_player.nr > MAX_PLAYERS)
305 Error(ERR_EXIT, "sorry - no more than %d players", MAX_PLAYERS);
307 Error(ERR_NETWORK_CLIENT, "you get client # %d", new_client_nr);
310 static void Handle_OP_NUMBER_WANTED()
312 int client_nr_wanted = buffer[2];
313 int old_client_nr = buffer[0];
314 int new_client_nr = buffer[3];
315 int old_index_nr = old_client_nr - 1;
316 int new_index_nr = new_client_nr - 1;
317 int index_nr_wanted = client_nr_wanted - 1;
318 struct PlayerInfo *old_player = &stored_player[old_index_nr];
319 struct PlayerInfo *new_player = &stored_player[new_index_nr];
321 printf("OP_NUMBER_WANTED: %d\n", buffer[0]);
323 if (new_client_nr == client_nr_wanted) /* switching succeeded */
325 struct NetworkClientPlayerInfo *player;
327 if (old_client_nr != client_nr_wanted) /* client's nr has changed */
328 Error(ERR_NETWORK_CLIENT, "client %d switches to # %d",
329 old_client_nr, new_client_nr);
330 else if (old_client_nr == first_player.nr) /* local player keeps his nr */
331 Error(ERR_NETWORK_CLIENT, "keeping client # %d", new_client_nr);
333 if (old_client_nr != new_client_nr)
335 /* copy existing player settings and change to new player */
337 *new_player = *old_player;
338 old_player->connected = FALSE;
341 player = getNetworkPlayer(old_client_nr);
342 player->nr = new_client_nr;
344 if (old_player == local_player) /* local player switched */
345 local_player = new_player;
347 else if (old_client_nr == first_player.nr) /* failed -- local player? */
349 char *color[] = { "yellow", "red", "green", "blue" };
352 sprintf(request, "Sorry ! %s player still exists ! You are %s player !",
353 color[index_nr_wanted], color[new_index_nr]);
354 Request(request, REQ_CONFIRM);
356 Error(ERR_NETWORK_CLIENT, "cannot switch -- you keep client # %d",
361 static void Handle_OP_PLAYER_NAME(unsigned int len)
363 struct NetworkClientPlayerInfo *player;
364 int player_nr = (int)buffer[0];
366 printf("OP_PLAYER_NAME: %d\n", player_nr);
367 player = getNetworkPlayer(player_nr);
369 Error(ERR_NETWORK_CLIENT, "client %d calls itself \"%s\"",
370 buffer[0], &buffer[2]);
371 strncpy(player->name, (char *)&buffer[2], MAX_PLAYER_NAME_LEN);
374 static void Handle_OP_PLAYER_CONNECTED()
376 struct NetworkClientPlayerInfo *player, *last_player = NULL;
377 int new_client_nr = (int)buffer[0];
378 int new_index_nr = new_client_nr - 1;
380 printf("OP_PLAYER_CONNECTED: %d\n", new_client_nr);
381 Error(ERR_NETWORK_CLIENT, "new client %d connected", new_client_nr);
383 for (player=&first_player; player; player=player->next)
385 if (player->nr == new_client_nr)
386 Error(ERR_EXIT, "multiplayer server sent duplicate player id");
388 last_player = player;
391 last_player->next = player =
392 checked_malloc(sizeof(struct NetworkClientPlayerInfo));
393 player->nr = new_client_nr;
394 player->name[0] = '\0';
397 stored_player[new_index_nr].connected = TRUE;
400 static void Handle_OP_PLAYER_DISCONNECTED()
402 struct NetworkClientPlayerInfo *player, *player_disconnected;
403 int player_nr = (int)buffer[0];
405 printf("OP_PLAYER_DISCONNECTED: %d\n", player_nr);
406 player_disconnected = getNetworkPlayer(player_nr);
407 Error(ERR_NETWORK_CLIENT, "client %d (%s) disconnected",
408 player_nr, getNetworkPlayerName(buffer[0]));
410 for (player=&first_player; player; player=player->next)
411 if (player->next == player_disconnected)
412 player->next = player_disconnected->next;
413 free(player_disconnected);
416 static void Handle_OP_START_PLAYING()
418 struct LevelDirInfo *new_leveldir;
420 int dummy; /* !!! HAS NO MEANING ANYMORE !!! */
421 unsigned long new_random_seed;
422 char *new_leveldir_filename;
424 new_level_nr = (buffer[2] << 8) + buffer[3];
425 dummy = (buffer[4] << 8) + buffer[5];
427 (buffer[6] << 24) | (buffer[7] << 16) | (buffer[8] << 8) | (buffer[9]);
428 new_leveldir_filename = (char *)&buffer[10];
430 new_leveldir = getLevelDirInfoFromFilename(new_leveldir_filename);
431 if (new_leveldir == NULL)
433 Error(ERR_WARN, "no such level directory: '%s'", new_leveldir_filename);
435 new_leveldir = leveldir_first;
436 Error(ERR_WARN, "using default level directory: '%s'", new_leveldir->name);
439 printf("OP_START_PLAYING: %d\n", buffer[0]);
440 Error(ERR_NETWORK_CLIENT,
441 "client %d starts game [level %d from leveldir '%s']\n",
442 buffer[0], new_level_nr, new_leveldir->name);
444 leveldir_current = new_leveldir;
445 level_nr = new_level_nr;
451 if (setup.autorecord)
452 TapeStartRecording();
455 tape.random_seed = new_random_seed;
457 InitRND(new_random_seed);
459 game_status = PLAYING;
463 static void Handle_OP_PAUSE_PLAYING()
465 printf("OP_PAUSE_PLAYING: %d\n", buffer[0]);
466 Error(ERR_NETWORK_CLIENT, "client %d pauses game", buffer[0]);
469 DrawVideoDisplay(VIDEO_STATE_PAUSE_ON,0);
472 static void Handle_OP_CONTINUE_PLAYING()
474 printf("OP_CONTINUE_PLAYING: %d\n", buffer[0]);
475 Error(ERR_NETWORK_CLIENT, "client %d continues game", buffer[0]);
477 tape.pausing = FALSE;
478 DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
481 static void Handle_OP_STOP_PLAYING()
483 printf("OP_STOP_PLAYING: %d\n", buffer[0]);
484 Error(ERR_NETWORK_CLIENT, "client %d stops game", buffer[0]);
486 game_status = MAINMENU;
490 static void Handle_OP_MOVE_FIGURE(unsigned int len)
492 int server_frame_counter;
495 if (!network_playing)
498 server_frame_counter =
499 (buffer[2] << 24) | (buffer[3] << 16) | (buffer[4] << 8) | (buffer[5]);
501 if (server_frame_counter != FrameCounter)
503 Error(ERR_RETURN, "client and servers frame counters out of sync");
504 Error(ERR_RETURN, "frame counter of client is %d", FrameCounter);
505 Error(ERR_RETURN, "frame counter of server is %d", server_frame_counter);
506 Error(ERR_EXIT, "this should not happen -- please debug");
509 /* copy valid player actions */
510 for (i=0; i<MAX_PLAYERS; i++)
511 stored_player[i].effective_action =
512 (i < len - 6 && stored_player[i].active ? buffer[6 + i] : 0);
514 network_player_action_received = TRUE;
517 static void HandleNetworkingMessages()
519 unsigned int message_length;
521 while (nread >= 4 && nread >= 4 + readbuffer[3])
523 message_length = readbuffer[3];
524 if (readbuffer[0] || readbuffer[1] || readbuffer[2])
525 Error(ERR_EXIT, "wrong network server line length");
527 memcpy(buffer, &readbuffer[4], message_length);
528 nread -= 4 + message_length;
529 memmove(readbuffer, readbuffer + 4 + message_length, nread);
533 case OP_BAD_PROTOCOL_VERSION:
534 Handle_OP_BAD_PROTOCOL_VERSION();
538 Handle_OP_YOUR_NUMBER();
541 case OP_NUMBER_WANTED:
542 Handle_OP_NUMBER_WANTED();
546 Handle_OP_PLAYER_NAME(message_length);
549 case OP_PLAYER_CONNECTED:
550 Handle_OP_PLAYER_CONNECTED();
553 case OP_PLAYER_DISCONNECTED:
554 Handle_OP_PLAYER_DISCONNECTED();
557 case OP_START_PLAYING:
558 Handle_OP_START_PLAYING();
561 case OP_PAUSE_PLAYING:
562 Handle_OP_PAUSE_PLAYING();
565 case OP_CONTINUE_PLAYING:
566 Handle_OP_CONTINUE_PLAYING();
569 case OP_STOP_PLAYING:
570 Handle_OP_STOP_PLAYING();
574 Handle_OP_MOVE_FIGURE(message_length);
577 case OP_BROADCAST_MESSAGE:
578 printf("OP_BROADCAST_MESSAGE: %d\n", buffer[0]);
579 Error(ERR_NETWORK_CLIENT, "client %d sends message", buffer[0]);
587 void HandleNetworking()
589 static struct timeval tv = { 0, 0 };
596 r = select(sfd + 1, &rfds, NULL, NULL, &tv);
598 if (r < 0 && errno != EINTR)
599 Error(ERR_EXIT, "HandleNetworking(): select() failed");
604 if (FD_ISSET(sfd, &rfds))
608 r = read(sfd, readbuffer + nread, MAX_BUFFER_SIZE - nread);
611 Error(ERR_EXIT, "error reading from network server");
614 Error(ERR_EXIT, "connection to network server lost");
618 HandleNetworkingMessages();
622 #endif /* PLATFORM_UNIX */