void start_server(int port, void (*post_cb) (post_content_t*))
{
    int listen_fd;
    listen_fd = get_tcp_socket();
    set_enable_port_reuse(listen_fd);
    bind_socket(listen_fd, port);
    prepare_listen(listen_fd, 5);

    intlist_t* socklist = intlist_add(NULL, listen_fd);

    fd_set readfds;
    int r;
    int max;
    while(true){
        FD_ZERO(&readfds);
        intlist_t* cursor = socklist;
        while(cursor != NULL){
            FD_SET(cursor->value, &readfds);
            cursor = cursor->next;
        }
        max = intlist_max(socklist);
        r = select(max + 1,
                   &readfds,
                   NULL,
                   NULL,
                   NULL);
        if(r == -1){
            perror("select error");
            exit(EXIT_FAILURE);
        }
        else{
            intlist_t* cursor = socklist;
            while(cursor != NULL){
                if(FD_ISSET(cursor->value, &readfds)){
                    if(cursor->value == listen_fd){
                        int client_fd = accept_socket(cursor->value);
                        intlist_add(socklist, client_fd);
                    }
                    else{
                        read_socket(cursor->value, post_cb);
                        // TODO: write HTTP/1.1 200 OK if read success
                        close(cursor->value);
                        intlist_del(socklist, cursor->value);
                    }
                }
                cursor = cursor->next;
            }
        }
    }
}
示例#2
0
文件: piracy.c 项目: philbooth/server
int *parse_ids(const order *ord) {
    const char *s;
    int *il = NULL;

    init_order(ord);
    s = getstrtoken();
    if (s != NULL && *s) {
        il = intlist_init();
        while (s && *s) {
            il = intlist_add(il, atoi36(s));
            s = getstrtoken();
        }
    }
    return il;
}