compat_libevent.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (c) 2009-2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #ifndef TOR_COMPAT_LIBEVENT_H
  4. #define TOR_COMPAT_LIBEVENT_H
  5. #include "orconfig.h"
  6. struct event;
  7. struct event_base;
  8. #ifdef USE_BUFFEREVENTS
  9. struct bufferevent;
  10. struct ev_token_bucket_cfg;
  11. struct bufferevent_rate_limit_group;
  12. #endif
  13. #ifdef HAVE_EVENT2_EVENT_H
  14. #include <event2/util.h>
  15. #elif !defined(EVUTIL_SOCKET_DEFINED)
  16. #define EVUTIL_SOCKET_DEFINED
  17. #define evutil_socket_t int
  18. #endif
  19. void configure_libevent_logging(void);
  20. void suppress_libevent_log_msg(const char *msg);
  21. #ifdef HAVE_EVENT2_EVENT_H
  22. #define tor_event_new event_new
  23. #define tor_evtimer_new evtimer_new
  24. #define tor_evsignal_new evsignal_new
  25. #define tor_evdns_add_server_port(sock, tcp, cb, data) \
  26. evdns_add_server_port_with_base(tor_libevent_get_base(), \
  27. (sock),(tcp),(cb),(data));
  28. #else
  29. struct event *tor_event_new(struct event_base * base, evutil_socket_t sock,
  30. short what, void (*cb)(evutil_socket_t, short, void *), void *arg);
  31. struct event *tor_evtimer_new(struct event_base * base,
  32. void (*cb)(evutil_socket_t, short, void *), void *arg);
  33. struct event *tor_evsignal_new(struct event_base * base, int sig,
  34. void (*cb)(evutil_socket_t, short, void *), void *arg);
  35. #define tor_evdns_add_server_port evdns_add_server_port
  36. #endif
  37. void tor_event_free(struct event *ev);
  38. typedef struct periodic_timer_t periodic_timer_t;
  39. periodic_timer_t *periodic_timer_new(struct event_base *base,
  40. const struct timeval *tv,
  41. void (*cb)(periodic_timer_t *timer, void *data),
  42. void *data);
  43. void periodic_timer_free(periodic_timer_t *);
  44. #ifdef HAVE_EVENT_BASE_LOOPEXIT
  45. #define tor_event_base_loopexit event_base_loopexit
  46. #else
  47. struct timeval;
  48. int tor_event_base_loopexit(struct event_base *base, struct timeval *tv);
  49. #endif
  50. /** Defines a configuration for using libevent with Tor: passed as an argument
  51. * to tor_libevent_initialize() to describe how we want to set up. */
  52. typedef struct tor_libevent_cfg {
  53. /** Flag: if true, disable IOCP (assuming that it could be enabled). */
  54. int disable_iocp;
  55. /** How many CPUs should we use (relevant only with IOCP). */
  56. int num_cpus;
  57. /** How many milliseconds should we allow between updating bandwidth limits?
  58. * (relevant only with bufferevents). */
  59. int msec_per_tick;
  60. } tor_libevent_cfg;
  61. void tor_libevent_initialize(tor_libevent_cfg *cfg);
  62. MOCK_DECL(struct event_base *, tor_libevent_get_base, (void));
  63. const char *tor_libevent_get_method(void);
  64. void tor_check_libevent_version(const char *m, int server,
  65. const char **badness_out);
  66. void tor_check_libevent_header_compatibility(void);
  67. const char *tor_libevent_get_version_str(void);
  68. const char *tor_libevent_get_header_version_str(void);
  69. #ifdef USE_BUFFEREVENTS
  70. const struct timeval *tor_libevent_get_one_tick_timeout(void);
  71. int tor_libevent_using_iocp_bufferevents(void);
  72. int tor_set_bufferevent_rate_limit(struct bufferevent *bev,
  73. struct ev_token_bucket_cfg *cfg);
  74. int tor_add_bufferevent_to_rate_limit_group(struct bufferevent *bev,
  75. struct bufferevent_rate_limit_group *g);
  76. #endif
  77. int tor_init_libevent_rng(void);
  78. void tor_gettimeofday_cached(struct timeval *tv);
  79. void tor_gettimeofday_cache_clear(void);
  80. #ifdef TOR_UNIT_TESTS
  81. void tor_gettimeofday_cache_set(const struct timeval *tv);
  82. #endif
  83. void tor_gettimeofday_cached_monotonic(struct timeval *tv);
  84. #endif