Search code examples
socketseventsserverfile-descriptorepoll

Can an Epoll FD handle two Epollin events differently?


Intro:

Suppose we have a server, running a single thread, which manages evetns via epoll. We also have two clients A,B which are connected to the server via socket. If now A or B send a message to the server normally an epollin event is triggered and this is processed e.g. with method a(). This means that the epollin events for both clients are processed with exactly the same method a().

Desired:

Is there a way to structure this in a way that the epollin events triggered by two different clients are processed with two different methods? E.g. A sends a message to the server. The Epoll Fd detects an Epollin event. This is processed with mehtode a(). B sends a message to the server. The Epoll Fd again detects an Epollin event. However, this is processed with mehtode b().


Solution

  • epoll itself does not associate a specific callback with a file descriptor. It just returns via epoll_wait on which file descriptors an event occurred and which kind of event. It is fully up to the application on what to do with this information, like handle it directly in the same function where epoll_wait was called, call a single function a() for all Epollin events or call different functions a(), b(), .... for Epollin on different file desriptors.