c - libevent中两个回调之间的区别

标签 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(..)的写法不同,但在我的测试中,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/

相关文章:

c++ - OpenSSL SSL_shutdown 收到信号 SIGPIPE,Broken pipe

c - 斯特林近似产生与预期不同的输出

c - 如何创建动态大小的 NULL 指针数组?

c - 在这种情况下 "pinned"是什么意思?

c - 文件删除/修改期间的 libevent

c++ - 如果我想要事件驱动的服务器,使用 libevent 还是 libev?

c - strcpy() 跳过数组元素

c - 具有整数参数的函数

c - TFTP 服务器 - 线程版本问题

c++ - libevent:如何覆盖 header 包括