jeudi 13 février 2020

How to implement asio from scratch

Here is some pseudocode that I trying to implement the async io (eg. send/receive).

Event loop waits for a ready to receive event like EPOLLIN:

while (true) {
    if (readyToRecv) {
        onReceived(context);
    }
}

The callback function of ready to receive:

void onReceived(IOContext context)
{
    Buffer data;            // Line 1
    context.asyncRecv(data);// Line 2
    process(data);          // Line 3
    context.asyncSend(data);// Line 4
    ...
}

after received some data I expect the running result go like this:

  1. enter onReceived
  2. Line 1
  3. Line 2
  4. spawn a read job (into a thread pool)
  5. onReceived return
  6. when read job is done reenter the onReceived function
  7. ignore Line 1 & 2, jump to Line 3

I need some ideas about implementation, especially the jump action.

I guess this can be achieved by __LINE__ MACRO with exception handling or setjmp...

Aucun commentaire:

Enregistrer un commentaire