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

gng*_*zrd 4 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);
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚这一点.当我安装单独的事件时,为什么每个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
Run Code Online (Sandbox Code Playgroud)

gng*_*zrd 8

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


Pot*_*ter 6

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

查阅 OS X 上的标头,我们发现

#define EVFILT_READ             (-1)
#define EVFILT_WRITE            (-2)
Run Code Online (Sandbox Code Playgroud)

由于-1全部为 1 位,x & EVFILT_READ因此对于任何非零都为 true x,对于不等于 0 或 1x & EVFILT_WRITE也为 true 。x