[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] Asynchronous writes in the event loop model



Hello everyone,

After reading the libevent documentation and hacking on some of the code samples, I've got a question about doing 'asynchronous' socket writes using the event loop model.

I completely understand how libevent can be used to create an HTTP (or alike) server, thanks to that protocol's request-response model. Most examples write the response back to the client after reading enough bytes in a read callback; very easy to grok and an obviously excellent fit for libevent's event loop model.

However, I am wondering how to best do 'spontaneous' or 'asynchronous' writes that originate not from a client's response, but from a different source.

As an example, consider a situation where network clients connect to a server to be notified of a simple 'event' like a button press on the server keyboard. The basic architecture would look something like this:

[ Thread 1 - "main" ]
step 1 - wait for the 'event' like a button press
step 2 - notify all clients
step 3 - goto step 1

[ Thread 2 - "libevent event loop" ]
events - listen for new connections (and optionally read some data from the client)

What's the best way to inject messages to be written out from thread 1 into the libevent loop on thread 2? Note that the button press is purely hypothetical, any form of external, non-libevent 'event' will suffice.

I would like to be able to send messages consisting of several kilobytes of data, so I'm looking towards libevent's buffers for abstracting the byte-level message logistics.

Right now, I'm thinking about designing my application using three threads, but I'm not sure if it makes any sense:
- thread 1 would handle all application logic and generate the messages to be sent
- thread 2 would be a thread running a libevent loop for accepting and reading sockets
- thread 3 would be a thread running a libevent loop for writing data out to the clients

Hypothetical questions (only relevant if my design makes sense):
How can I best communicate the newly accepted connections from thread 2 to thread 3? How do I know which client or socketfd to write to when thread 1 creates a message for a client in thread 3? What form of manual buffer management must I implement so that I can enqueue messages when the previous one isn't yet sent?

I am willing to use libevent specific functions and structures for creating buffers and references that need to be passed around, maybe that helps formulate an answer.

If anyone has got some tips to point me in the general direction of a solution, that would be awesome. The main problem I have is conceptual: how can I create an application where clients are notified of 'events' that happen outside of a libevent event loop. I would like to (re)use as much of libevent as possible.

Thanks in advance for your time everyone, I'll gladly elaborate if so desired.

With kind regards,

Frank Schoep

***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.