Raven Core  3.0.0
P2P Digital Currency
events.h
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_SUPPORT_EVENTS_H
7 #define RAVEN_SUPPORT_EVENTS_H
8 
9 #include <ios>
10 #include <memory>
11 
12 #include <event2/event.h>
13 #include <event2/http.h>
14 
15 #define MAKE_RAII(type) \
16 /* deleter */\
17 struct type##_deleter {\
18  void operator()(struct type* ob) {\
19  type##_free(ob);\
20  }\
21 };\
22 /* unique ptr typedef */\
23 typedef std::unique_ptr<struct type, type##_deleter> raii_##type
24 
25 MAKE_RAII(event_base);
26 MAKE_RAII(event);
27 MAKE_RAII(evhttp);
28 MAKE_RAII(evhttp_request);
29 MAKE_RAII(evhttp_connection);
30 
31 inline raii_event_base obtain_event_base() {
32  auto result = raii_event_base(event_base_new());
33  if (!result.get())
34  throw std::runtime_error("cannot create event_base");
35  return result;
36 }
37 
38 inline raii_event obtain_event(struct event_base* base, evutil_socket_t s, short events, event_callback_fn cb, void* arg) {
39  return raii_event(event_new(base, s, events, cb, arg));
40 }
41 
42 inline raii_evhttp obtain_evhttp(struct event_base* base) {
43  return raii_evhttp(evhttp_new(base));
44 }
45 
46 inline raii_evhttp_request obtain_evhttp_request(void(*cb)(struct evhttp_request *, void *), void *arg) {
47  return raii_evhttp_request(evhttp_request_new(cb, arg));
48 }
49 
50 inline raii_evhttp_connection obtain_evhttp_connection_base(struct event_base* base, std::string host, uint16_t port) {
51  auto result = raii_evhttp_connection(evhttp_connection_base_new(base, nullptr, host.c_str(), port));
52  if (!result.get())
53  throw std::runtime_error("create connection failed");
54  return result;
55 }
56 
57 #endif // RAVEN_SUPPORT_EVENTS_H
raii_event obtain_event(struct event_base *base, evutil_socket_t s, short events, event_callback_fn cb, void *arg)
Definition: events.h:38
raii_event_base obtain_event_base()
Definition: events.h:31
raii_evhttp obtain_evhttp(struct event_base *base)
Definition: events.h:42
raii_evhttp_request obtain_evhttp_request(void(*cb)(struct evhttp_request *, void *), void *arg)
Definition: events.h:46
#define MAKE_RAII(type)
Definition: events.h:15
raii_evhttp_connection obtain_evhttp_connection_base(struct event_base *base, std::string host, uint16_t port)
Definition: events.h:50