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:
;
}