我遇到了一个问题,我不确定是kqueue的预期行为,还是我做错了什么。
我需要用kqueue为单个套接字文件描述符安装单独的事件。单独的事件是一个for(sockfd,evfilt_read)和(sockfd,evfilt_write)。但是,当我稍后从kqueue中拉出事件时,我从kqueue中得到两个事件,但这两个事件都包含(event.filter&evfilt_read)和(event.filter&evfilt_write)。
我需要它们分开,否则我不知道哪个事件实际上已经准备好了,读还是写?
下面是我为说明这个问题而整理的一些示例代码。请记住,这只是测试代码,以找出为什么我在每个事件中都得到两个筛选器标志。

int main(int argc, char ** argv) {
    //listening_sockfd = setup listening socket

    //setup kqueue and vars
    int kq = kqueue();
    int res = 0;
    int bufres = 0;
    struct timespec ts = {0};
    struct timespec * tsp = &ts;
    struct kevent ke2[2];

    //install read for listening socket
    struct kevent ke1;
    bzero(&ke1,sizeof(ke1));
    ke1.ident = listening_sockfd;
    ke1.flags = EV_ADD;
    ke1.filter = EVFILT_READ;
    res = kevent(kq,&ke1,1,NULL,0,tsp);

    while(1) {
        new_client:

        //wait for a client to connect
        res = kevent(kq,NULL,0,&ke1,1,NULL);
        if(res < 0) printf("%s\n", strerror(errno));

        if(res > 0) {
            //accept the client
            int clientfd = accept(skinfo.sockfd,NULL,NULL);

            //install read events for client
            bzero(&ke1,sizeof(ke1));
            ke1.ident = clientfd;
            ke1.flags = EV_ADD;
            ke1.filter = EVFILT_READ;
            res = kevent(kq,&ke1,1,NULL,0,tsp);
            if(res < 0) printf("%s\n",strerror(errno));

            while(1) {

                //wait for readable content from the client
                bzero(&ke1,sizeof(ke1));
                res = kevent(kq,NULL,0,&ke1,1,NULL);
                if(res < 0) printf("%s\n",strerror(errno));

                if(res > 0) {

                    //check for client disconnect
                    if(ke1.flags & EV_EOF) {
                        close(clientfd);
                        goto new_client;
                    }

                    //now install write events for client
                    bzero(&ke1,sizeof(ke1));
                    ke1.ident = clientfd;
                    ke1.flags = EV_ADD;
                    ke1.filter = EVFILT_WRITE;
                    res = kevent(kq,&ke1,1,NULL,0,tsp);
                    if(res < 0) printf("%s\n",strerror(errno));

                    //now wait for it to be writable - this will return
                    //immediately because the socket is writable.
                    res = kevent(kq,NULL,0,ke2,2,NULL);
                    if(res < 0) printf("%s\n",strerror(errno));

                    if(res >= 0) {
                        char buf[128];
                        printf("res: %i\n",res);

                        //we have two events from kqueue because I installed
                        //two (ident,filter) pairs.
                        int i = 0;
                        for(i; i<2; i++) {
                            printf("ident: %i\n",ke2[i].ident);
                            printf("filter[%i] %lu\n",i,ke2[i].filter);
                            printf("data: %lu\n",ke2[i].data);

                            //QUESTION: Why does EVFILT_READ && EVFILT_WRITE come
                            //back in the same event when I installed two seperate
                            //(ident,filter) pairs?
                            if(ke2[i].filter & EVFILT_READ) printf("EVFILT_READ\n");
                            if(ke2[i].filter & EVFILT_WRITE) printf("EVFILT_WRITE\n");

                            if(ke2[i].filter & EVFILT_READ) {
                                printf("readable!\n");
                                bufres = read(clientfd,buf,128);
                            }

                            if(ke2[i].filter & EVFILT_WRITE) {
                                printf("writable\n");

                                //shut off write events to stop infinite loop
                                //because the socket is writable
                                bzero(&ke1,sizeof(ke1));
                                ke1.ident = clientfd;
                                ke1.flags = EV_DELETE;
                                ke1.filter = EVFILT_WRITE;
                                res = kevent(kq,&ke1,1,NULL,0,tsp);

                                write(clientfd,buf,bufres);
                            }
                        }
                    }
                }
            }
        }
    }
}

我还没搞清楚。为什么在安装单独的事件时,每个kqueue事件都包含evfilt_read和evfilt_write?
这给我带来的真正问题是,因为这些事件总是报告evfilt_read,它有副作用,因为代码总是认为有一个read可用,但实际上没有,所以调用read()将不成功并产生其他后果。请注意,这段代码并没有显示这些结果,这只是我编写的问题的一个示例,因此我会解决这个问题并继续使用我的实际代码。
有什么想法吗?
ps这是printf的输出:
res: 2
ident: 5
filter[0] 4294967295
data: 5
EVFILT_READ
EVFILT_WRITE
readable!
writable
ident: 5
filter[1] 4294967294
data: 146988
EVFILT_READ
EVFILT_WRITE
readable!
writable

最佳答案

啊!我想我明白了。kqueue文档没有说明event.filter不包含位标志。所以我需要用event.filter==evfilt_read,event.filter==evfilt_write检查读/写。

07-24 17:41