| enum RoomStatus { | |
| WAITING, | |
| PLAYING | |
| }; | |
| enum UserStatus { | |
| CHOOSE_NAME = 1, | |
| CHOOSE_ROOM = 2, | |
| ACTION_MENU = 3, | |
| AWAIT_RESPONSE = 4, | |
| WAITING_FOR_PLAYER = 5, | |
| }; | |
| enum Choice { | |
| NO_CHOICE = -1, | |
| TIMED_OUT = 0, | |
| ROCK = 1, | |
| PAPER = 2, | |
| SCISSORS = 3 | |
| }; | |
| struct Client { | |
| int user_fd; | |
| char name[MAX_NAME_LEN]; | |
| enum UserStatus user_status; | |
| int num_wins; | |
| int room_id; | |
| }; | |
| struct Room { | |
| int client_fd[2]; | |
| char player_name[2][MAX_NAME_LEN]; | |
| enum Choice player_choice[2]; | |
| time_t player_menu_sent_time[2]; | |
| struct sockaddr_in client_addr[2]; | |
| enum RoomStatus room_status; | |
| int room_port; | |
| int num_players; | |
| }; | |
| typedef struct Room Room; | |
| typedef struct Client Client; | |
| static int DEFAULT_SERVER_PORT; | |
| static char SERVER_IP[INET_ADDRSTRLEN]; | |
| static int NUM_ROOMS; | |
| Client client_set[MAX_CLIENTS]; | |
| Room room_set[MAX_ROOMS]; | |
| volatile sig_atomic_t timeout_flag = 0; | |
| /* Declare udp_socket_fd globally */ | |
| int udp_socket_fd; | |
| int create_tcp_server_socket(char *ip, int port); | |
| int create_udp_server_socket(int port); | |
| void initialize_rooms(); | |
| void start_server_loop(int server_fd); | |
| int accept_new_client(int server_fd); | |
| void handle_client_message(int client_fd); | |
| void process_client_input(int client_fd, const char *buffer); | |
| void send_available_rooms(int client_fd); | |
| void resolve_player_choices(int room_index); | |
| int parse_player_choice(const char *buffer); | |
| void monitor_player_timeouts(); | |
| void send_game_menu_udp(int room_index, int player_index); | |
| void timeout_signal_handler(int signum); | |
| void eject_player_from_room(int client_fd, int room_id); | |
| void handle_udp_message(); | |
| void process_udp_message(const char *buffer, struct sockaddr_in client_addr, socklen_t addr_len); | |
| void eject_player_from_room_udp(struct sockaddr_in client_addr, int room_id); | |
| bool is_integer_value(const char *buffer); | |
| void process_game_end(); | |
| int main(int argc, char *argv[]) { | |
| if (argc != REQUIRED_ARGUMENT_COUNT) { | |
| fprintf(stderr, USAGE_INSTRUCTIONS, argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| strncpy(SERVER_IP, argv[1], INET_ADDRSTRLEN); | |
| DEFAULT_SERVER_PORT = atoi(argv[2]); | |
| NUM_ROOMS = atoi(argv[3]); | |
| int server_fd = create_tcp_server_socket(SERVER_IP, DEFAULT_SERVER_PORT); | |
| /* Initialize udp_socket_fd globally */ | |
| udp_socket_fd = create_udp_server_socket(DEFAULT_SERVER_PORT + ROOM_PORT_OFFSET); | |
| initialize_rooms(); | |
| struct sigaction sa; | |
| sa.sa_handler = timeout_signal_handler; | |
| sigemptyset(&sa.sa_mask); | |
| sa.sa_flags = SA_RESTART; | |
| if (sigaction(SIGALRM, &sa, NULL) == -1) { | |
| perror(ERROR_SIGNAL_HANDLER_SETUP); | |
| exit(EXIT_FAILURE); | |
| } | |
| struct itimerval timer; | |
| timer.it_value.tv_sec = TIMER_INTERVAL_SEC; | |
| timer.it_value.tv_usec = TIMER_INTERVAL_USEC; | |
| timer.it_interval.tv_sec = TIMER_INTERVAL_SEC; | |
| timer.it_interval.tv_usec = TIMER_INTERVAL_USEC; | |
| if (setitimer(ITIMER_REAL, &timer, NULL) == -1) { | |
| perror(ERROR_TIMER_SETUP); | |
| exit(EXIT_FAILURE); | |
| } | |
| start_server_loop(server_fd); | |
| return 0; | |
| } | |
| int create_tcp_server_socket(char *ip, int port) { | |
| struct sockaddr_in server_addr; | |
| int server_fd; | |
| int opt = 1; | |
| server_fd = socket(AF_INET, SOCK_STREAM, 0); | |
| if (server_fd == -1) { | |
| perror(ERROR_SOCKET_CREATION); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) { | |
| perror(ERROR_SETSOCKOPT_REUSEADDR); | |
| } | |
| server_addr.sin_family = AF_INET; | |
| server_addr.sin_port = htons(port); | |
| if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) { | |
| perror(ERROR_INVALID_IP); | |
| exit(EXIT_FAILURE); | |
| } | |
| if ((bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr))) < 0) { | |
| perror(ERROR_BIND_FAILED); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (listen(server_fd, SERVER_BACKLOG) < 0) { | |
| perror(ERROR_LISTEN_FAILED); | |
| exit(EXIT_FAILURE); | |
| } | |
| printf(SERVER_LISTENING, ip, port); | |
| return server_fd; | |
| } | |
| int create_udp_server_socket(int port) { | |
| int udp_fd = socket(AF_INET, SOCK_DGRAM, 0); | |
| if (udp_fd < 0) { | |
| perror("Error creating UDP socket"); | |
| exit(EXIT_FAILURE); | |
| } | |
| struct sockaddr_in udp_addr; | |
| memset(&udp_addr, 0, sizeof(udp_addr)); | |
| udp_addr.sin_family = AF_INET; | |
| udp_addr.sin_addr.s_addr = INADDR_ANY; | |
| udp_addr.sin_port = htons(port); | |
| int opt = 1; | |
| if (setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { | |
| perror("Error setting socket options for UDP socket"); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (bind(udp_fd, (struct sockaddr *)&udp_addr, sizeof(udp_addr)) < 0) { | |
| perror("Error binding UDP socket"); | |
| exit(EXIT_FAILURE); | |
| } | |
| return udp_fd; | |
| } | |
| void initialize_rooms() { | |
| for (int i = 0; i < NUM_ROOMS; i++) { | |
| room_set[i].room_port = DEFAULT_SERVER_PORT + i + ROOM_PORT_OFFSET; | |
| room_set[i].room_status = WAITING; | |
| room_set[i].num_players = 0; | |
| room_set[i].client_fd[0] = -1; | |
| room_set[i].client_fd[1] = -1; | |
| room_set[i].player_choice[0] = NO_CHOICE; | |
| room_set[i].player_choice[1] = NO_CHOICE; | |
| room_set[i].player_menu_sent_time[0] = 0; | |
| room_set[i].player_menu_sent_time[1] = 0; | |
| memset(room_set[i].player_name[0], 0, MAX_NAME_LEN); | |
| memset(room_set[i].player_name[1], 0, MAX_NAME_LEN); | |
| memset(&room_set[i].client_addr[0], 0, sizeof(struct sockaddr_in)); | |
| memset(&room_set[i].client_addr[1], 0, sizeof(struct sockaddr_in)); | |
| } | |
| printf(ROOMS_INITIALIZED, NUM_ROOMS); | |
| } | |
| void start_server_loop(int server_fd) { | |
| char buffer[BUFFER_SIZE] = {'\0'}; | |
| struct pollfd fds[MAX_CLIENTS + 2]; | |
| int nfds = 3; | |
| int timeout = -1; | |
| fds[0].fd = server_fd; | |
| fds[0].events = POLLIN; | |
| fds[1].fd = STDIN_FILENO; | |
| fds[1].events = POLLIN; | |
| fds[2].fd = udp_socket_fd; | |
| fds[2].events = POLLIN; | |
| printf(SERVER_UP_MESSAGE); | |
| while (1) { | |
| int ret = poll(fds, nfds, timeout); | |
| if (ret < 0) { | |
| if (errno == EINTR) { | |
| if (timeout_flag) { | |
| monitor_player_timeouts(); | |
| timeout_flag = 0; | |
| } | |
| continue; | |
| } | |
| perror(ERROR_POLL_FAILED); | |
| break; | |
| } | |
| for (int i = 0; i < nfds; i++) { | |
| if (fds[i].revents == 0) | |
| continue; | |
| if (fds[i].revents != POLLIN) { | |
| close(fds[i].fd); | |
| fds[i].fd = -1; | |
| continue; | |
| } | |
| if (fds[i].fd == server_fd) { | |
| int new_socket = accept_new_client(server_fd); | |
| if (new_socket >= 0) { | |
| fds[nfds].fd = new_socket; | |
| fds[nfds].events = POLLIN; | |
| nfds++; | |
| } | |
| } else if (fds[i].fd == STDIN_FILENO) { | |
| fgets(buffer, BUFFER_SIZE, stdin); | |
| if (strcmp(buffer, GAME_END_COMMAND) == 0) { | |
| process_game_end(); | |
| goto end_server; | |
| } else { | |
| fprintf(stderr, "Unknown command\n"); | |
| } | |
| } else if (fds[i].fd == udp_socket_fd) { | |
| handle_udp_message(); | |
| } else { | |
| handle_client_message(fds[i].fd); | |
| if (client_set[fds[i].fd].user_fd == -1) { | |
| close(fds[i].fd); | |
| fds[i].fd = -1; | |
| } | |
| } | |
| } | |
| int j = 0; | |
| for (int i = 0; i < nfds; i++) { | |
| if (fds[i].fd != -1) { | |
| fds[j++] = fds[i]; | |
| } | |
| } | |
| nfds = j; | |
| if (timeout_flag) { | |
| monitor_player_timeouts(); | |
| timeout_flag = 0; | |
| } | |
| } | |
| end_server: | |
| for (int i = 0; i < nfds; i++) { | |
| if (fds[i].fd >= 0) | |
| close(fds[i].fd); | |
| } | |
| } | |
| int accept_new_client(int server_fd) { | |
| int client_fd; | |
| struct sockaddr_in client_addr; | |
| socklen_t cli_addr_len = sizeof(client_addr); | |
| client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &cli_addr_len); | |
| if (client_fd < 0) { | |
| perror(ERROR_ACCEPT_CONNECTION); | |
| return -1; | |
| } | |
| write(1, NEW_CLIENT_CONNECTED, strlen(NEW_CLIENT_CONNECTED)); | |
| send(client_fd, PROMPT_ENTER_NAME, strlen(PROMPT_ENTER_NAME), 0); | |
| client_set[client_fd].user_fd = client_fd; | |
| client_set[client_fd].user_status = CHOOSE_NAME; | |
| client_set[client_fd].room_id = -1; | |
| memset(client_set[client_fd].name, 0, MAX_NAME_LEN); | |
| client_set[client_fd].num_wins = 0; | |
| return client_fd; | |
| } | |
| void handle_client_message(int client_fd) { | |
| char buffer[BUFFER_SIZE]; | |
| int number_of_bytes = recv(client_fd, buffer, BUFFER_SIZE, 0); | |
| if (number_of_bytes <= 0) { | |
| int log_fd = open(LOG_FILE_NAME, O_APPEND | O_RDWR | O_CREAT, LOG_FILE_PERMISSIONS); | |
| char log_msg[BUFFER_SIZE]; | |
| snprintf(log_msg, BUFFER_SIZE, CLIENT_DISCONNECTED, client_fd); | |
| write(log_fd, log_msg, strlen(log_msg)); | |
| close(log_fd); | |
| if (client_set[client_fd].room_id != -1) { | |
| eject_player_from_room(client_fd, client_set[client_fd].room_id); | |
| } | |
| close(client_fd); | |
| client_set[client_fd].user_fd = -1; | |
| } else { | |
| buffer[number_of_bytes] = '\0'; | |
| process_client_input(client_fd, buffer); | |
| } | |
| } | |
| void process_client_input(int client_fd, const char *buffer) { | |
| Client *client = &client_set[client_fd]; | |
| // int room_id = client->room_id; // Unused variable, can be removed | |
| switch (client->user_status) { | |
| case CHOOSE_NAME: | |
| strncpy(client->name, buffer, MAX_NAME_LEN); | |
| client->name[strcspn(client->name, "\n")] = '\0'; | |
| client->user_status = CHOOSE_ROOM; | |
| send_available_rooms(client_fd); | |
| break; | |
| case CHOOSE_ROOM: { | |
| int chosen_room = atoi(buffer); | |
| if (chosen_room >= 0 && chosen_room < NUM_ROOMS && room_set[chosen_room].num_players < 2 && is_integer_value(buffer)) { | |
| client->room_id = chosen_room; | |
| int player_index = room_set[chosen_room].num_players; | |
| room_set[chosen_room].num_players++; | |
| room_set[chosen_room].client_fd[player_index] = client_fd; | |
| strncpy(room_set[chosen_room].player_name[player_index], client->name, MAX_NAME_LEN); | |
| // Inform client to use UDP | |
| char port_message[BUFFER_SIZE]; | |
| snprintf(port_message, sizeof(port_message), "%s %d\n", ROOM_PORT_PREFIX, DEFAULT_SERVER_PORT + ROOM_PORT_OFFSET); | |
| send(client_fd, port_message, strlen(port_message), 0); | |
| // Set client status | |
| client->user_status = ACTION_MENU; | |
| send_game_menu_udp(chosen_room, player_index); | |
| } else { | |
| send(client_fd, ROOM_FULL_OR_INVALID, strlen(ROOM_FULL_OR_INVALID), 0); | |
| // usleep(1000); | |
| send_available_rooms(client_fd); | |
| } | |
| break; | |
| } | |
| case ACTION_MENU: | |
| if (strcmp(buffer, "exit\n") == 0) { | |
| eject_player_from_room(client_fd, client->room_id); | |
| client->user_status = CHOOSE_ROOM; | |
| send_available_rooms(client_fd); | |
| } else { | |
| int choice = parse_player_choice(buffer); | |
| if (choice != NO_CHOICE && choice >= ROCK && choice <= SCISSORS) { | |
| if (room_set[client->room_id].num_players == 2) { | |
| int room_index = client->room_id; | |
| int player_index = (room_set[room_index].client_fd[0] == client_fd) ? 0 : 1; | |
| room_set[room_index].player_choice[player_index] = Choice(choice); | |
| room_set[room_index].player_menu_sent_time[player_index] = time(NULL); | |
| // send_game_menu_udp(room_index, player_index); | |
| if (room_set[room_index].player_choice[player_index] != NO_CHOICE) { | |
| resolve_player_choices(room_index); | |
| }else { | |
| send(client_fd, WAITING_FOR_OPPONENT, strlen(WAITING_FOR_OPPONENT), 0); | |
| } | |
| } | |
| else { | |
| int room_index = client->room_id; | |
| int player_index = (room_set[room_index].client_fd[0] == client_fd) ? 0 : 1; | |
| room_set[room_index].player_choice[player_index] = Choice(choice); | |
| room_set[room_index].player_menu_sent_time[player_index] = time(NULL); | |
| send(client_fd, WAITING_FOR_OPPONENT, strlen(WAITING_FOR_OPPONENT), 0); | |
| } | |
| } else { | |
| send(client_fd, INVALID_CHOICE_MESSAGE, strlen(INVALID_CHOICE_MESSAGE), 0); | |
| } | |
| } | |
| break; | |
| default: | |
| send(client_fd, ERROR_UNKNOWN_STATE, strlen(ERROR_UNKNOWN_STATE), 0); | |
| break; | |
| } | |
| } | |
| void send_available_rooms(int client_fd) { | |
| char room_list[BUFFER_SIZE]; | |
| strcpy(room_list, AVAILABLE_ROOMS_MESSAGE); | |
| for (int i = 0; i < NUM_ROOMS; i++) { | |
| if (room_set[i].num_players < 2) { | |
| char room_info[50]; | |
| snprintf(room_info, sizeof(room_info), ROOM_INFO_FORMAT, i); | |
| strcat(room_list, room_info); | |
| } | |
| } | |
| send(client_fd, room_list, strlen(room_list), 0); | |
| } | |
| void resolve_player_choices(int room_index) { | |
| enum Choice choice0 = room_set[room_index].player_choice[0]; | |
| enum Choice choice1 = room_set[room_index].player_choice[1]; | |
| if (choice0 != NO_CHOICE || choice1 != NO_CHOICE) { | |
| char result_message[BUFFER_SIZE]; | |
| if (choice0 == TIMED_OUT && choice1 == TIMED_OUT) { | |
| snprintf(result_message, BUFFER_SIZE, DRAW_TIMEOUT_MESSAGE, | |
| room_set[room_index].player_name[0], room_set[room_index].player_name[1]); | |
| } else if (choice0 == TIMED_OUT) { | |
| snprintf(result_message, BUFFER_SIZE, PLAYER_WIN_BY_TIMEOUT, | |
| room_set[room_index].player_name[1], room_set[room_index].player_name[0]); | |
| client_set[room_set[room_index].client_fd[1]].num_wins++; | |
| } else if (choice1 == TIMED_OUT) { | |
| snprintf(result_message, BUFFER_SIZE, PLAYER_WIN_BY_TIMEOUT, | |
| room_set[room_index].player_name[0], room_set[room_index].player_name[1]); | |
| client_set[room_set[room_index].client_fd[0]].num_wins++; | |
| } else if (choice0 != NO_CHOICE && choice1 != NO_CHOICE) { | |
| int result = (3 + choice0 - choice1) % 3; | |
| if (result == 1) { | |
| snprintf(result_message, BUFFER_SIZE, PLAYER_WIN_MESSAGE, | |
| room_set[room_index].player_name[0], room_set[room_index].player_name[1]); | |
| client_set[room_set[room_index].client_fd[0]].num_wins++; | |
| } else if (result == 2) { | |
| snprintf(result_message, BUFFER_SIZE, PLAYER_WIN_MESSAGE, | |
| room_set[room_index].player_name[1], room_set[room_index].player_name[0]); | |
| client_set[room_set[room_index].client_fd[1]].num_wins++; | |
| } else { | |
| snprintf(result_message, BUFFER_SIZE, DRAW_GAME, | |
| room_set[room_index].player_name[0], room_set[room_index].player_name[1]); | |
| } | |
| } else { | |
| return; | |
| } | |
| // Send result message to both clients via UDP | |
| for (int i = 0; i < 2; i++) { | |
| if (room_set[room_index].client_addr[i].sin_family != 0) { | |
| sendto(udp_socket_fd, result_message, strlen(result_message), 0, | |
| (struct sockaddr *)&room_set[room_index].client_addr[i], sizeof(room_set[room_index].client_addr[i])); | |
| } | |
| } | |
| // Reset player choices | |
| room_set[room_index].player_choice[0] = NO_CHOICE; | |
| room_set[room_index].player_choice[1] = NO_CHOICE; | |
| room_set[room_index].player_menu_sent_time[0] = 0; | |
| room_set[room_index].player_menu_sent_time[1] = 0; | |
| // Send game menu to both clients via UDP | |
| for (int i = 0; i < 2; i++) { | |
| send_game_menu_udp(room_index, i); | |
| } | |
| } | |
| } | |
| int parse_player_choice(const char *buffer) { | |
| int choice = atoi(buffer); | |
| if (choice >= ROCK && choice <= SCISSORS) { | |
| return choice; | |
| } | |
| return NO_CHOICE; | |
| } | |
| void monitor_player_timeouts() { | |
| time_t current_time = time(NULL); | |
| for (int i = 0; i < NUM_ROOMS; i++) { | |
| if (room_set[i].num_players == 2) { | |
| for (int j = 0; j < 2; j++) { | |
| if (room_set[i].player_choice[j] == NO_CHOICE && room_set[i].player_menu_sent_time[j] != 0) { | |
| double time_diff = difftime(current_time, room_set[i].player_menu_sent_time[j]); | |
| if (time_diff >= PLAYER_RESPONSE_TIMEOUT) { | |
| room_set[i].player_choice[j] = TIMED_OUT; | |
| client_set[room_set[i].client_fd[j]].user_status = ACTION_MENU; | |
| const char *message = TIMEOUT_MESSAGE; | |
| sendto(udp_socket_fd, message, strlen(message), 0, | |
| (struct sockaddr *)&room_set[i].client_addr[j], sizeof(room_set[i].client_addr[j])); | |
| } | |
| } | |
| } | |
| if (room_set[i].player_choice[0] != NO_CHOICE && room_set[i].player_choice[1] != NO_CHOICE) { | |
| resolve_player_choices(i); | |
| } | |
| } | |
| } | |
| } | |
| void timeout_signal_handler(int signum) { | |
| timeout_flag = 1; | |
| } | |
| void eject_player_from_room(int client_fd, int room_id) { | |
| if (room_id < 0 || room_id >= NUM_ROOMS) | |
| return; | |
| Room *room = &room_set[room_id]; | |
| int player_index = -1; | |
| if (room->client_fd[0] == client_fd) { | |
| player_index = 0; | |
| } else if (room->client_fd[1] == client_fd) { | |
| player_index = 1; | |
| } | |
| if (player_index != -1) { | |
| room->client_fd[player_index] = -1; | |
| room->player_choice[player_index] = NO_CHOICE; | |
| room->player_menu_sent_time[player_index] = 0; | |
| memset(room->player_name[player_index], 0, MAX_NAME_LEN); | |
| memset(&room->client_addr[player_index], 0, sizeof(struct sockaddr_in)); | |
| room->num_players--; | |
| int other_player_index = 1 - player_index; | |
| if (room->client_fd[other_player_index] > 0) { | |
| const char *message = OPPONENT_LEFT_MESSAGE; | |
| sendto(udp_socket_fd, message, strlen(message), 0, | |
| (struct sockaddr *)&room->client_addr[other_player_index], sizeof(room->client_addr[other_player_index])); | |
| client_set[room->client_fd[other_player_index]].user_status = CHOOSE_ROOM; | |
| client_set[room->client_fd[other_player_index]].room_id = -1; | |
| send_available_rooms(room->client_fd[other_player_index]); | |
| room->client_fd[other_player_index] = -1; | |
| room->player_choice[other_player_index] = NO_CHOICE; | |
| room->player_menu_sent_time[other_player_index] = 0; | |
| memset(room->player_name[other_player_index], 0, MAX_NAME_LEN); | |
| memset(&room->client_addr[other_player_index], 0, sizeof(struct sockaddr_in)); | |
| room->num_players = 0; | |
| } | |
| } | |
| } | |
| void handle_udp_message() { | |
| char buffer[BUFFER_SIZE]; | |
| struct sockaddr_in client_addr; | |
| socklen_t addr_len = sizeof(client_addr); | |
| int bytes_received = recvfrom(udp_socket_fd, buffer, BUFFER_SIZE - 1, 0, (struct sockaddr *)&client_addr, &addr_len); | |
| if (bytes_received <= 0) { | |
| perror("Error receiving UDP message"); | |
| return; | |
| } | |
| buffer[bytes_received] = '\0'; | |
| process_udp_message(buffer, client_addr, addr_len); | |
| } | |
| void process_udp_message(const char *buffer, struct sockaddr_in client_addr, socklen_t addr_len) { | |
| int room_id = -1; | |
| int choice = NO_CHOICE; | |
| if (strncmp(buffer, "exit", 4) == 0) { | |
| // Handle player exit | |
| for (int i = 0; i < NUM_ROOMS; i++) { | |
| Room *room = &room_set[i]; | |
| for (int j = 0; j < room->num_players; j++) { | |
| if (room->client_addr[j].sin_addr.s_addr == client_addr.sin_addr.s_addr && | |
| room->client_addr[j].sin_port == client_addr.sin_port) { | |
| eject_player_from_room_udp(client_addr, i); | |
| return; | |
| } | |
| } | |
| } | |
| return; | |
| } | |
| char *buffer_copy = strdup(buffer); | |
| char *token = strtok(buffer_copy, ";"); | |
| while (token != NULL) { | |
| if (strncmp(token, "room_id:", 8) == 0) { | |
| room_id = atoi(token + 8); | |
| } else if (strncmp(token, "choice:", 7) == 0) { | |
| choice = atoi(token + 7); | |
| } | |
| token = strtok(NULL, ";"); | |
| } | |
| free(buffer_copy); | |
| if (room_id < 0 || room_id >= NUM_ROOMS) { | |
| // Invalid room_id | |
| const char *message = "Invalid room ID.\n"; | |
| sendto(udp_socket_fd, message, strlen(message), 0, (struct sockaddr *)&client_addr, addr_len); | |
| return; | |
| } | |
| Room *room = &room_set[room_id]; | |
| int player_index = -1; | |
| // Check if client is already in the room | |
| for (int i = 0; i < room->num_players; i++) { | |
| if (room->client_addr[i].sin_addr.s_addr == client_addr.sin_addr.s_addr && | |
| room->client_addr[i].sin_port == client_addr.sin_port) { | |
| player_index = i; | |
| break; | |
| } | |
| } | |
| if (player_index == -1) { | |
| // New player in the room | |
| if (room->num_players < 2) { | |
| player_index = room->num_players; | |
| room->num_players++; | |
| room->client_addr[player_index] = client_addr; | |
| room->player_choice[player_index] = (enum Choice)choice; | |
| room->player_menu_sent_time[player_index] = time(NULL); | |
| // Send response to client | |
| const char *message = "Waiting for opponent...\n"; | |
| sendto(udp_socket_fd, message, strlen(message), 0, (struct sockaddr *)&client_addr, addr_len); | |
| } else { | |
| // Room is full | |
| const char *message = "Room is full.\n"; | |
| sendto(udp_socket_fd, message, strlen(message), 0, (struct sockaddr *)&client_addr, addr_len); | |
| } | |
| return; | |
| } else { | |
| room->player_choice[player_index] = (enum Choice)choice; | |
| room->player_menu_sent_time[player_index] = time(NULL); | |
| if (room->player_choice[0] != NO_CHOICE && room->player_choice[1] != NO_CHOICE) { | |
| resolve_player_choices(room_id); | |
| } else { | |
| const char *message = "Waiting for opponent's choice...\n"; | |
| sendto(udp_socket_fd, message, strlen(message), 0, (struct sockaddr *)&client_addr, addr_len); | |
| } | |
| } | |
| } | |
| void eject_player_from_room_udp(struct sockaddr_in client_addr, int room_id) { | |
| if (room_id < 0 || room_id >= NUM_ROOMS) | |
| return; | |
| Room *room = &room_set[room_id]; | |
| int player_index = -1; | |
| for (int i = 0; i < room->num_players; i++) { | |
| if (room->client_addr[i].sin_addr.s_addr == client_addr.sin_addr.s_addr && | |
| room->client_addr[i].sin_port == client_addr.sin_port) { | |
| player_index = i; | |
| break; | |
| } | |
| } | |
| if (player_index != -1) { | |
| // Remove player from room | |
| for (int i = player_index; i < room->num_players - 1; i++) { | |
| room->client_addr[i] = room->client_addr[i + 1]; | |
| strncpy(room->player_name[i], room->player_name[i + 1], MAX_NAME_LEN); | |
| room->player_choice[i] = room->player_choice[i + 1]; | |
| room->player_menu_sent_time[i] = room->player_menu_sent_time[i + 1]; | |
| } | |
| room->num_players--; | |
| // Notify the other player | |
| if (room->num_players == 1) { | |
| const char *message = OPPONENT_LEFT_MESSAGE; | |
| sendto(udp_socket_fd, message, strlen(message), 0, | |
| (struct sockaddr *)&room->client_addr[0], sizeof(room->client_addr[0])); | |
| } | |
| } | |
| } | |
| void send_game_menu_udp(int room_index, int player_index) { | |
| const char *message = GAME_MENU_MESSAGE; | |
| sendto(udp_socket_fd, message, strlen(message), 0, | |
| (struct sockaddr *)&room_set[room_index].client_addr[player_index], sizeof(room_set[room_index].client_addr[player_index])); | |
| room_set[room_index].player_menu_sent_time[player_index] = time(NULL); | |
| } | |
| bool is_integer_value(const char *buffer) { | |
| if (buffer == NULL || *buffer == '\0') { | |
| return false; | |
| } | |
| for (int i = 0; buffer[i] != '\0'; ++i) { | |
| if (!isdigit(buffer[i])) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| void process_game_end() { | |
| char final_results[BUFFER_SIZE] = GAME_END_RESULTS_MESSAGE; | |
| char temp[BUFFER_SIZE]; | |
| for (int i = 0; i < MAX_CLIENTS; i++) { | |
| if (client_set[i].user_fd != -1) { | |
| snprintf(temp, BUFFER_SIZE, PLAYER_FINAL_SCORE_FORMAT, client_set[i].name, client_set[i].num_wins); | |
| strcat(final_results, temp); | |
| } | |
| } | |
| for (int i = 0; i < MAX_CLIENTS; i++) { | |
| if (client_set[i].user_fd != -1) { | |
| send(client_set[i].user_fd, final_results, strlen(final_results), 0); | |
| close(client_set[i].user_fd); | |
| client_set[i].user_fd = -1; | |
| } | |
| } | |
| printf("%s", final_results); | |
| } | |
Xet Storage Details
- Size:
- 28.6 kB
- Xet hash:
- 65a201f2fe204a74dc4ae3a3d0a57dced793985c1abfa0db33b83ef84aacebd9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.