int connect_nbr(t_player *player, int nb, char *s2) { char *buff; t_elem *ptr; (void)nb; (void)s2; player->time = -1; if ((buff = malloc(8)) == NULL) { erase_client(player, 1); return (FAIL); } ptr = g_serv.opt.team.first; while (ptr != NULL) { if (strcmp(((t_team*)ptr->data)->name, player->team) == 0) { sprintf(buff, "%i", ((t_team*)ptr->data)->nb_player); break ; } ptr = ptr->next; } if (append(&player->send, buff) == FAIL) erase_client(player, 1); return (NONE); }
void handle_player(t_player *player, char buff[]) { int len; char *tmp; if (player->cmd_buff == NULL) len = 0; else len = strlen(player->cmd_buff); if ((player->cmd_buff = realloc(player->cmd_buff, 256 + len)) == NULL) erase_client(player, 1); else { strcpy(player->cmd_buff + len, buff); while (player->cmd_buff && strchr(player->cmd_buff, '\n') != NULL) { if (player->cmd.nb_elem < 10 && handle_player2(player, &tmp) == FAIL) return ; else if (player->cmd.nb_elem >= 10) tmp = NULL; free(player->cmd_buff); player->cmd_buff = tmp; } } }
void *manage_waiting_player(t_elem *ptr, float time) { t_player *player; player = ptr->data; ptr = ptr->next; player->time -= time; if (player->time <= 0.0000) { close(player->fd); erase_client(player, 1); } return (ptr); }
/* deallocate memory for the player */ void free_player( esd_player_t *player ) { esd_sample_t *sample; /* see if we need to do any housekeeping */ if ( ( player->format & ESD_MASK_MODE ) == ESD_STREAM ) { /* TODO: erase client should be independent of players */ erase_client( player->parent ); } else if ( ( player->format & ESD_MASK_MODE ) == ESD_SAMPLE ) { sample = (esd_sample_t *) (player->parent); sample->ref_count--; esd_playing_samples--; ESDBG_TRACE( printf( "<%02d> free player: [%p] refs=%d erase?=%d samps=%d\n", player->source_id, player, sample->ref_count, sample->erase_when_done, esd_playing_samples ); );
int handle_player2(t_player *player, char **tmp) { char *s; s = strtok(player->cmd_buff, "\n"); if (s != NULL && (s = strdup(s)) == NULL) return (FAIL); if (append(&player->cmd, s) == FAIL) { erase_client(player, 1); return (FAIL); } *tmp = strtok(NULL, ""); if (*tmp != NULL && (*tmp = strdup(*tmp)) == NULL) return (FAIL); return (NONE); }
void *handle_client(t_elem *player) { t_player *data; char buff[256]; int len; data = player->data; player = player->next; if ((len = read(data->fd, buff, 255)) > 0) { buff[len] = 0; if (data->state == WAITING) handle_waiting_player(data, buff); else handle_player(data, buff); } else erase_client(data, 1); return (player); }
void connect_client(t_player *player, t_team *team) { t_elem *list; void *data; if (connect_client2(player) == FAIL) return ; list = g_serv.graphic.first; while (list != NULL) { data = list->data; list = list->next; if (pnwg(data, player) == FAIL || ping(data, player) == FAIL) erase_graphic(data); } team->nb_player -= 1; dprintf(player->fd, "%i\n%i %i\n", team->nb_player, g_serv.opt.x, g_serv.opt.y); if (append(&g_serv.map[player->y][player->x].players, player) == FAIL) erase_client(player, 1); }