Created
November 23, 2015 11:01
-
-
Save azat/92cbb34232ac02d7972b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -ggdb -pthread -o test_timeout test_timeout.c -levent -levent_pthreads | |
#include <event2/dns.h> | |
#include <event2/event.h> | |
#include <event2/http.h> | |
#include <event2/thread.h> | |
#include <event2/bufferevent.h> | |
#include <pthread.h> | |
#include <stdio.h> | |
int done = 0; | |
struct event_base* base; | |
struct evdns_base* dns; | |
struct evhttp_connection* conn; | |
struct event* close_conn; | |
void connection_closer(evutil_socket_t sock, short flag, void* userdata) { | |
//printf("freeing connection\n"); | |
evhttp_connection_free(conn); | |
printf("connection freed\n"); | |
event_base_loopexit(base, NULL); | |
} | |
void request_done(struct evhttp_request* req, void* userdata) { | |
printf("request_done: %p\n", req); | |
if (req) { | |
printf("response code: %i\n", evhttp_request_get_response_code(req)); | |
} | |
#if 0 | |
//printf("freeing connection\n"); | |
evhttp_connection_free(conn); | |
//printf("exiting loop\n"); | |
event_base_loopexit(base, NULL); | |
#else | |
event_active(close_conn, 0, 0); | |
#endif | |
} | |
void* event_dispatch_thread(void* userdata) { | |
printf("event dispatch thread running\n"); | |
printf("event_base_dispatch: %i\n", event_base_dispatch(base)); | |
return NULL; | |
} | |
#define THREAD_DISPATCH | |
int main() { | |
#ifdef THREAD_DISPATCH | |
pthread_t thr; | |
#endif | |
struct evhttp_request* req; | |
struct bufferevent *bev = NULL; | |
evthread_use_pthreads(); | |
base = event_base_new(); | |
dns = evdns_base_new(base, 1); | |
close_conn = event_new(base, -1, 0, &connection_closer, NULL); | |
#ifdef THREAD_DISPATCH | |
printf("pthread_create: %i\n", | |
pthread_create(&thr, NULL, &event_dispatch_thread, NULL)); | |
#endif | |
//bev = bufferevent_socket_new(base, -1, BEV_OPT_THREADSAFE|BEV_OPT_DEFER_CALLBACKS); | |
bev = bufferevent_socket_new(base, -1, BEV_OPT_THREADSAFE); | |
conn = evhttp_connection_base_bufferevent_new(base, dns, bev, "www.google.com", 80); | |
#if 1 | |
evhttp_connection_set_timeout(conn, -42); | |
#endif | |
req = evhttp_request_new(&request_done, NULL); | |
printf("evhttp_make_request: %i\n", | |
evhttp_make_request(conn, req, EVHTTP_REQ_GET, "/")); | |
#ifdef THREAD_DISPATCH | |
printf("waiting for event_dispatch_thread\n"); | |
pthread_join(thr, NULL); | |
printf("event_dispatch_thread joined\n"); | |
#else | |
event_dispatch_thread(NULL); | |
#endif | |
event_free(close_conn); | |
evdns_base_free(dns, 1); | |
event_base_free(base); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment