epoll with edge triggered event

前端 未结 2 1317
走了就别回头了
走了就别回头了 2020-12-29 18:09

The man page of epoll has a sample code for edge triggered like the following :

for (;;) {
    nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
    if          


        
2条回答
  •  不知归路
    2020-12-29 18:30

    when you use Edge Triggered under epoll, read some thing, can like this

    int n = -1;
    while (1)
    {
        n = recv(fd, iobuf, init_buff_size, MSG_DONTWAIT);
        if (n > 0)
        {
            LOG(glogfd, LOG_TRACE, "fd[%d] recv len %d\n", fd, n);
            mybuff_setdata(&(curcon->recv_buff), iobuf, n); // this is my func
            if (n == init_buff_size)
            {
                LOG(glogfd, LOG_DEBUG, "fd[%d] need recv nextloop %d\n", fd, n);
                continue;
            }
            break;
        }
        if (n == 0)
        {
            LOG(glogfd, LOG_ERROR, "fd[%d] close %s:%d!\n", fd, ID, LN);
            return do_close(fd);
        }
        if (errno == EINTR)
        {
            LOG(glogfd, LOG_TRACE, "fd[%d] need recv again!\n", fd);
            continue;
        }
        else if (errno == EAGAIN)
        {
            LOG(glogfd, LOG_TRACE, "fd[%d] need recv next!\n", fd);
            modify_fd_event(fd, EPOLLIN);   // this is the KEY, add read again
            break;
        }
        else
        {
            LOG(glogfd, LOG_ERROR, "fd[%d] close %s:%d!\n", fd, ID, LN);
            return do_close(fd);
        }
    }
    

提交回复
热议问题