Libev, How to pass arguments to relevant callbacks

人走茶凉 提交于 2019-12-04 22:23:56
lsk

The author answered it himself, but in a comment. Since this shows up as unanswered, I am posting his answer in the "answer" section and closing the loop. Not sure if there is a better way... feel free to fix this.

Question author says:

Sorry, I think I got the answer now, and feel deeply ashamed at my carelessness of reading documentation:

struct my_io{
   ev_io io;
   int otherfd;
   void *somedata;
   struct whatever *mostinteresting;
};
// ...
struct my_io w;
ev_io_init (&w.io, my_cb, fd, EV_READ);

And then we use the my_io like this:

static void my_cb (struct ev_loop *loop, ev_io *w_, int revents)
{
   struct my_io *w = (struct my_io *)w_;
   //...
}

Yes it's explained in libev document, and there is another way. Each watcher has a void *data member that you can read or modify, and libev will completely ignore it, so you can pass the argument like this:

w->data = (void *)recv_buff;
...
static void my_cb (struct ev_loop *loop, ev_io *w_, int revents)
{
    S_RECV_MSG *recv_buff = (S_RECV_MSG*)w_->data;
    ...
}

see libev document.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!