X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Fnetwork.c;h=b0a6a0eee2fcc78096a6c25eae72a34cd4358b94;hp=2bbdf6b18b1277c576258cee3982775f379e858b;hb=2b062aac2defaf49659459125225c82c309ba0f5;hpb=830015715a325bc617caacae668c31fa6976cb25 diff --git a/src/network.c b/src/network.c index 2bbdf6b1..b0a6a0ee 100644 --- a/src/network.c +++ b/src/network.c @@ -48,6 +48,85 @@ 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 boolean stop_network_client = FALSE; +static char stop_network_client_message[MAX_OUTPUT_LINESIZE + 1]; + +static void DrawNetworkTextExt(char *message, int font_nr, boolean initialize) +{ + static int xpos = 0, ypos = 0; + static int max_line_width = 0; + int font_width = getFontWidth(font_nr); + int font_height = getFontHeight(font_nr); + int ypos_1 = 120; + int ypos_2 = 150; + + if (initialize) + { + if (game_status == GAME_MODE_LOADING) + { + max_line_width = WIN_XSIZE; + + xpos = (max_line_width - getTextWidth(message, font_nr)) / 2; + ypos = ypos_1; + + DrawText(xpos, ypos, message, font_nr); + + xpos = 0; + ypos = ypos_2; + } + else + { + max_line_width = SXSIZE; + + DrawTextSCentered(ypos_1, font_nr, message); + + /* calculate offset to x position caused by rounding */ + int max_chars_per_line = max_line_width / font_width; + int xoffset = (max_line_width - max_chars_per_line * font_width) / 2; + + xpos = SX + xoffset; + ypos = SY + ypos_2; + } + + Error(ERR_DEBUG, "========== %s ==========", message); + } + else + { + int max_chars_per_line = max_line_width / font_width; + int max_lines_per_text = 10; + int num_lines_spacing = (font_nr == FC_YELLOW ? 1 : 3); + int num_lines_printed = DrawTextBuffer(xpos, ypos, message, font_nr, + max_chars_per_line, -1, + max_lines_per_text, 0, -1, + TRUE, TRUE, FALSE); + + ypos += (num_lines_printed + num_lines_spacing) * font_height; + + Error(ERR_DEBUG, "%s", message); + } + + BackToFront(); +} + +static void DrawNetworkText(char *message) +{ + DrawNetworkTextExt(message, FC_YELLOW, FALSE); +} + +static void DrawNetworkText_Success(char *message) +{ + DrawNetworkTextExt(message, FC_GREEN, FALSE); +} + +static void DrawNetworkText_Failed(char *message) +{ + DrawNetworkTextExt(message, FC_RED, FALSE); +} + +static void DrawNetworkText_Title(char *message) +{ + DrawNetworkTextExt(message, FC_GREEN, TRUE); +} static void SendBufferToServer(int size) { @@ -120,6 +199,8 @@ boolean ConnectToServer(char *hostname, int port) int server_host = 0; int i; + DrawNetworkText_Title("Initializing Network"); + if (port == 0) port = DEFAULT_SERVER_PORT; @@ -159,27 +240,36 @@ boolean ConnectToServer(char *hostname, int port) SDLNet_UDP_Send(udp, -1, &packet); - Error(ERR_DEBUG, "doing UDP broadcast for local network server ..."); + DrawNetworkText("Looking for local network server ..."); - if (SDLNet_CheckSockets(udp_socket_set, 1000) == 1) + /* wait for any local network server to answer UDP broadcast */ + for (i = 0; i < 5; i++) { - int num_packets = SDLNet_UDP_Recv(udp, &packet); - - if (num_packets == 1) + if (SDLNet_CheckSockets(udp_socket_set, 0) == 1) { - Error(ERR_DEBUG, "network server found"); + int num_packets = SDLNet_UDP_Recv(udp, &packet); + + if (num_packets == 1) + { + DrawNetworkText_Success("Network server found!"); + + server_host = SDLNet_Read32(&packet.address.host); + } + else + { + DrawNetworkText_Failed("No answer from network server!"); + } - server_host = SDLNet_Read32(&packet.address.host); + break; } else { - Error(ERR_DEBUG, "no answer from network server"); + Delay_WithScreenUpdates(100); } } - else - { - Error(ERR_DEBUG, "no network server found"); - } + + if (server_host == 0) + DrawNetworkText_Failed("No network server found!"); } rfds = SDLNet_AllocSocketSet(1); @@ -192,6 +282,8 @@ boolean ConnectToServer(char *hostname, int port) Error(ERR_EXIT, "cannot locate host '%s'", hostname); else server_host = SDLNet_Read32(&ip.host); + + DrawNetworkText("Connecting to remote host ..."); } else { @@ -202,6 +294,8 @@ boolean ConnectToServer(char *hostname, int port) SDLNet_Write32(server_host, &ip.host); SDLNet_Write16(port, &ip.port); + + DrawNetworkText("Connecting to local host ..."); } Error(ERR_DEBUG, "trying to connect to network server at %d.%d.%d.%d ...", @@ -215,31 +309,41 @@ boolean ConnectToServer(char *hostname, int port) if (sfd) { SDLNet_TCP_AddSocket(rfds, sfd); + + DrawNetworkText_Success("Successfully connected!"); + return TRUE; } else { + DrawNetworkText_Failed("Failed to connect to network server!"); + 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"); + DrawNetworkText("Starting new local network server ..."); + StartNetworkServer(port); /* wait for server to start up and try connecting several times */ - for (i = 0; i < 6; i++) + for (i = 0; i < 30; i++) { - Delay(500); /* wait 500 ms == 0.5 seconds */ - if ((sfd = SDLNet_TCP_Open(&ip))) /* connected */ { + DrawNetworkText_Success("Successfully connected!"); + SDLNet_TCP_AddSocket(rfds, sfd); return TRUE; } + + Delay_WithScreenUpdates(100); } + DrawNetworkText_Failed("Failed to connect to network server!"); + /* when reaching this point, connect to newly started server has failed */ return FALSE; } @@ -328,9 +432,15 @@ void SendToServer_MovePlayer(byte player_action) static void Handle_OP_BAD_PROTOCOL_VERSION() { Error(ERR_WARN, "protocol version mismatch"); - Error(ERR_EXIT, "server expects %d.%d.x instead of %d.%d.%d", + Error(ERR_WARN, "server expects %d.%d.x instead of %d.%d.%d", buffer[2], buffer[3], PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3); + + sprintf(stop_network_client_message, "Network protocol version mismatch! Server expects version %d.%d.x instead of %d.%d.%d!", + buffer[2], buffer[3], + PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, PROTOCOL_VERSION_3); + + stop_network_client = TRUE; } static void Handle_OP_YOUR_NUMBER() @@ -360,6 +470,8 @@ static void Handle_OP_YOUR_NUMBER() Error(ERR_EXIT, "sorry, more than %d players not allowed", MAX_PLAYERS); Error(ERR_NETWORK_CLIENT, "you get client # %d", new_client_nr); + + stored_player[new_index_nr].connected_network = TRUE; } static void Handle_OP_NUMBER_WANTED() @@ -416,6 +528,9 @@ static void Handle_OP_NUMBER_WANTED() Error(ERR_NETWORK_CLIENT, "cannot switch -- you keep client # %d", new_client_nr); } + + if (game_status == GAME_MODE_MAIN) + DrawNetworkPlayers(); } static void Handle_OP_PLAYER_NAME(unsigned int len) @@ -475,6 +590,24 @@ static void Handle_OP_PLAYER_DISCONNECTED() stored_player[index_nr].connected_locally = FALSE; stored_player[index_nr].connected_network = FALSE; + + if (game_status == GAME_MODE_PLAYING) + { + char message[100]; + + sprintf(message, "Player %d left network server! Network game stopped!", + player_nr); + + Request(message, REQ_CONFIRM | REQ_STAY_CLOSED); + + SetGameStatus(GAME_MODE_MAIN); + + DrawMainMenu(); + } + else if (game_status == GAME_MODE_MAIN) + { + DrawNetworkPlayers(); + } } static void Handle_OP_START_PLAYING() @@ -495,8 +628,9 @@ static void Handle_OP_START_PLAYING() { Error(ERR_WARN, "no such level identifier: '%s'", new_leveldir_identifier); - new_leveldir = leveldir_first; - Error(ERR_WARN, "using default level set: '%s'", new_leveldir->identifier); + stop_network_game = TRUE; + + return; } printf("OP_START_PLAYING: %d\n", buffer[0]); @@ -519,8 +653,11 @@ static void Handle_OP_PAUSE_PLAYING() printf("OP_PAUSE_PLAYING: %d\n", buffer[0]); Error(ERR_NETWORK_CLIENT, "client %d pauses game", buffer[0]); - tape.pausing = TRUE; - DrawVideoDisplay(VIDEO_STATE_PAUSE_ON,0); + if (game_status == GAME_MODE_PLAYING) + { + tape.pausing = TRUE; + DrawVideoDisplay(VIDEO_STATE_PAUSE_ON,0); + } } static void Handle_OP_CONTINUE_PLAYING() @@ -528,8 +665,11 @@ static void Handle_OP_CONTINUE_PLAYING() printf("OP_CONTINUE_PLAYING: %d\n", buffer[0]); Error(ERR_NETWORK_CLIENT, "client %d continues game", buffer[0]); - tape.pausing = FALSE; - DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0); + if (game_status == GAME_MODE_PLAYING) + { + tape.pausing = FALSE; + DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0); + } } static void Handle_OP_STOP_PLAYING() @@ -543,19 +683,21 @@ static void Handle_OP_STOP_PLAYING() int index_nr = client_nr - 1; struct PlayerInfo *client_player = &stored_player[index_nr]; boolean stopped_by_remote_player = (!client_player->connected_locally); - char *message = (buffer[2] == NETWORK_STOP_BY_PLAYER ? - "Network game stopped by player!" : - buffer[2] == NETWORK_STOP_BY_ERROR ? - "Network game stopped due to internal error!" : - "Network game stopped!"); + char message[100]; + + sprintf(message, (buffer[2] == NETWORK_STOP_BY_PLAYER ? + "Network game stopped by player %d!" : + buffer[2] == NETWORK_STOP_BY_ERROR ? + "Network game stopped due to internal error!" : + "Network game stopped!"), client_nr); if (buffer[2] != NETWORK_STOP_BY_PLAYER || stopped_by_remote_player) Request(message, REQ_CONFIRM | REQ_STAY_CLOSED); - } - SetGameStatus(GAME_MODE_MAIN); + SetGameStatus(GAME_MODE_MAIN); - DrawMainMenu(); + DrawMainMenu(); + } } static void Handle_OP_MOVE_PLAYER(unsigned int len) @@ -692,11 +834,25 @@ static char *HandleNetworkingPackets() nread += num_bytes; HandleNetworkingMessages(); + + if (stop_network_client) + return stop_network_client_message; } return NULL; } +static void FreeNetworkClientPlayerInfo(struct NetworkClientPlayerInfo *player) +{ + if (player == NULL) + return; + + if (player->next) + FreeNetworkClientPlayerInfo(player->next); + + checked_free(player); +} + static void HandleNetworkingDisconnect() { int i; @@ -704,11 +860,20 @@ static void HandleNetworkingDisconnect() SDLNet_TCP_DelSocket(rfds, sfd); SDLNet_TCP_Close(sfd); - network.enabled = FALSE; network_playing = FALSE; + network.enabled = FALSE; + network.connected = FALSE; + + setup.network_mode = FALSE; + for (i = 0; i < MAX_PLAYERS; i++) stored_player[i].connected_network = FALSE; + + FreeNetworkClientPlayerInfo(first_player.next); + + first_player.nr = 0; + first_player.next = NULL; } void HandleNetworking() @@ -730,6 +895,22 @@ void HandleNetworking() else { Request(error_message, REQ_CONFIRM); + + if (game_status == GAME_MODE_MAIN) + ClearNetworkPlayers(); } } } + +void DisconnectFromNetworkServer() +{ + DrawNetworkText_Title("Terminating Network"); + DrawNetworkText("Disconnecting from network server ..."); + + HandleNetworkingDisconnect(); + + DrawNetworkText_Success("Successfully disconnected!"); + + /* short time to recognize result of network initialization */ + Delay_WithScreenUpdates(1000); +}