Skip to content

vanshjangir/server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TCP server for persistent connections

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

Usage

The create_server function receive three arguments

  1. A server_args struct
  2. Maximum number of clients to be handled
  3. 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).

How it works

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.

Useful

  1. A million web socket connections
  2. Fix EPOLL

Releases

No releases published

Packages

No packages published

Languages