Search code examples
cepoll

How can I make sure I read a complete message when using epoll


I have a few questions about epoll:

  1. According to Linux man-page:

Since even with edge-triggered epoll, multiple events can be generated upon receipt of multiple chunks of data, the caller has the option to specify the EPOLLONESHOT flag, to tell epoll to disable the associated file descriptor after the receipt of an event with epoll_wait(2). When the EPOLLONESHOT flag is specified, it is the caller's responsibility to rearm the file descriptor using epoll_ctl(2) with EPOLL_CTL_MOD.

How if I don't use EPOLLONESHOT and I read until I get EAGAIN, will I lose data? I have ckecked the source code of Libevent, they don't use EPOLLONESHOT.

  1. How to make sure we receive the whole request data? One solution is to add length in request data. Any other solution?

Solution

  • If you don't use EPOLLONESHOT and you always read until you get EAGAIN, there is no reason (except a bug in your code), why you should miss any incoming data. In this case it doesn't matter, if you use level-triggering or edge-triggering. So it is safest to always read until you get EAGAIN.

    If you use EPOLLONESHOT, you also need to make sure, that you rearm the file descriptor. This is unnecessary complexity in most cases, so if you don't know why you need it, you don't need it.

    Your second question is not directly related to epoll. The method you use for detecting beginning, end and completeness of your request depends on what protocols you are using. Having length somewhere in your request could be one of the options.