X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fnetwork.c;h=b13e4c9a17f78943a7391c0fb5a73b39d016bc33;hb=refs%2Fheads%2Freleases;hp=73a13b1f8ab3032962e7b46a57fa048452fce944;hpb=74c0f7de91268e40d15948f473eac51a9760b9c0;p=rocksndiamonds.git diff --git a/src/network.c b/src/network.c index 73a13b1f..b13e4c9a 100644 --- a/src/network.c +++ b/src/network.c @@ -1,7 +1,7 @@ /*********************************************************** * Rocks'n'Diamonds -- McDuffin Strikes Back! * *----------------------------------------------------------* -* (c) 1995-2002 Artsoft Entertainment * +* (c) 1995-2006 Artsoft Entertainment * * Holger Schemel * * Detmolder Strasse 189 * * 33604 Bielefeld * @@ -13,16 +13,21 @@ #include "libgame/platform.h" -#if defined(PLATFORM_UNIX) +#if defined(NETWORK_AVALIABLE) #include #include + +#if defined(TARGET_SDL) +#include "main.h" +#else #include #include #include #include #include #include +#endif #include "libgame/libgame.h" @@ -50,11 +55,18 @@ static struct NetworkClientPlayerInfo first_player = /* server stuff */ -static int sfd; +#if defined(TARGET_SDL) +static TCPsocket sfd; /* server socket */ +static SDLNet_SocketSet rfds; /* socket set */ +#else +static int sfd; /* server socket */ +#endif + static byte realbuffer[512]; static byte readbuffer[MAX_BUFFER_SIZE], writbuffer[MAX_BUFFER_SIZE]; static byte *buffer = realbuffer + 4; static int nread = 0, nwrite = 0; +static boolean stop_network_game = FALSE; static void SendBufferToServer(int size) { @@ -72,7 +84,12 @@ static void SendBufferToServer(int size) nwrite += 4 + size; /* directly send the buffer to the network server */ - write(sfd, writbuffer, nwrite); +#if defined(TARGET_SDL) + SDLNet_TCP_Send(sfd, writbuffer, nwrite); +#else + if (write(sfd, writbuffer, nwrite) == -1) + Error(ERR_WARN, "write() failed; %s", strerror(errno)); +#endif nwrite = 0; } @@ -109,6 +126,15 @@ char *getNetworkPlayerName(int player_nr) static void StartNetworkServer(int port) { +#if defined(TARGET_SDL) + static int p; + + p = port; + server_thread = SDL_CreateThread(NetworkServerThread, &p); + network_server = TRUE; + +#else + switch (fork()) { case 0: @@ -127,8 +153,68 @@ static void StartNetworkServer(int port) /* we are parent process -- resume normal operation */ return; } +#endif +} + +#if defined(TARGET_SDL) +boolean ConnectToServer(char *hostname, int port) +{ + IPaddress ip; + int i; + + if (port == 0) + port = DEFAULT_SERVER_PORT; + + rfds = SDLNet_AllocSocketSet(1); + + if (hostname) + { + SDLNet_ResolveHost(&ip, hostname, port); + if (ip.host == INADDR_NONE) + Error(ERR_EXIT, "cannot locate host '%s'", hostname); + } + else + { + SDLNet_Write32(0x7f000001, &ip.host); /* 127.0.0.1 */ + SDLNet_Write16(port, &ip.port); + } + + sfd = SDLNet_TCP_Open(&ip); + + if (sfd) + { + SDLNet_TCP_AddSocket(rfds, sfd); + return TRUE; + } + else + { + printf("SDLNet_TCP_Open(): %s\n", SDLNet_GetError()); + } + + if (hostname) /* connect to specified server failed */ + return FALSE; + + printf("No rocksndiamonds server on localhost -- starting up one ...\n"); + StartNetworkServer(port); + + /* wait for server to start up and try connecting several times */ + for (i = 0; i < 6; i++) + { + Delay(500); /* wait 500 ms == 0.5 seconds */ + + if ((sfd = SDLNet_TCP_Open(&ip))) /* connected */ + { + SDLNet_TCP_AddSocket(rfds, sfd); + return TRUE; + } + } + + /* when reaching this point, connect to newly started server has failed */ + return FALSE; } +#else + boolean ConnectToServer(char *hostname, int port) { struct sockaddr_in s; @@ -169,7 +255,7 @@ boolean ConnectToServer(char *hostname, int port) if (hostname) /* connect to specified server failed */ return FALSE; - printf("No rocksndiamonds server on localhost - starting up one ...\n"); + printf("No rocksndiamonds server on localhost -- starting up one ...\n"); StartNetworkServer(port); /* wait for server to start up and try connecting several times */ @@ -191,6 +277,7 @@ boolean ConnectToServer(char *hostname, int port) /* when reaching this point, connect to newly started server has failed */ return FALSE; } +#endif /* defined(TARGET_SDL) */ void SendToServer_PlayerName(char *player_name) { @@ -222,7 +309,7 @@ void SendToServer_NrWanted(int nr_wanted) void SendToServer_StartPlaying() { - unsigned long new_random_seed = InitRND(NEW_RANDOMIZE); + unsigned int new_random_seed = InitRND(level.random_seed); int dummy = 0; /* !!! HAS NO MEANING ANYMORE !!! */ /* the name of the level must be enough */ @@ -257,11 +344,12 @@ void SendToServer_ContinuePlaying() SendBufferToServer(2); } -void SendToServer_StopPlaying() +void SendToServer_StopPlaying(int cause_for_stopping) { buffer[1] = OP_STOP_PLAYING; + buffer[2] = cause_for_stopping; - SendBufferToServer(2); + SendBufferToServer(3); } void SendToServer_MovePlayer(byte player_action) @@ -300,7 +388,7 @@ static void Handle_OP_YOUR_NUMBER() } if (first_player.nr > MAX_PLAYERS) - Error(ERR_EXIT, "sorry - no more than %d players", MAX_PLAYERS); + Error(ERR_EXIT, "sorry, more than %d players not allowed", MAX_PLAYERS); Error(ERR_NETWORK_CLIENT, "you get client # %d", new_client_nr); } @@ -344,11 +432,11 @@ static void Handle_OP_NUMBER_WANTED() } else if (old_client_nr == first_player.nr) /* failed -- local player? */ { - char *color[] = { "yellow", "red", "green", "blue" }; char request[100]; - sprintf(request, "Sorry ! %s player still exists ! You are %s player !", - color[index_nr_wanted], color[new_index_nr]); + sprintf(request, "Sorry ! Player %d already exists ! You are player %d !", + index_nr_wanted + 1, new_index_nr + 1); + Request(request, REQ_CONFIRM); Error(ERR_NETWORK_CLIENT, "cannot switch -- you keep client # %d", @@ -415,12 +503,10 @@ static void Handle_OP_START_PLAYING() { LevelDirTree *new_leveldir; int new_level_nr; - int dummy; /* !!! HAS NO MEANING ANYMORE !!! */ - unsigned long new_random_seed; + unsigned int new_random_seed; char *new_leveldir_identifier; new_level_nr = (buffer[2] << 8) + buffer[3]; - dummy = (buffer[4] << 8) + buffer[5]; new_random_seed = (buffer[6] << 24) | (buffer[7] << 16) | (buffer[8] << 8) | (buffer[9]); new_leveldir_identifier = (char *)&buffer[10]; @@ -447,16 +533,7 @@ static void Handle_OP_START_PLAYING() LoadTape(level_nr); LoadLevel(level_nr); - if (setup.autorecord) - TapeStartRecording(); - - if (tape.recording) - tape.random_seed = new_random_seed; - - InitRND(new_random_seed); - - game_status = GAME_MODE_PLAYING; - InitGame(); + StartGameActions(FALSE, setup.autorecord, new_random_seed); } static void Handle_OP_PAUSE_PLAYING() @@ -479,8 +556,18 @@ static void Handle_OP_CONTINUE_PLAYING() static void Handle_OP_STOP_PLAYING() { - printf("OP_STOP_PLAYING: %d\n", buffer[0]); - Error(ERR_NETWORK_CLIENT, "client %d stops game", buffer[0]); + printf("OP_STOP_PLAYING: %d [%d]\n", buffer[0], buffer[2]); + Error(ERR_NETWORK_CLIENT, "client %d stops game [%d]", buffer[0], buffer[2]); + + if (game_status == GAME_MODE_PLAYING) + { + if (buffer[2] == NETWORK_STOP_BY_PLAYER) + Request("Network game stopped by player!", REQ_CONFIRM); + else if (buffer[2] == NETWORK_STOP_BY_ERROR) + Request("Network game stopped due to internal error!", REQ_CONFIRM); + else + Request("Network game stopped !", REQ_CONFIRM); + } game_status = GAME_MODE_MAIN; DrawMainMenu(); @@ -497,12 +584,21 @@ static void Handle_OP_MOVE_PLAYER(unsigned int len) server_frame_counter = (buffer[2] << 24) | (buffer[3] << 16) | (buffer[4] << 8) | (buffer[5]); +#if 0 + Error(ERR_NETWORK_CLIENT, "receiving server frame counter value %d [%d]", + server_frame_counter, FrameCounter); +#endif + if (server_frame_counter != FrameCounter) { - Error(ERR_RETURN, "client and servers frame counters out of sync"); - Error(ERR_RETURN, "frame counter of client is %d", FrameCounter); - Error(ERR_RETURN, "frame counter of server is %d", server_frame_counter); - Error(ERR_EXIT, "this should not happen -- please debug"); + Error(ERR_INFO, "client and servers frame counters out of sync"); + Error(ERR_INFO, "frame counter of client is %d", FrameCounter); + Error(ERR_INFO, "frame counter of server is %d", server_frame_counter); + Error(ERR_INFO, "this should not happen -- please debug"); + + stop_network_game = TRUE; + + return; } /* copy valid player actions */ @@ -517,6 +613,8 @@ static void HandleNetworkingMessages() { unsigned int message_length; + stop_network_game = FALSE; + while (nread >= 4 && nread >= 4 + readbuffer[3]) { message_length = readbuffer[3]; @@ -527,7 +625,7 @@ static void HandleNetworkingMessages() nread -= 4 + message_length; memmove(readbuffer, readbuffer + 4 + message_length, nread); - switch(buffer[1]) + switch (buffer[1]) { case OP_BAD_PROTOCOL_VERSION: Handle_OP_BAD_PROTOCOL_VERSION(); @@ -581,41 +679,66 @@ static void HandleNetworkingMessages() } fflush(stdout); + + /* in case of internal error, stop network game */ + if (stop_network_game) + SendToServer_StopPlaying(NETWORK_STOP_BY_ERROR); } +/* TODO */ + void HandleNetworking() { +#if !defined(TARGET_SDL) static struct timeval tv = { 0, 0 }; fd_set rfds; +#endif int r = 0; - FD_ZERO(&rfds); - FD_SET(sfd, &rfds); - - r = select(sfd + 1, &rfds, NULL, NULL, &tv); + do + { +#if defined(TARGET_SDL) + if ((r = SDLNet_CheckSockets(rfds, 1)) < 0) + Error(ERR_EXIT, "HandleNetworking(): SDLNet_CheckSockets() failed"); - if (r < 0 && errno != EINTR) - Error(ERR_EXIT, "HandleNetworking(): select() failed"); +#else - if (r < 0) FD_ZERO(&rfds); + FD_SET(sfd, &rfds); - if (FD_ISSET(sfd, &rfds)) - { - int r; + r = select(sfd + 1, &rfds, NULL, NULL, &tv); - r = read(sfd, readbuffer + nread, MAX_BUFFER_SIZE - nread); + if (r < 0 && errno != EINTR) + Error(ERR_EXIT, "HandleNetworking(): select() failed"); if (r < 0) - Error(ERR_EXIT, "error reading from network server"); + FD_ZERO(&rfds); +#endif + +#if defined(TARGET_SDL) + if (r > 0) +#else + if (FD_ISSET(sfd, &rfds)) +#endif + { +#if defined(TARGET_SDL) + r = SDLNet_TCP_Recv(sfd, readbuffer + nread, 1); +#else + r = read(sfd, readbuffer + nread, MAX_BUFFER_SIZE - nread); +#endif - if (r == 0) - Error(ERR_EXIT, "connection to network server lost"); + if (r < 0) + Error(ERR_EXIT, "error reading from network server"); - nread += r; + if (r == 0) + Error(ERR_EXIT, "connection to network server lost"); - HandleNetworkingMessages(); + nread += r; + + HandleNetworkingMessages(); + } } + while (r > 0); } #endif /* PLATFORM_UNIX */