any1 / neatvnc

A liberally licensed VNC server library with a clean interface
ISC License
118 stars 29 forks source link

External event loop #22

Closed agners closed 4 years ago

agners commented 4 years ago

Hi,

This implements support for external event loop as discussed in #16. It is somewhat rough and obviously breaks API, but seems to work with an adopted Weston VNC backend: https://gitlab.freedesktop.org/agners/weston/-/commits/rfc-vnc-support-with-neatvnc-wayland-event-loop

any1 commented 4 years ago

I've actually been working on a main loop library to go with neatvnc and wayvnc. Because it doesn't try to be super fast and use epoll/kqueue with io-threads and what not, it's super simple. It doesn't need to service many users for this application, so it can be simple. Also, one can define callbacks to handle fds, timers and signals.

It may sound a little silly to implement one's own event loop library, but the rationale here is that it'll be much simpler than the ones that try to be "the fastest event loop library in the west", so it should be easier to reason about and fix bugs.

I'll share my work with you.

agners commented 4 years ago

Library looks neat :-)

From my perspective the most important feature would be to have support for integrating it into another event loop library. I am not 100% sure how that is usually implemented, I was always under the assumption that epoll is used for that? Do you have plans for this feature?

any1 commented 4 years ago

Well, things would actually be a lot simpler if I could just use epoll, but epoll isn't portable. That's why it must either be imitated using threads and poll()/select() or allow the user to register callbacks.

The main feature of this library is that it can be integrated with other event loops and in that case it should function as a thin abstraction layer on top. I was thinking that I might actually even include backends for the most common event loops in the code base. That way people wouldn't even have to write integration code when using the library.

Getting different event loops to play together nicely is a pain point for many developers. Maybe this library can be a toolkit for solving that problem.

The total list of goals should look something like this:

Non-goals:

any1 commented 4 years ago

The repo is now public. Here is an example of how the library can act as a wrapper around another event loop library: https://github.com/any1/aml/blob/master/examples/uv.c

There would be less integration code to write if I allowed myself to just use epoll and kqueue, but I will add built-in backends for that later, in which case the user might allow himself to be lazy and just use aml_get_fd().

Anyhow, the whole thing is only 1700 lines of code, whereas libuv is 39000 lines of code. I've a feeling that my little library should be much easier to debug. :)

any1 commented 4 years ago

I tried replacing libuv with aml here: https://github.com/any1/neatvnc/tree/aml wayvnc branch is here: https://github.com/any1/wayvnc/tree/aml

I might try creating a wl_event backend for aml tomorrow.

any1 commented 4 years ago

@agners, wayland-server integration example is here: https://github.com/any1/aml/blob/master/examples/wayland.c Now I just need to make it so that aml's thread pool can be used with event loops that don't have their own thread pool, and then you should be all set for further weston work.

any1 commented 4 years ago

Neat VNC now uses aml. To integrate with it, users should implement the appropriate event backend for aml. See example in prior comment.