static int run_broadcast(t_srv *srv, t_client *client, t_client *tmp, char *cmd1) { int direction; while (tmp != NULL) { if (tmp->type == FD_CLIENT) { if (tmp->fd != client->fd && tmp->position.x == client->position.x && tmp->position.y == client->position.y) { if (broadcast_client(srv, tmp, 0, cmd1) == FAILURE) return (FAILURE); } else if (tmp->fd != client->fd) { if ((direction = search_bro_direction(client, tmp, srv)) == FAILURE) return (FAILURE); if (broadcast_client(srv, tmp, direction, cmd1) == FAILURE) return (FAILURE); } } tmp = tmp->next; } return (SUCCESS); }
void *client_thread(void *args){ printf("Client connected\n"); CLIENT_OBJ *client = (CLIENT_OBJ *)args; char *buff = (char *)malloc(1024 * 8); //Read in 8k at a time should be more than enough int bytes_read; bool messages_empty; char *buff2 = (char *)malloc(1024 * 8); char *path = (char *)malloc(1024); char *temp_buff = (char *)malloc(1024 * 8); memset(temp_buff, '\0', 1024 * 8); char *temp_buff2 = (char *)malloc(1024 * 8); memset(temp_buff2, '\0', 1024 * 8); read(client->client_socket, buff2, USER_NAME_LEN * 4); printf("user name connected %s on socket %d\n", buff2, client->client_socket); memset(client->user_data->name, '\0', USER_NAME_LEN * 4); get_user_name_message(buff2, client->user_data->name); memset(path, '\0', 1024); buff2[0] = '\0'; create_uid_message(client->user_data->uid, buff2); printf("UID MSG: %s", buff2); write(client->client_socket, buff2, strlen(buff2)); free(buff2); sprintf(path, "./%s", client->user_data->name); //mkdir(path, 777); free(path); for(;;){ printf("Begin client loop\n"); pthread_testcancel(); pthread_mutex_lock(&mutex); messages_empty = isEmpty(client->messages); pthread_mutex_unlock(&mutex); if(!messages_empty){ memset(buff, '\0', 1024 * 8); bytes_read = read(client->client_socket, buff, 1024*8); client->bytes_from += bytes_read; if(bytes_read == 0){ sprintf(temp_buff2, "%s has logged off.", client->user_data->name); create_main_chat_message(client->user_data->uid, temp_buff2, temp_buff); for(int i = 1; i <= 10; i++){ if(client->user_data->uid != 1) broadcast_client(client->server->clients[i], temp_buff); } break; } //printf("Adding To message queue: %s\n", buff); pthread_mutex_lock(&mutex); enqueue(client->messages, buff); pthread_mutex_unlock(&mutex); sem_post(&messages_sem); } else{ memset(buff, '\0', 1024 * 8); bytes_read = read(client->client_socket, buff, 1024*8); client->bytes_from += bytes_read; if(bytes_read == 0) break; //printf("Adding to message queue in else block: %s\n", buff); pthread_mutex_lock(&mutex); enqueue(client->messages, buff); pthread_mutex_unlock(&mutex); sem_post(&messages_sem); //Lets the main thread know there are messages to process } } free(temp_buff); free(temp_buff2); printf("Closing Client begin\n"); pthread_mutex_lock(&mutex); client->bytes_from = 0; client->bytes_to = 0; client->is_connected = false; client->seconds_connected = 0; close(client->client_socket); client->server->num_users_connected -= 1; pthread_mutex_unlock(&mutex); printf("closing client end\n"); pthread_exit(0); }