Exemple #1
0
int main(void)
{
    int h = 0;
	int f = 1;
    int clientfd, maxfd, nready, i, result;
    socklen_t len;
    struct sockaddr_in q;
    fd_set allset, rset;
	struct queue_list *clientlist = q_init(); //client fds
	struct queue_list *battlelist = q_init(); //battles
	struct queue_list *playerlist = q_init(); //players w/ stats, name etc.
	  
	int listenfd = bindandlisten();
    maxfd = listenfd;

    FD_ZERO(&allset);
    FD_SET(listenfd, &allset);
    

    while (1)
    {
        printf("server on %d iteration\n", h);
        h++;
        if (f == 1)
        {
            printf("server initialized! \n");
            f++;
        }
        rset = allset;
        		
		struct node *bb = battlelist->front;
        while (bb)
        {
			if (bb->content->status == 0)
			{
                 battle_end(bb->content, playerlist, 0);
			}
			bb = bb->next;
        }

        printf("attempting to create matches: ");
        if (matchmaking(playerlist, battlelist) == 1)//find match for two clients each iteration
        {
            printf("Battle created with pid %d !\n", bid);
			bid = bid + 1;
        }
        
        nready = select(maxfd + 1, &rset, NULL, NULL, NULL); //check if there is any client active


        if (nready == 0) // if no client is active
        {
            continue; //go back to the beginning.
        }

        else if (nready == -1) //error
        {
            perror("select");
            continue;
        }


        if (FD_ISSET(listenfd, &rset)) //check if listenfd active that connected by any client
        {
            printf("a new client is connecting\n");
            len = sizeof(q);
            
            if ((clientfd = accept(listenfd, (struct sockaddr *)&q, &len)) < 0)  //accept the client, *i think* it will reset listenfd to inactive
            {
                perror("accept");
                exit(1);
            }
            
            FD_SET(clientfd, &allset); //add the client fd in to the set of fd
   
            write(clientfd, "What is your name?\r\n", 20);

            if (clientfd > maxfd) 
            {
                maxfd = clientfd; //update max # of fd if needed
            }
            
            printf("connection from %s\n", inet_ntoa(q.sin_addr));
            addclient(clientfd, q.sin_addr, clientlist);

        }
        
        struct node *p; 
        for(i = 0; i <= maxfd; i++) //iterate through fd set and
        {
            if (FD_ISSET(i, &rset)) //check each connected client fd in the set of fd, if anyone becomes active.
            {
                for (p = clientlist->front; p != NULL; p = p->next)  //TODO: change this for new data structure
                {
                    if (p->content->fd == i)
                    {          
                        result = handle(clientlist, playerlist, p->content);
                        
                        if (result == -1 || result == 5) 
                        {
                            if (result == 5)
                            {
                                bid = battle_end(p->content->player, playerlist, 1);
                                printf("battle %d drops due to client disconnected\n", bid);
                            }
                            
							int tmp_fd = p->fd;
                            removeclient(p->content, clientlist);
                            FD_CLR(tmp_fd, &allset);
                            close(tmp_fd);
                        }
                        
                        break; 
                    }
                }
            }
        }
    }
    

}
Exemple #2
0
int main(void) {
    int clientfd, maxfd, nready;
    struct client *p;
    struct client *head = NULL;
    socklen_t len;
    struct sockaddr_in q;
    struct timeval tv;
    fd_set allset;
    fd_set rset;
    srand (time(NULL));

    int i;

    //Initialize a listening fd of the server.
    int listenfd = bindandlisten();

    // initialize allset and add listenfd to the
    // set of file descriptors passed into select
    FD_ZERO(&allset);
    FD_SET(listenfd, &allset);

    // maxfd identifies how far into the set to search
    maxfd = listenfd;

    while (1) {//keep running
        // make a copy of the set before we pass it into select
        rset = allset;
        /* timeout in seconds (You may not need to use a timeout for
        * your assignment)*/
        tv.tv_sec = 2; // change to 0 from 10
        tv.tv_usec = 0;  /* and microseconds */

        nready = select(maxfd + 1, &rset, NULL, NULL, &tv); //Select a ready fd descriptor(client) to read.
        if (nready == 0) {
            printf("No response from clients in %ld seconds\n", tv.tv_sec);
            continue;
        }

        if (nready == -1) {
            perror("select");
            continue;
        }

        if (FD_ISSET(listenfd, &rset)) {
            printf("a new client is connecting\n");
            len = sizeof(q);
            if ((clientfd = accept(listenfd, (struct sockaddr *)&q, &len)) < 0) {
                perror("accept");
                exit(1);
            }
            FD_SET(clientfd, &allset);
            if (clientfd > maxfd) {
                maxfd = clientfd;
            }
            printf("connection from %s\n", inet_ntoa(q.sin_addr));
            //sprintf("what is your name?");
            head = addclient(head, clientfd, q.sin_addr);
        }
        //Handle every client
        //printf("running\n");
        for(i = 0; i <= maxfd; i++) {
            if (FD_ISSET(i, &rset)) {
                for (p = head; p != NULL; p = p->next) {
                    if (p->fd == i) {//
                        int result = handleclient(p, head);
                        if (result == -1) {
                            int tmp_fd = p->fd;
                            head = removeclient(head, p->fd);
                            FD_CLR(tmp_fd, &allset);
                            close(tmp_fd);
                        }
                        break;
                    }
                }
            }
        }
    }
    return 0;
}