Manually registering a socket with Tokio?

Background

I am not very experienced at implementing Futures.

I am using a srt-rs which wraps libsrt via FFI, providing an async interface.

libsrt manages the underlying network sockets, but exposes socket descriptors so that calling code can e.g. test to see if the socket is ready to read (read via libsrt provided functions, not directly).

The srt-rs wrapper utilises those sockets, but doesn't currently register them with the reactor of some async runtime, and instead spawns a background thread to epoll the socket and eventually wake() the async task's waker each time an async operation happens:

I expect this was a quick and simple way to get the wrapper working, but probably leaves room for optimisation.

Question

How can I change this code to register the socket with tokio's runtime and avoid the cost of spawning a thread / epoll set for every read I perform?

I have tried to look at the implementation of tokio::net::UdpSocket for inspiration, but the implementation details are in terms of a number of structs that are private inside of tokio, so I would not be able to reuse them directly, and I not confident I understand them in order to recreate their functionality in a separate crate, if that's even possible.

Can anyone suggest an example crate that does something like what I'm attempting, for me to study, or give any other pointers?

Thank you!

Tokio provides the AsyncFd type for this purpose.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.