I'm trying to backport a libevent-based library to use ASIO backend (thus avoid multiple event loops in single application). There are other ways to solve the "problem", but I'm interested in this one
I don't see a direct equivalent of event object (or, rather, a handle - because libevent is written in plain C) in Boost::ASIO documentation; boost::asio::strand looks familiar, but it doesn't seem to follow libevent's pattern: create, expect, receive, do work {, repeat }.
What I need is to have a set of object / event / event callback, which can be created and forgotten unless callback (by socket event) happens, running on top of io_service's loop; is there anything like that in Boost?
Boost.Asio does not provide an equivalent to the libevent's events.
In Boost.Asio, one creates an I/O object, such as a socket (1). The program will then initiate an operation, such as socket.async_receive(buffer, &handler) (2) to indicate that it wants data to be read from the socket into buffer, and invoke handler after data has been read. This step is similar to creating a non-persistent pending event in libevent, but one key difference is that Boost.Asio's Proactor will read data from the socket into buffer on behalf of the user, rather than informing the user that data is available to be read. Finally, Boost.Asio will forward the request to the operating system (3).

The operation system will then notify the io_service that data is available to read (4) in a similar fashion to when a libevent event becomes active. At some point, the application will process the event loop via io_service::run() (5), just like one would do with libevent's event_base_loop(). As data is available to be read, Boost.Asio will read data from the socket into buffer, then post user's callback (handler) into a queue of callbacks ready to invoked (6). The application will then invoke the callback while processing the event loop.

Boost.Asio puts a strong focus on the I/O object rather than the operations:
event_free() without affecting the read event. On the other hand, with operations not being identifiable in Boost.Asio, one can only cancel all pending operations, such as via socket.cancel(), rather than being able to cancel a specific operation, such as a write but not a read.Source images and general details can located here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With