The aim is to handle multiple live, concurrent connections on a single machine with minimum latency in each connection. It is done by using thread pooling and EPOLL
The create_server function receive three arguments
- A server_args struct
- Maximum number of clients to be handled
- Maximum numer of threads to be created
If a client is sending a message the function provided in server_args will be called (see example).
All the file descriptors are added to the epoll structure. When a file descriptor has a new connection or some data to read, that fd is added in the ready state (by the kernel in the background). When the epoll_wait() is called, it returns the number of file descriptors that are in the ready state and the file descriptors are added in the epoll_arr. If the fd is s_socket then it means there is a new connection so a new client is added. If the fd is a client and there is data to read then a new task is added in the QUEUE. All the threads are waiting for a task to get. Thundering Herd problem is solved by using mutex and conditional variables. If the fd is a client and it has lost the connection then the fd is removed from the epoll structure.