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

[Libevent-users] Callback user_data



Hi All,

I'm using libevent 2.1.3-alpha-dev and was wondering how I could pass an event to a callback function as part of another structure. In code I would like the following:

struct my_struct {
  struct event* evt;
  struct foo* f;
  int x
};

void event_callback(evutil_socket_t sock, short what, void *user_data) {
  struct my_struct* data = "" my_struct*)user_data;
  another_function(what, data->evt);
}

Trouble is when I create and add the event the only option to pass the event itself is using event_self_cbarg() as follows:

struct event* new_event;
new_event = event_new(base, fd, EV_WRITE, event_callback, event_self_cbarg());

My question is would it be safe to create my_struct and pass it to event_new and then fill in the evt member of my_struct later on by using my_struct->evt = event_self_cbarg()?

The closest thing I could find for an answer was this:
http://archives.seul.org/libevent/users/Sep-2012/msg00041.html
but that's for a pre 2.1 release and not sure it's valid but it seems to hint that passing pointer to pointer would work. I tried both methods (assigning event_self_cbarg() to my_struct->evt and passing changing my_struct so that evt member is defined as struct event** evt and assigning my_struct->evt = &new_event.


Any direction/thoughts appreciated.

Best,
Timir
--

Timir Karia
tkaria@xxxxxxxxx