在libevent中两个回调之间的区别

时间:2014-08-03 05:08:32

标签: c libevent

在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(..)的措辞不同,但在我的测试中,在HTTP事务结束时确实调用了evhttp_request_new(..)中的回调。

1 个答案:

答案 0 :(得分:1)

我已经在 http.c (libevent 2.1.8)中检查了这些功能,我发现它们的内部用法不同:

1) evhttp_request_new ()创建evhttp_request对象并设置 req-> cb :USED BY:

  • evhttp_connection_incoming_fail() --->将 req-> cb 作为回复函数调用来自任何地方的此失败 - 错误,错误太长,第一行功能等......
  • evhttp_connection_done() --->称为( * req-> cb )(req,req-> cb_arg);在处理请求/响应之后。

它位于 evhttp_connection和错误

2) evhttp_request_set_on_complete_cb ()SETS req-> on_complete_cb 除了req-> cb 之外,还存在evhttp_request对象。仅使用:evhttp_send_done(此函数从evhttp_send()调用 - > evhttp_write_buffer():evcon-> cb = evhttp_send_done()在整个请求的页面数据发送/分块请求完成后)。

在数据写入后,它位于,并且仅在未发生某些错误的情况下