nvqrReturn_t nvqr_disconnect(NVQRConnection *connection) { if (disconnect_from_server(*connection)) { close_client_connection(*connection); destroy_client(*connection); close_server_connection(connection->server_handle); free(connection->process_name); return NVQR_SUCCESS; } return NVQR_ERROR_UNKNOWN; }
nvqrReturn_t nvqr_connect(NVQRConnection *connection, pid_t pid) { memset(connection, 0, sizeof(*connection)); connection->pid = pid; connection->process_name = process_name_from_pid(pid); if (!create_client(connection)) { return NVQR_ERROR_UNKNOWN; } if (!open_server_connection(&(connection->server_handle), pid)) { destroy_client(*connection); return NVQR_ERROR_NOT_SUPPORTED; } if (!connect_to_server(connection)) { destroy_client(*connection); close_server_connection(connection->server_handle); return NVQR_ERROR_UNKNOWN; } return NVQR_SUCCESS; }
int main(int ac, char **av) { // doit absolument etre apelle avant d'autres appels a la lib init_nettool(); if (ac > 1) // server mode { // set handlers assign_newclient(process_newclient, NULL); assign_deadclient(process_deadclient, NULL); assign_clients(process_clients, NULL); // open listen connection if (init_server_connection(PORT)) return (1); // check messages while (1) check_select(MAXSELECT); // close connection close_server_connection(); close_connection(); } else // client mode { // connect to server if (init_connection("localhost", PORT)) return (1); assign_deadclient(process_drop, NULL); printf("step 1\n");fflush(stdout); fflush(stdout); // stock messages to send stock_remote_msg(WELCOME, strlen("tata"), (void*)"tata"); printf("step 2\n");fflush(stdout); fflush(stdout); stock_remote_msg(HELLOWORLD, 0, NULL); printf("step 3\n");fflush(stdout); fflush(stdout); stock_remote_msg(HELLOWORLD, 0, NULL); printf("step 4\n");fflush(stdout); fflush(stdout); stock_remote_msg(HELLOWORLD, 0, NULL); printf("step 5\n");fflush(stdout); fflush(stdout); stock_remote_msg(HELLOWORLD, 0, NULL); printf("step 5\n");fflush(stdout); fflush(stdout); printf("step 7\n");fflush(stdout); fflush(stdout); // send messages while (1) check_select(MAXSELECT); printf("step 8\n");fflush(stdout); fflush(stdout); // close connection close_connection(); printf("step 9\n");fflush(stdout); fflush(stdout); } // free memory, and close connections nettool_quit(); if (players) free(players); return (0); }
int play_multiplayer(void) { int sd = connect_to_server(); if(sd == -1) { ifitron_abort("Couldn't connect to the server.\n"); }else set_client_socket_descriptor(sd); engine_init(); engine_show_main_menu(); //initiate multiplayer_init(get_tron()); player_t *opponent = malloc(sizeof(player_t)); if(opponent == NULL) { ifitron_abort("Error allocating memory for the opponent in play_multiplayer().\n"); } multiplayer_init(opponent); if(send_game_init(sd, MULTIPLE_PLAYER_TYPE + (game.level - 1), 0, 0, 0, 0, 0, tron.user_name) == -1) ifitron_abort("send_game_init encountered an error.\n"); //Receive init. In case someone initiated sooner and level is something other //than what this player requested. byte game_type; int x, y; int ox, oy; char buf[25]; if(receive_game_init(sd, &game_type, &get_tron()->opponents, &x, &y, &ox, &oy, buf) == -1) ifitron_abort("Something went wrong receiving_game_init()\n"); game.level = game_type - MULTIPLE_PLAYER_TYPE; player_increase_size(&tron, 1); update_player_coordinates(&tron, x, y); player_increase_size(opponent, 1); update_player_coordinates(opponent, ox, oy); //wait for start. if(receive_game_start(sd) == -1) ifitron_abort("Something went wrong receiving game start.\n"); while(TRUE) { engine_get_game_input(); player_increase_size(get_tron(), 1); player_update(get_tron()); player_increase_score(get_tron(), game.level); if(tron.score % 50 == 0 && game.level < 9) game.level++; if(send_game_update(sd, get_tron()->body[0].x, get_tron()->body[0].y) == -1) ifitron_abort("send_game_update encountered an error.\n"); //Check if end or update is coming int size = 5; char buf[5]; if(read(sd, buf, size) == -1) ifitron_abort("Error reading end or update.\n"); if(buf[2] == GAME_UPDATE) { player_increase_size(opponent, 1); update_player_coordinates(opponent, buf[3], buf[4]); player_increase_score(opponent, game.level); }else if(buf[2] == GAME_END) { //free opponent's memory if(opponent->body != NULL) free(opponent->body); free(opponent); //close the connection close_server_connection(sd); ifitron_game_over(); return 0; } //Draw the map with the players multiplayer_show_screen (opponent); } }