简体   繁体   English

如何在Libevent中修改注册事件?

[英]How to modify the registered event in Libevent?

I use libevent like this, 我这样使用libevent,

client->m_event = event_new(listener->m_server->m_server_base, client->m_sockfd, EV_PERSIST, Client::ClientEventCallback, client);
event_add(client->m_event, NULL);

But I don't know how to modify the event of m_event, there seems to be no interface to operate in official manual, i tried to do it like this, but it make a core dump. 但是我不知道如何修改m_event的事件,在官方手册中似乎没有可操作的接口,我试图这样做,但它会导致核心转储。

short event = event_get_events(m_event);
event_del(m_event);
event_assign(m_event, m_server->m_server_base, m_sockfd, event | EV_WRITE, Client::ClientEventCallback, this);
event_add(m_event, NULL); 

core dump ocurs at event_assign, please help me ... how to modify the registed event of the struct event ? 核心转储发生在event_assign中,请帮助我...如何修改struct事件的注册事件?

I don't see anything in your code that should make you core dump, but I might inquire as to why you're event_assign'ing again anyways just to add in the EV_WRITE flag. 我在您的代码中看不到任何应该使您成为核心转储的内容,但我可能会询问为什么您仍然要再次event_assign'ing,只是添加EV_WRITE标志。 Correct me if i'm wrong, but since your event_new is only specifying EV_PERSIST , I don't think it'll ever fire (so why event_add it at that point?) It seems like you should just be doing 如果我错了,请纠正我,但是由于您的event_new仅指定EV_PERSIST ,所以我认为它永远不会触发(所以为什么在这一点上将event_add添加?)似乎您应该这样做

client->m_event = event_new(listener->m_server->m_server_base, client->m_sockfd, EV_PERSIST | EV_WRITE, Client::ClientEventCallback, client);

up top, then when you are ready to write just do 首先,那么当您准备写作时

event_add(m_event, NULL);

and when you're done writing 当你写完

event_del(m_event);

Anyways, like I said I wouldn't think what you're doing should be a problem per-say, but if there is some funky behavior that happens when you add an event with only EV_PERSIST as the event type and then later del/assign it, the above may solve it. 无论如何,就像我说的那样,我不会认为您正在做的事情会说一个问题,但是当您添加一个仅将EV_PERSIST作为事件类型的事件并随后进行del / assign时,如果发生一些时髦的行为它,以上可能解决了。 (And it'll be less code / more effecient anyways, so might as well :)) (而且代码更少/效率更高,所以也可能:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM