int handle_client(int id) { uint8_t rx_cmd; int nrx = recv(client_tab[id].fd, &rx_cmd, 1, 0); if(nrx<0) return -1; if(nrx == 0) return 0; switch(rx_cmd) { case SVCMD_POS: // fprintf(stderr,"Send!\n"); return send_position(client_tab[id].fd, last_pos); case SVCMD_START_STREAMING: client_tab[id].state = CLIENT_STATE_STREAMING; return 0; case SVCMD_STOP_STREAMING: client_tab[id].state = CLIENT_STATE_IDLE; return 0; case SVCMD_END_SESSION: return -1; default: return -1; } }
void navigate_path(){ int bool; struct timespec wait; wait.tv_sec = 0; wait.tv_nsec = SLEEP_DURATION; reset_timer(); set_direction(¤t); set_distance(¤t); send_direction(¤t.current_destination.angle); send_distance(¤t.current_destination.distance); while(running == 1){//Infinite loop until it reaches point or collision avoidance occurs. printf("%d\n", current.num); update_position(¤t); send_position(¤t.current_point); if(check(current.current_point, current.current_destination) == 1){ if(check(current.current_point, current.ending_point) == 1){ current.path[current.num] = current.current_destination; current.num++; count++; current.path = realloc(current.path, sizeof(position) * (current.num+1)); if(current.path == NULL){ printf("!!!!!\n"); send_stop(); } send_stop(); bool = 0; free(current.path); free(route.path); break; } else{ current.path[current.num] = current.current_destination; current.num++; count++; printf("%d\n", sizeof(position) * (current.num+1)); current.path = realloc(current.path, sizeof(position) * (current.num+1)); if(current.path == NULL){ printf("!!!!!\n"); send_stop(); } current.current_destination = route.path[count]; bool = 1; break; } }
int com_avance(t_server *s, char **tab, int sock) { t_client *client; UNUSED(tab); client = s->client; while (client != NULL) { if (client->sock == sock && client->type == 1) { avance(s, client); if (s->sock_graph > 0) send_position(client, s->sock_graph); return (return_ok(sock)); } client = client->next; } return (0); }
void handle_streaming() { int i=0, j, npos = 0; struct pos_ang pos_buf[POS_QUEUE_DEPTH]; pthread_mutex_lock(&pos_queue_lock); while(!pos_queue_get(&pos_buf[npos])) npos++; pthread_mutex_unlock(&pos_queue_lock); if(npos) { memcpy(&last_pos, &pos_buf[npos-1], sizeof(struct pos_ang)); for(i=0; i<MAX_CLIENTS; i++) { if(client_tab[i].state == CLIENT_STATE_STREAMING && client_tab[i].fd>0) { for(j=0;j<npos;j++) send_position(client_tab[i].fd, pos_buf[j]); } } } }