Chaitanya
Chaitanya

Reputation: 3479

libev custom events

I am trying to use libev for event based programming. But there are some events provided by libev like EV_READ, EV_WRITE, EV_TIMER ..

So, is it possible to have an custom event of my own.

For instance, I have a continuous flow of messages from a socket an I am interested in only a type of message in that stream of messages. So, its basically like

while(true)
{
     Msg msg = getMessage();
     if(msg != null && msg.id == ourId)
         return msg;
}

So, I want to register for events of this sort (only that if condition is satisfied.) . Its a custom event right ? How do I specify this event to libev.

I mean in libev we specify like this ..

ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
ev_io_start (loop, &stdin_watcher);

I dint see any stuff where we can check for our own events. Is it possible ?

Upvotes: 3

Views: 1183

Answers (1)

user823738
user823738

Reputation: 17521

Unfortunately you cannot have a custom event, because libev doesn't manipulate with your data, just checks if there are some, or if you can send some.

So basically, you will have to:

  1. Create a watcher with EV_READ
  2. When the watcher fires your function, check if there is a message you want:
    • if yes, push it to your message queue
    • if not, just skip it
  3. Run ev_loop, ideally with EV_ONESHOT and a timeout watcher. Right after that call your message handler, which will check the message queue and process all messages thet are in the queue

Upvotes: 1

Related Questions