Smithay / calloop

A callback-based Event Loop
MIT License
177 stars 35 forks source link

Composing event sources #207

Open vikigenius opened 3 weeks ago

vikigenius commented 3 weeks ago

I am trying to create an event source for Unix domain sockets to use in an IPC Server. I tried to follow the zmq example in the book and have a working solution. But I have some design questions. Becasue there are some differences in the event sources between a ZMQ socket and a Unix socket.

With unix sockets the event sources are split into two. The listener that accepts connections. And the UnixStream that sends and receives messages.

Is it better to keep these sources separate or have a composed EventSource like this

pub struct IPCSource {
    socket_source: calloop::generic::Generic<calloop::generic::FdWrapper<UnixListener>>,
    connections: HashMap<&str, calloop::generic::Generic<calloop::generic::FdWrapper<UnixStream>>>,
}

Writ now I have two separate Event Sources one for the Listener and one for the UnixStream (there will be multiple UnixStreams because of multiple connections).

But I am wondering if I can gain some benefit by composing them like this or something similar?

elinorbgr commented 2 weeks ago

If having multiple sources works well for you, I don't think you'd gain much by composing everything into a single event source. So unless you think doing so would significantly improve your program structure, I'd recommend against it.

Having a single source with a dynamically changing number of underlying fds would mean it'd end up being regularly reregistered, which would be rather wasteful.