From 22cc2d19037b7124704e8adf72048eb566fb8c94 Mon Sep 17 00:00:00 2001 From: Jean Billaud Date: Tue, 3 Jul 2018 23:06:59 +0200 Subject: [PATCH] Solv realloc (#26) * test de realloc avec une struc static * use type more than pointer * "" * end * sizeof --- client/client.c | 88 +++----------------------------- client/client_get_game_info.c | 10 +++- common/game_info_serialization.c | 17 +++--- common/request_communication.c | 4 +- common/request_serialization.c | 2 +- server/server.c | 2 +- 6 files changed, 30 insertions(+), 93 deletions(-) diff --git a/client/client.c b/client/client.c index 614fc83..acfa17a 100644 --- a/client/client.c +++ b/client/client.c @@ -1,9 +1,9 @@ /* ** client.c for Bomberman in /Users/kumatetsu/projet-etna/DVC4/Bomberman/Bomberman -** +** ** Made by BILLAUD Jean ** Login -** +** ** Started on Wed Jun 27 17:03:07 2018 BILLAUD Jean ** Last update Tue Jul 3 20:29:30 2018 MASERA Mathieu */ @@ -35,7 +35,7 @@ void init_client(t_sdl *sdl) cr = create_player_request(); start_map(sdl, cs, cr); free_player_request(cr); - + } char *enter_addr(t_sdl *sdl) @@ -49,8 +49,8 @@ char *enter_addr(t_sdl *sdl) SDL_Color black = {0, 0, 0, 0}; TTF_Font *police; int x; - int y; - + int y; + police = TTF_OpenFont("ressources/bm.ttf",60); addr = malloc(sizeof(*addr)); memset(addr, 0, sizeof(*addr)); @@ -86,7 +86,7 @@ char *enter_addr(t_sdl *sdl) case SDL_MOUSEBUTTONDOWN: x = event_queue.button.x; y = event_queue.button.y; - + if (( x > connect.x ) && ( x < connect.x + connect.w ) && ( y > connect.y ) && ( y < connect.y + connect.h ) ) if (strlen(addr) >= 7 && strlen(addr) <= 15) quit = 1; @@ -95,84 +95,10 @@ char *enter_addr(t_sdl *sdl) SDL_RenderClear(sdl->renderer); SDL_RenderCopy(sdl->renderer, sdl->white_back, NULL, NULL); SDL_RenderCopy(sdl->renderer, sdl->server_welcome, NULL, &join_position); - SDL_RenderCopy(sdl->renderer, sdl->join_game, NULL, &connect); + SDL_RenderCopy(sdl->renderer, sdl->join_game, NULL, &connect); SDL_RenderPresent(sdl->renderer); SDL_SetRenderTarget(sdl->renderer, NULL); } return addr; } - -void client_loop(t_sdl *sdl, int socket, t_player_request *cr) { - int quit = 0; - //int x; - //int y; - SDL_Event event_queue; - SDL_Rect join_position = {200, 300, 400, 60}; - SDL_Color black = {0, 0, 0, 0}; - TTF_Font *police; - fd_set fd_read; - - police = TTF_OpenFont("ressources/bm.ttf",60); - sdl->server_welcome = SDL_CreateTextureFromSurface(sdl->renderer, TTF_RenderText_Blended(police, "PATATE", black)); - printf("%d", socket); - cr->command = 1; - - while(!quit) { - //FD_SET(STDIN_FILENO, &fd_read); - //printf("toto"); - if (select((socket + 1), &fd_read, NULL, NULL, NULL) == -1) - { - quit = 1; - } - - while(SDL_PollEvent(&event_queue)) { - switch(event_queue.type){ - case SDL_QUIT: - quit = 1; - break; - case SDL_KEYUP: - break; - case SDL_KEYDOWN: - switch(event_queue.key.keysym.sym){ - case SDLK_UP: - printf("up\n"); - send_request(socket, cr); - fseek(stdin,0,SEEK_END); - break; - case SDLK_DOWN: - printf("down\n"); - send_request(socket, cr); - fseek(stdin,0,SEEK_END); - break; - case SDLK_RIGHT: - printf("right\n"); - send_request(socket, cr); - fseek(stdin,0,SEEK_END); - break; - case SDLK_LEFT: - printf("left\n"); - send_request(socket, cr); - fseek(stdin,0,SEEK_END); - break; - } - } - } - - if (FD_ISSET(socket, &fd_read)) - { - printf("tata\n"); - if (get_message(socket) == 0){ - quit = 1; - } - } - - SDL_RenderClear(sdl->renderer); - SDL_RenderCopy(sdl->renderer, sdl->white_back, NULL, NULL); - SDL_RenderCopy(sdl->renderer, sdl->server_welcome, NULL, &join_position); - SDL_RenderPresent(sdl->renderer); - SDL_SetRenderTarget(sdl->renderer, NULL); - } - - return; -} diff --git a/client/client_get_game_info.c b/client/client_get_game_info.c index f4f51cf..1cc53e8 100644 --- a/client/client_get_game_info.c +++ b/client/client_get_game_info.c @@ -15,17 +15,23 @@ int get_message(int s) { - char buff[1024]; + char buff[sizeof(t_game_info)]; int r; t_game_info *game_info; game_info = NULL; - r = recv(s, buff, 1024 - 1, 0); + r = recv(s, buff, sizeof(t_game_info), 0); if (r > 0) { + printf("%d", r); deserialize_game_info(buff); game_info = get_game_info(); printf("%d \n", game_info->game_status); + printf("%d \n", game_info->tick_time); + printf("%ld \n", (long int)game_info->checksum); + if (game_info->players[0] != NULL){ + printf("%d \n", game_info->players[0]->num_player); + } return 1; } else diff --git a/common/game_info_serialization.c b/common/game_info_serialization.c index a9df9ba..9dc49cb 100644 --- a/common/game_info_serialization.c +++ b/common/game_info_serialization.c @@ -6,23 +6,29 @@ char *serialize_game_info() { - char *game_info_str; + char *game_info_str; t_game_info *game_info; + t_game_info *tmp; game_info = get_game_info(); + tmp = malloc(sizeof(t_game_info)); + //maybe not good at all + memcpy(tmp, &game_info, sizeof(t_game_info) + 1); if ((game_info_str = calloc(1, sizeof(t_game_info))) == NULL) return NULL; - game_info_str = (char*) game_info; - if ((game_info_str = realloc(game_info_str, sizeof(t_game_info) + 1)) == NULL) + game_info_str = (char*) tmp; + printf("before realloc\n"); + if ((game_info_str = (char*)realloc(game_info_str, sizeof(t_game_info) + 1)) == NULL) return NULL; game_info_str[sizeof(t_game_info)] = '\0'; + printf("before return\n"); return game_info_str; } void deserialize_game_info(char *serialized_game_info) { t_game_info *game_info; - + game_info = (t_game_info*) serialized_game_info; set_game_info(game_info); } @@ -33,7 +39,7 @@ int get_game_info_checksum() unsigned char *p; t_game_info *game_info; int i; - + game_info = get_game_info(); checksum = game_info->tick_time; p = (unsigned char *)&game_info->players; @@ -44,4 +50,3 @@ int get_game_info_checksum() checksum += p[i]; return checksum; } - diff --git a/common/request_communication.c b/common/request_communication.c index 23998ee..00659af 100644 --- a/common/request_communication.c +++ b/common/request_communication.c @@ -37,7 +37,7 @@ char* request_serialization(t_player_request* client_request) return NULL; client_request->checksum = get_request_checksum(client_request); request_string = (char*) client_request; - request_string = realloc(request_string,sizeof(t_player_request)+1); + request_string = realloc(request_string, sizeof(t_player_request)+1); if (request_string == NULL) return NULL; request_string[sizeof(t_player_request)] = '\0'; @@ -52,4 +52,4 @@ t_player_request* request_deserialize(char* request_serialized) client_request = (t_player_request*)request_serialized; return client_request; -} \ No newline at end of file +} diff --git a/common/request_serialization.c b/common/request_serialization.c index 7651853..fdba027 100644 --- a/common/request_serialization.c +++ b/common/request_serialization.c @@ -4,7 +4,7 @@ int get_request_checksum(t_player_request* client_request) { int checksum = 0; int i; - + unsigned char *p = (unsigned char *)&client_request->magic; for (i = 0; i<(int)sizeof(client_request->magic); i++) { checksum += p[i]; diff --git a/server/server.c b/server/server.c index 873818b..8ef096e 100644 --- a/server/server.c +++ b/server/server.c @@ -40,7 +40,7 @@ void *init_server() // sdl provient de old/old_server.c // PROVIENT DE OLD_SERVER.c dans le folder old/ // sdl->server_welcome = NULL; - + pthread_join(tick_thread, NULL); pthread_join(main_thread, NULL); return (NULL);