tidy code for asynchronous IO

后端 未结 5 1085
醉话见心
醉话见心 2020-12-12 13:15

Whilst asynchronous IO (non-blocking descriptors with select/poll/epoll/kqueue etc) is not the most documented thing on the web, there are a handful of good examples.

<
5条回答
  •  误落风尘
    2020-12-12 13:48

    You want to decouple "io" from processing, at which point the code you read will become very readable. Basically you have:

    
        int read_io_event(...) { /* triggers when we get a read event from epoll/poll/whatever */
    
         /* read data from "fd" into a vstr/buffer/whatever */
    
         if (/* read failed */) /* return failure code to event callback */ ;
    
         if (/* "message" received */) return process_io_event();
    
         if (/* we've read "too much" */) /* return failure code to event callback */ ;
    
         return /* keep going code for event callback */ ;
        }
    
    
        int process_io_event(...) {
           /* this is where you process the HTTP request/whatever */
        }
    

    ...then the real code is in process event, and even if you have multiple requests responses it's pretty readable, you just do "return read_io_event()" after setting a state or whatever.

提交回复
热议问题