Conversation
Co-authored-by: Ryan Butler <thebutlah@gmail.com> Co-authored-by: Kevin Mehall <km@kevinmehall.net>
|
Edited the description to reflect the fact that the Additionally, the The |
|
@a1ien Would love some feedback on this, especially around the error handling situation. I personally believe that integrating the async stuff into That would allow reusing the |
|
Hi, thank you for you effort. The rusb project deserves more attention than I currently have. I will promise that i will take a look this PR. |
|
@a1ien I could step up to help if you want, but perhaps it's worth discussing what that would entail and how it would work in terms of changes, code reviews, etc. |
Co-authored-by: Aytixel <36609903+Aytixel@users.noreply.github.com>
Awesome! Thanks for taking the time to test it out! |
Hey there 👋 ,
I'm new to the
libusbworld and, by extension,rusb, but I'm currently using it in a personal embedded project. To give some context, the host side of the project is a tray icon implemented throughgtk-rswhich does very little most of the time and is in no way UI intensive. So I started wondering if, instead of the sync interface, I could integrate the async API oflibusbinto theglibevent loop to keep things as snappy as possible while still staying single-threaded.I saw the efforts from the
rusb-asyncbranch but I felt like there could be so much more to this, so I took it into my own hands to implement proper aFuturebasedlibusbintegration. The integration is meant to be runtime agnostic and could in fact support event handling in a dedicated async background task or even a background thread.But that's more for platforms like Windows where file descriptors are not available. For UNIX-like systems, I implemented a
FdEventHandlerwith aFdCallbackstrait interface that allows creating event sources from the file descriptorslibusbuses for direct integration in event loops. I did that successfully forgliband I also added some examples that show how to do this intokio.I haven't yet published my project because I still have to write docs for it and I got sidetracked with this, but when I do I could add a link as a reference for how the
glibintegration works. It's really not that different fromtokio, and I strongly suspect it wouldn't be very different regardless of the runtime used.With that being said, I know the implementation here still needs some polishing, but I wanted to open a dialog and look for some reviews on how it looks like so far. At the top of my head, the current things come to mind:
rusbitself, or will it live in a separate crate? That can dictate whether to reuse therusberror type or have one dedicated for the async operations. Based on this one error type needs to be extended.TheI explored the idea ofFdEventHandleris a bit quirky in the sense that it should only be constructed once perContext. Or rather, at most oneFdEventHandlershould be alive at any given time for each context. Otherwise things get confusing since the file descriptor even callbacks get registered/unregistered in a weird order. Maybe it's worth exploring introducing some sort ofAsyncContext?AsyncContextand ended up implementing it. The event handler registration is done through a static map and theAsyncUsbContexttrait was introduced to separate sync contexts from async ones.GlobalContextdoes implementAsyncUsbContext, but event handler registration must be explicit.UsbContextso thatAsyncContextcannot be used for event handling directly, just to kinda restrict the users from doing that instead of setting an event handler. That, however, implies messing with the current state ofrusband would be a breaking change. Might be worth doing if the async part will be integrated intorusbthough.I'll add things to the list as they come to mind and as people get to experiment with this more. Personally I only use interrupt transfers in my project, so I can't really vouch for them all. But the implementation is based on the
rusb-asyncbranch.I also could not help but notice #217 so I took into account the suggested changes there as well and came up with a nicer interface for isochronous endpoints.
I'm willing to work more on this so that it hopefully gets merged in as the de facto way of doing async with
rusb, but I'd highly appreciate some assistance in order to get there faster.