在libevent中,HTTP事务完成后,以下两个API是否都会调用回调函数?
//from event2/http.h
/**
* Creates a new request object that needs to be filled in with the request
* parameters. The callback is executed when the request completed or an
* error occurred.
*/
struct evhttp_request *evhttp_request_new(
void (*cb)(struct evhttp_request *, void *), void *arg);
/*The callback function will be called on the completion of the request after
* the output data has been written and before the evhttp_request object
* is destroyed ....*/
void evhttp_request_set_on_complete_cb(struct evhttp_request *req,
void (*cb)(struct evhttp_request *, void *), void *cb_arg);
evhttp_request_new(..)的写法与evhttp_request_set_on_complete_cb(..)的写法不同,但在我的测试中,evhttp_request_new(..)中的回调确实是在HTTP事务结束时调用的。
最佳答案
我检查了 http.c (libevent 2.1.8) 中的那些函数,我发现它们的内部用法不同:
1) evhttp_request_new() 创建 evhttp_request 对象并设置 req->cb 被使用:
- evhttp_connection_incoming_fail() ---> 调用 req->cb 作为关于来自任何地方的失败的回复函数 - 损坏的错误,太长,一线函数,等等……
- evhttp_connection_done() ---> 调用为 (*req->cb)(req, req->cb_arg);在处理请求/响应之后。
由evhttp_connection和error定位
2) evhttp_request_set_on_complete_cb() 设置req->on_complete_cb FOR EXISTING evhttp_request object in addition to req->cb。 USED ONLY BY: evhttp_send_done(此函数从 evhttp_send()->evhttp_write_buffer() 调用:evcon->cb=evhttp_send_done() 在发送整个请求的页面数据/分块请求完成后)。
它位于数据写入之后,并且只有在没有发生错误的情况下。
关于c - libevent中两个回调之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25101826/