[Libevent-users] [PATCH] set base for evhttp

Paul Querna chip at corelands.com
Mon Apr 23 16:26:42 EDT 2007


Attached is a patch against the 1.3b sources, to set the event_base* for
both the evhttp server and client connections.

It adds the following functions to the API:
evhttp_base_start
evhttp_base_connection_new

Only lightly tested, but it passes the regress tests.

Thoughts?

Thanks,

-Paul

-------------- next part --------------
Index: http.c
===================================================================
--- http.c	(revision 13656)
+++ http.c	(working copy)
@@ -150,6 +150,15 @@
 void evhttp_read(int, short, void *);
 void evhttp_write(int, short, void *);
 
+static int evhttp_null_base_set(struct event_base *base, struct event *ev)
+{
+	if (base != NULL) {
+		return event_base_set(base, ev);
+	}
+	
+	return (0);
+}
+
 #ifndef HAVE_STRSEP
 static char *
 strsep(char **s, const char *del)
@@ -277,6 +286,7 @@
 		event_del(&evcon->ev);
 
 	event_set(&evcon->ev, evcon->fd, EV_WRITE, evhttp_write, evcon);
+	evhttp_null_base_set(evcon->base_ev, &evcon->ev);
 	evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_WRITE_TIMEOUT);
 }
 
@@ -706,6 +716,7 @@
 	}
 	/* Read more! */
 	event_set(&evcon->ev, evcon->fd, EV_READ, evhttp_read, evcon);
+	evhttp_null_base_set(evcon->base_ev, &evcon->ev);
 	evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_READ_TIMEOUT);
 }
 
@@ -862,6 +873,7 @@
 		event_del(&evcon->close_ev);
 	event_set(&evcon->close_ev, evcon->fd, EV_READ,
 	    evhttp_detect_close_cb, evcon);
+	evhttp_null_base_set(evcon->base_ev, &evcon->ev);
 	event_add(&evcon->close_ev, NULL);
 }
 
@@ -1349,6 +1361,12 @@
 struct evhttp_connection *
 evhttp_connection_new(const char *address, unsigned short port)
 {
+	return evhttp_base_connection_new(NULL, address, port);
+}
+
+struct evhttp_connection *
+evhttp_base_connection_new(struct event_base *evb, const char *address, unsigned short port)
+{
 	struct evhttp_connection *evcon = NULL;
 	
 	event_debug(("Attempting connection to %s:%d\n", address, port));
@@ -1360,6 +1378,8 @@
 
 	evcon->fd = -1;
 	evcon->port = port;
+    
+	evcon->base_ev = evb;
 
 	evcon->timeout = -1;
 	evcon->retry_cnt = evcon->retry_max = 0;
@@ -1382,6 +1402,8 @@
 	evcon->state = EVCON_DISCONNECTED;
 	TAILQ_INIT(&evcon->requests);
 
+	evhttp_null_base_set(evcon->base_ev, &evcon->ev);
+	
 	return (evcon);
 	
  error:
@@ -1441,6 +1463,7 @@
 
 	/* Set up a callback for successful connection setup */
 	event_set(&evcon->ev, evcon->fd, EV_WRITE, evhttp_connectioncb, evcon);
+	evhttp_null_base_set(evcon->base_ev, &evcon->ev);
 	evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_CONNECT_TIMEOUT);
 
 	evcon->state = EVCON_CONNECTING;
@@ -1506,7 +1529,7 @@
 	if (event_initialized(&evcon->ev))
 		event_del(&evcon->ev);
 	event_set(&evcon->ev, evcon->fd, EV_READ, evhttp_read_header, evcon);
-	
+	evhttp_null_base_set(evcon->base_ev, &evcon->ev);
 	evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_READ_TIMEOUT);
 }
 
@@ -1883,6 +1906,7 @@
 
 	/* Schedule the socket for accepting */
 	event_set(ev, fd, EV_READ | EV_PERSIST, accept_socket, http);
+	evhttp_null_base_set(http->base_ev, ev);
 	event_add(ev, NULL);
 
 	event_debug(("Bound to port %d - Awaiting connections ... ", port));
@@ -1897,6 +1921,12 @@
 struct evhttp *
 evhttp_start(const char *address, u_short port)
 {
+	return evhttp_base_start(NULL, address, port);
+}
+
+struct evhttp *
+evhttp_base_start(struct event_base *evb, const char *address, u_short port)
+{
 	struct evhttp *http;
 
 	if ((http = calloc(1, sizeof(struct evhttp))) == NULL) {
@@ -1904,6 +1934,8 @@
 		return (NULL);
 	}
 
+	http->base_ev =  evb;
+	
 	http->timeout = -1;
 
 	TAILQ_INIT(&http->callbacks);
@@ -2073,7 +2105,7 @@
 
 static struct evhttp_connection*
 evhttp_get_request_connection(
-	int fd, struct sockaddr *sa, socklen_t salen)
+	struct evhttp *http, int fd, struct sockaddr *sa, socklen_t salen)
 {
 	struct evhttp_connection *evcon;
 	char *hostname, *portname;
@@ -2083,7 +2115,7 @@
 			__func__, hostname, portname, fd));
 
 	/* we need a connection object to put the http request on */
-	if ((evcon = evhttp_connection_new(hostname, atoi(portname))) == NULL)
+	if ((evcon = evhttp_base_connection_new(http->base_ev, hostname, atoi(portname))) == NULL)
 		return (NULL);
 	evcon->flags |= EVHTTP_CON_INCOMING;
 	evcon->state = EVCON_CONNECTED;
@@ -2123,7 +2155,7 @@
 {
 	struct evhttp_connection *evcon;
 
-	evcon = evhttp_get_request_connection(fd, sa, salen);
+	evcon = evhttp_get_request_connection(http, fd, sa, salen);
 	if (evcon == NULL)
 		return;
 
Index: evhttp.h
===================================================================
--- evhttp.h	(revision 13656)
+++ evhttp.h	(working copy)
@@ -64,6 +64,7 @@
 
 /* Start an HTTP server on the specified address and port */
 struct evhttp *evhttp_start(const char *address, u_short port);
+struct evhttp *evhttp_base_start(struct event_base *evb, const char *address, u_short port);
 
 /*
  * Free the previously create HTTP server.  Works only if no requests are
@@ -168,6 +169,8 @@
  */
 struct evhttp_connection *evhttp_connection_new(
 	const char *address, unsigned short port);
+struct evhttp_connection *evhttp_base_connection_new(
+	struct event_base *evb, const char *address, unsigned short port);
 
 /* Frees an http connection */
 void evhttp_connection_free(struct evhttp_connection *evcon);
Index: http-internal.h
===================================================================
--- http-internal.h	(revision 13656)
+++ http-internal.h	(working copy)
@@ -44,6 +44,7 @@
 	struct event close_ev;
 	struct evbuffer *input_buffer;
 	struct evbuffer *output_buffer;
+	struct event_base *base_ev;
 	
 	char *address;
 	u_short port;
@@ -81,6 +82,7 @@
 };
 
 struct evhttp {
+	struct event_base *base_ev;
 	struct event bind_ev;
 
 	TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;


More information about the Libevent-users mailing list