The attached patch allows an evhttp server to properly operate on an 
alternative event_base that is set via a new interface evhttp_base_start(), 
which is also added by this patch. Basically this makes the http.c 
implementation apply the event_base present in the struct evhttp instance 
associated with the evhttp_connection/evhttp_request whenever an event is 
scheduled via event_set/event_add.

A separate patch will need to handle the evhttp clients.


--

paul 
diff -u libevent-1.3e.002/evhttp.h libevent-1.3e.003/evhttp.h
--- libevent-1.3e.002/evhttp.h	2007-10-31 11:49:23.000000000 -0500
+++ libevent-1.3e.003/evhttp.h	2007-10-31 11:50:14.000000000 -0500
@@ -66,6 +66,8 @@
 
 /* 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 *base, const char *address,
+                                 u_short port);
 
 /*
  * Free the previously create HTTP server.  Works only if no requests are
diff -u libevent-1.3e.002/http.c libevent-1.3e.003/http.c
--- libevent-1.3e.002/http.c	2007-09-12 17:20:34.000000000 -0500
+++ libevent-1.3e.003/http.c	2007-10-31 11:55:43.000000000 -0500
@@ -247,6 +247,14 @@
 }
 
 static void
+evhttp_base_set(struct evhttp_connection *evcon, struct event *ev)
+{
+    if( evcon->http_server != 0
+        && evcon->http_server->bind_ev.ev_base != 0 )
+        event_base_set(evcon->http_server->bind_ev.ev_base, ev);
+}
+
+static void
 evhttp_add_event(struct event *ev, int timeout, int default_timeout)
 {
 	if (timeout != 0) {
@@ -275,6 +283,7 @@
 		event_del(&evcon->ev);
 
 	event_set(&evcon->ev, evcon->fd, EV_WRITE, evhttp_write, evcon);
+        evhttp_base_set(evcon, &evcon->ev);
 	evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_WRITE_TIMEOUT);
 }
 
@@ -730,6 +739,7 @@
 	}
 	/* Read more! */
 	event_set(&evcon->ev, evcon->fd, EV_READ, evhttp_read, evcon);
+        evhttp_base_set(evcon, &evcon->ev);
 	evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_READ_TIMEOUT);
 }
 
@@ -886,6 +896,7 @@
 		event_del(&evcon->close_ev);
 	event_set(&evcon->close_ev, evcon->fd, EV_READ,
 	    evhttp_detect_close_cb, evcon);
+        event_base_set(evcon->http_server->bind_ev.ev_base, &evcon->close_ev);
 	event_add(&evcon->close_ev, NULL);
 }
 
@@ -952,6 +963,7 @@
  cleanup:
 	if (evcon->retry_max < 0 || evcon->retry_cnt < evcon->retry_max) {
 		evtimer_set(&evcon->ev, evhttp_connection_retry, evcon);
+                evhttp_base_set(evcon, &evcon->ev);
 		evhttp_add_event(&evcon->ev, MIN(3600, 2 << evcon->retry_cnt),
 		    HTTP_CONNECT_TIMEOUT);
 		evcon->retry_cnt++;
@@ -1474,6 +1486,7 @@
 
 	/* Set up a callback for successful connection setup */
 	event_set(&evcon->ev, evcon->fd, EV_WRITE, evhttp_connectioncb, evcon);
+        evhttp_base_set(evcon, &evcon->ev);
 	evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_CONNECT_TIMEOUT);
 
 	evcon->state = EVCON_CONNECTING;
@@ -1539,7 +1552,7 @@
 	if (event_initialized(&evcon->ev))
 		event_del(&evcon->ev);
 	event_set(&evcon->ev, evcon->fd, EV_READ, evhttp_read_header, evcon);
-	
+        evhttp_base_set(evcon, &evcon->ev);
 	evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_READ_TIMEOUT);
 }
 
@@ -1918,7 +1931,8 @@
 }
 
 static int
-bind_socket(struct evhttp *http, const char *address, u_short port)
+bind_socket(struct event_base *base, struct evhttp *http,
+            const char *address, u_short port)
 {
 	struct event *ev = &http->bind_ev;
 	int fd;
@@ -1933,6 +1947,7 @@
 
 	/* Schedule the socket for accepting */
 	event_set(ev, fd, EV_READ | EV_PERSIST, accept_socket, http);
+        event_base_set(base, ev);
 	event_add(ev, NULL);
 
 	event_debug(("Bound to port %d - Awaiting connections ... ", port));
@@ -1945,7 +1960,7 @@
  */
 
 struct evhttp *
-evhttp_start(const char *address, u_short port)
+evhttp_base_start(struct event_base *base, const char *address, u_short port)
 {
 	struct evhttp *http;
 
@@ -1959,7 +1974,7 @@
 	TAILQ_INIT(&http->callbacks);
 	TAILQ_INIT(&http->connections);
 
-	if (bind_socket(http, address, port) == -1) {
+	if (bind_socket(base, http, address, port) == -1) {
 		free(http);
 		return (NULL);
 	}
@@ -1967,6 +1982,12 @@
 	return (http);
 }
 
+struct evhttp *
+evhttp_start(const char *address, u_short port)
+{
+        return evhttp_base_start(NULL, address, port);
+}
+
 void
 evhttp_free(struct evhttp* http)
 {
_______________________________________________
Libevent-users mailing list
Libevent-users@monkey.org
http://monkey.org/mailman/listinfo/libevent-users

Reply via email to