Search code examples
ciononblockingbuffered

How can I buffer non-blocking IO?


When I need buffered IO on blocking file descriptor I use stdio. But if I turn file descriptor into non-blocking mode according to manual stdio buffering is unusable. After some research I see that BIO can be usable for buffering non-blocking IO.

But may be there are other alternatives?

I need this to avoid using threads in a multi-connection environment.


Solution

  • I think what you are talking about is the Reactor Pattern. This is a pretty standard way of processing lots of network connections without threads, and is very common in multiplayer game server engines. Another implementation (in python) is twisted matrix.

    The basic algorith is:

    • have a buffer for each socket
    • check which sockets are ready to read (select(), poll(), or just iterate)
    • for each socket:
      • call recv() and accumulate the contents into the socket's buffer until recv returns 0 or an error with EWOULDBLOCK
      • call application level data handler for the socket with the contents of the buffer
      • clear the socket's buffer