Kqueue返回EVFILT_READ和EVFILT_WRITE,但我安装了单独的(ident,filt)对

时间:2012-08-28 18:18:47

标签: c kqueue

我遇到了一个问题,我不确定是否是来自kqueue的预期行为,或者是否是我做错了。

我需要使用kqueue为单个套接字文件描述符安装单独的事件。单独的事件是(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()的调用将是不成功并有其他后果。请注意,此代码没有显示这些后果,这只是我写的问题的一个示例,所以我想出了这个并继续我的真实代码。

有什么想法吗?

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

2 个答案:

答案 0 :(得分:8)

AH!我想我明白了。 kqueue文档没有说的是event.filter不包含位标志。所以我需要使用event.filter == EVFILT_READ,event.filter == EVFILT_WRITE来检查读/写。

答案 1 :(得分:3)

正如您所注意到的,它们不是要测试的部分。

咨询OS X上的标题,我们找到了

#define EVFILT_READ             (-1)
#define EVFILT_WRITE            (-2)

由于-1都是1位,x & EVFILT_READ对于任何非零x都是正确的,而x & EVFILT_WRITE对于x不等于零或一。