Skip to content

Commit ffce893

Browse files
committed
Merge #9517: [refactor] Switched httpserver.cpp to use RAII wrapped libevents.
1ae86ec Changed event RAII helper functions to inline to deal with duplicate symbol linker errors. (Karl-Johan Alm) fd369d2 Switched httpserver.cpp to use RAII wrapped libevents. (Kalle Alm) Tree-SHA512: 877e431f211024d42a3b0800e860e02833398611433e8393f8d5d4970f47f4bd670b900443678c067fec110c087aaab7dc1981ccbf17f6057676fdbbda89aed9
2 parents 209eef6 + 1ae86ec commit ffce893

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

src/httpserver.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
#include <signal.h>
2222
#include <future>
2323

24-
#include <event2/event.h>
25-
#include <event2/http.h>
2624
#include <event2/thread.h>
2725
#include <event2/buffer.h>
2826
#include <event2/util.h>
2927
#include <event2/keyvalq_struct.h>
3028

29+
#include "support/events.h"
30+
3131
#ifdef EVENT__HAVE_NETINET_IN_H
3232
#include <netinet/in.h>
3333
#ifdef _XOPEN_SOURCE_EXTENDED
@@ -367,9 +367,6 @@ static void libevent_log_cb(int severity, const char *msg)
367367

368368
bool InitHTTPServer()
369369
{
370-
struct evhttp* http = 0;
371-
struct event_base* base = 0;
372-
373370
if (!InitHTTPAllowList())
374371
return false;
375372

@@ -395,17 +392,13 @@ bool InitHTTPServer()
395392
evthread_use_pthreads();
396393
#endif
397394

398-
base = event_base_new(); // XXX RAII
399-
if (!base) {
400-
LogPrintf("Couldn't create an event_base: exiting\n");
401-
return false;
402-
}
395+
raii_event_base base_ctr = obtain_event_base();
403396

404397
/* Create a new evhttp object to handle requests. */
405-
http = evhttp_new(base); // XXX RAII
398+
raii_evhttp http_ctr = obtain_evhttp(base_ctr.get());
399+
struct evhttp* http = http_ctr.get();
406400
if (!http) {
407401
LogPrintf("couldn't create evhttp. Exiting.\n");
408-
event_base_free(base);
409402
return false;
410403
}
411404

@@ -416,8 +409,6 @@ bool InitHTTPServer()
416409

417410
if (!HTTPBindAddresses(http)) {
418411
LogPrintf("Unable to bind any endpoint for RPC server\n");
419-
evhttp_free(http);
420-
event_base_free(base);
421412
return false;
422413
}
423414

@@ -426,8 +417,9 @@ bool InitHTTPServer()
426417
LogPrintf("HTTP: creating work queue of depth %d\n", workQueueDepth);
427418

428419
workQueue = new WorkQueue<HTTPClosure>(workQueueDepth);
429-
eventBase = base;
430-
eventHTTP = http;
420+
// tranfer ownership to eventBase/HTTP via .release()
421+
eventBase = base_ctr.release();
422+
eventHTTP = http_ctr.release();
431423
return true;
432424
}
433425

src/support/events.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ MAKE_RAII(evhttp);
2727
MAKE_RAII(evhttp_request);
2828
MAKE_RAII(evhttp_connection);
2929

30-
raii_event_base obtain_event_base() {
30+
inline raii_event_base obtain_event_base() {
3131
auto result = raii_event_base(event_base_new());
3232
if (!result.get())
3333
throw std::runtime_error("cannot create event_base");
3434
return result;
3535
}
3636

37-
raii_event obtain_event(struct event_base* base, evutil_socket_t s, short events, event_callback_fn cb, void* arg) {
37+
inline raii_event obtain_event(struct event_base* base, evutil_socket_t s, short events, event_callback_fn cb, void* arg) {
3838
return raii_event(event_new(base, s, events, cb, arg));
3939
}
4040

41-
raii_evhttp obtain_evhttp(struct event_base* base) {
41+
inline raii_evhttp obtain_evhttp(struct event_base* base) {
4242
return raii_evhttp(evhttp_new(base));
4343
}
4444

45-
raii_evhttp_request obtain_evhttp_request(void(*cb)(struct evhttp_request *, void *), void *arg) {
45+
inline raii_evhttp_request obtain_evhttp_request(void(*cb)(struct evhttp_request *, void *), void *arg) {
4646
return raii_evhttp_request(evhttp_request_new(cb, arg));
4747
}
4848

49-
raii_evhttp_connection obtain_evhttp_connection_base(struct event_base* base, std::string host, uint16_t port) {
49+
inline raii_evhttp_connection obtain_evhttp_connection_base(struct event_base* base, std::string host, uint16_t port) {
5050
auto result = raii_evhttp_connection(evhttp_connection_base_new(base, NULL, host.c_str(), port));
5151
if (!result.get())
5252
throw std::runtime_error("create connection failed");

0 commit comments

Comments
 (0)