我正在使用libevent在特定时间间隔中获取网站的一些统计数据。我已经基于这个程序了。我唯一缺少的是在请求上的超时,最好是以秒准确性。
我已经尝试了几件事,但无法正常工作。我真的很感谢任何建议。
这是一个非常粗略的示例:我使用libevent计时器来实现超时。通过将计时器放入结构并将其取消在回调中,可以进一步改善这一点。
#include <stdio.h>
#include <event.h>
#include <evhttp.h>
#include <stdlib.h>
#include <unistd.h>
struct http_client {
struct evhttp_connection *conn;
struct evhttp_request *req;
bool finished;
};
void _reqhandler(struct evhttp_request *req, void *state) {
struct http_client *hc = (http_client*)state;
hc->finished = true;
if (req == NULL) {
printf("timed out!n");
} else if (req->response_code == 0) {
printf("connection refused!n");
} else if (req->response_code != 200) {
printf("error: %u %sn", req->response_code, req->response_code_line);
} else {
printf("success: %u %sn", req->response_code, req->response_code_line);
}
event_loopexit(NULL);
}
void timeout_cb(int fd, short event, void *arg) {
struct http_client *hc = (http_client*)arg;
printf("Timed outn");
if (hc->finished == false){ // Can't cancel request if the callback has already executed
evhttp_cancel_request(hc->req);
}
}
int main(int argc, char *argv[]) {
struct http_client *hc = (struct http_client *)malloc(sizeof(struct http_client));
hc->finished = false;
struct event ev;
struct timeval tv;
tv.tv_sec = 3; // Timeout is set to 3.005 seconds
tv.tv_usec = 5000;
const char *addr = "173.194.39.64"; //google.com
unsigned int port = 80;
event_init();
hc->conn = evhttp_connection_new(addr, port);
evhttp_connection_set_timeout(hc->conn, 5);
hc->req = evhttp_request_new(_reqhandler, (void*)hc);
evhttp_add_header(hc->req->output_headers, "Host", addr);
evhttp_add_header(hc->req->output_headers, "Content-Length", "0");
evhttp_make_request(hc->conn, hc->req, EVHTTP_REQ_GET, "/");
evtimer_set(&ev, timeout_cb, (void*)hc); // Set a timer to cancel the request after certain time
evtimer_add(&ev, &tv);
printf("starting event loop..n");
printf("n");
event_dispatch();
return 0;
}