Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 600
Chaitanya Avatar asked Aug 16 '26 20:08

Chaitanya


1 Answers

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
like image 140
Zaffy Avatar answered Aug 19 '26 12:08

Zaffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!