libevent http

http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html


event_base_new会根据config.h中的配置,选择效率最高的方式初始化,如linux,同时支持epoll、select等,则选择epool,不选择select。

每种动作有对应的回调函数,处理方式不同。


缺省情况下,会使用current_base这个缺省的event_base。

  • 使用evtimer_set、evtime_add,如果没有使用event_base_set函数设置新的event_base,则使用current_base;
  • 同理,如果evhttp_connection没有设置evhttp_connection_set_base,则也使用current_base;
  • 使用的函数有event_loop,默认直接使用current_base;如果想使用自定义的,则需要调用event_base_loop。
  • 默认的event_loop非线程安全,多个线程不同使用多次,会出问题,所以才自己创建一个event_base。
  • 同一个线程内,读数据的IO和timer用一次event_base_loop即可。



event_base_loop函数:在对应的evsel->dispatch动作里面,只是active event,把event挂接到active队列,然后再执行event_process_active。










能做各种事情,如http。



static void
http_post_test(void *arg)
{
struct basic_test_data *data = arg;
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;


test_ok = 0;


http = http_setup(&port, data->base);


evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);


/*
* At this point, we want to schedule an HTTP POST request
* server using our make request method.
*/


req = evhttp_request_new(http_postrequest_done, data->base);
tt_assert(req);


/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
evbuffer_add_printf(evhttp_request_get_output_buffer(req), POST_DATA);


if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/postit") == -1) {
tt_abort_msg("Couldn't make request");
}


event_base_dispatch(data->base);


tt_int_op(test_ok, ==, 1);


test_ok = 0;


req = evhttp_request_new(http_postrequest_done, data->base);
tt_assert(req);


/* Now try with 100-continue. */


/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
evhttp_add_header(evhttp_request_get_output_headers(req), "Expect", "100-continue");  ------------- 加http头部
evbuffer_add_printf(evhttp_request_get_output_buffer(req), POST_DATA);   -------------- 加http内容


if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/postit") == -1) {--------------- 添加http请求,检查连接,并尝试发送
tt_abort_msg("Couldn't make request");
}


event_base_dispatch(data->base);


tt_int_op(test_ok, ==, 1);


evhttp_connection_free(evcon);
evhttp_free(http);


 end:
;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值