/* ** Connexion d un nouveau joueur */ static void set_new_ia(t_world *world, t_cli *ia, t_team *team) { init_position(world, ia); gen_ressource(world); ia->statut = NOACTION; ia->lvl = 1; ia->team = team; init_inventaire(&(ia->invent)); ia->invent.objet[FOOD] = DEFAULT_FOOD; ia->elevation = 0; set_timeval(world, &(ia->t_cur_cmd), NOACTION); set_timeval(world, &(ia->t_alive), LIFETIME); welcome_ia(world, team, ia); if (world->pgui != NULL) gui_pnw_serv(world, ia); }
long int fd_read(int fd, void *buf, size_t count, Net_timeout_t tm) { long int n; int err; fd_set rfd; struct timeval tv; if (fd == -1) return(-1); //** If closed return n = read(fd, buf, count); log_printf(15, "fd_read: fd=%d n=%ld errno=%d\n", fd, n, errno); if ((n==-1) && ((errno == EAGAIN) || (errno == EINTR))) n = 0; if (n == 0) { //** Nothing written to let's try and wait FD_ZERO(&rfd); FD_SET(fd, &rfd); set_timeval(&tv, tm); err = select(fd+1, &rfd, NULL, NULL, &tv); if (err > 0) { n = read(fd, buf, count); log_printf(15, "fd_read2: fd=%d n=%ld select=%d errno=%d\n", fd, n, err,errno); if (n == 0) { n = -1; //** Dead connection } else if ((n==-1) && ((errno == EAGAIN) || (errno == EINTR))) { n = 0; //** Try again later } } } return(n); }
int fd_connection_request(int fd, int timeout) { struct timeval dt; fd_set rfd; if (fd == -1) return(-1); set_timeval(&dt, timeout, 0); FD_ZERO(&rfd); FD_SET(fd, &rfd); return(select(fd+1, &rfd, NULL, NULL, &dt)); }
int fd_connect(int *fd, const char *hostname, int port, int tcpsize, Net_timeout_t timeout) { struct sockaddr_in addr; char in_addr[6]; int sfd, err; fd_set wfd; apr_time_t endtime; Net_timeout_t to; struct timeval tv; log_printf(15, "fd_connect: Trying to make connection to Hostname: %s Port: %d\n", hostname, port); *fd = -1; if (hostname == NULL) { log_printf(15, "fd_connect: lookup_host failed. Missing hostname! Port: %d\n",port); return(1); } // Get ip address if (lookup_host(hostname, in_addr, NULL) != 0) { log_printf(15, "fd_connect: lookup_host failed. Hostname: %s Port: %d\n", hostname, port); return(1); } // get the socket sfd = socket(PF_INET, SOCK_STREAM, 0); *fd = sfd; if (sfd == -1) { log_printf(10, "fd_connect: socket open failed. Hostname: %s Port: %d\n", hostname, port); return(1); } // Configure it correctly int flag=1; if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) != 0) { log_printf(0, "fd_connect: Can't configure SO_REUSEADDR!\n"); } if (tcpsize > 0) { log_printf(10, "fd_connect: Setting tcpbufsize=%d\n", tcpsize); if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (char*)&tcpsize, sizeof(tcpsize)) != 0) { log_printf(0, "fd_connect: Can't configure SO_SNDBUF to %d!\n", tcpsize); } if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (char*)&tcpsize, sizeof(tcpsize)) != 0) { log_printf(0, "fd_connect: Can't configure SO_RCVBUF to %d!\n", tcpsize); } } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); memcpy(&(addr.sin_addr), &in_addr, 4); // memset(&(addr.sin_zero), 0, 8); // Connect it err = connect(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr)); if (err != 0) { if (errno != EINPROGRESS) { log_printf(0, "fd_connect: connect failed. Hostname: %s Port: %d err=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno)); return(1); } // err = 0; } //Configure the socket for non-blocking I/O if ((err = fcntl(sfd, F_SETFL, O_NONBLOCK)) == -1) { log_printf(0, "fd_connect: Can't configure connection for non-blocking I/O!"); } log_printf(20, "fd_connect: Before select time=" TT "\n", apr_time_now()); endtime = apr_time_now() + apr_time_make(5, 0); do { FD_ZERO(&wfd); FD_SET(sfd, &wfd); set_net_timeout(&to, 1, 0); set_timeval(&tv, to); err = select(sfd+1, NULL, &wfd, NULL, &tv); log_printf(20, "fd_connect: After select err=%d\n", err); if (err != 1) { if (errno != EINPROGRESS) { log_printf(0, "fd_connect: select failed. Hostname: %s Port: %d select=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno)); return(1); } else { log_printf(10, "fd_connect: In progress..... time=" TT " Hostname: %s Port: %d select=%d errno: %d error: %s\n", apr_time_now(), hostname, port, err, errno, strerror(errno)); } } } while ((err != 1) && (apr_time_now() < endtime)); if (err != 1) { //** There were problems so return with an error log_printf(0, "fd_connect: Couldn\'t make connection. Hostname: %s Port: %d select=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno)); close(sfd); return(1); } *fd = sfd; return(0); }
void *fill_queue(void *thread_params) { struct timeval basetime, prev_timestamp; int frametype; int quitflag = 0, mutlocked = 0, timeset = 0; Frame *frame; TimeoutEvent *event; ThreadInfo *tinfo = (ThreadInfo *)thread_params; /* set the cancel type to PTHREAD_CANCEL_ASYNCHRONOUS instead of PTHREAD_CANCEL_DEFERRED to get the thread immediately cancelled */ if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) != 0) { puts("pthread_setcanceltype"); } while (!quitflag) { lock_mutex(&queuelock); pthread_cond_wait(&queuecond, &queuelock); mutlocked = 1; if (!timeset) { CHECK((gettimeofday(&basetime, NULL)) == 0); timeset = 1; } oma_debug_print("Starting to fill the queue...\n"); while (queue.size < QUEUESIZE && !quitflag) { if (!mutlocked) { lock_mutex(&queuelock); } frame = (Frame *)malloc(sizeof(Frame)); oma_debug_print("fill_queue: calling get_frame\n"); /* Get the frame. If none are available, end the loop and the entire function. */ if ((frametype = get_frame(tinfo->ctx, frame, tinfo->videoIdx, tinfo->audioIdx)) == -1) { oma_debug_print("EOF from the media file!\n"); quitflag = 1; /* Notify the server about the EOF */ event = (TimeoutEvent *)malloc(sizeof(TimeoutEvent)); event->frame = NULL; event->type = ENDOFSTREAM; event->time = prev_timestamp; event->time.tv_usec += 10; push_event(event, &queue); } else { frame->frametype = (frametype == tinfo->videoIdx)?VIDEO_FRAME:AUDIO_FRAME; event = (TimeoutEvent *)malloc(sizeof(TimeoutEvent)); event->frame = frame; event->type = FRAME; set_timeval(event, basetime); prev_timestamp = event->time; push_event(event, &queue); } unlock_mutex(&queuelock); mutlocked = 0; usleep(1000); } /* End of inner while loop */ oma_debug_print("The queue is full\n"); } /* End of outer while loop */ pthread_exit(NULL); }