compat_libevent.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_event_free event_free
  26. #define tor_evdns_add_server_port(sock, tcp, cb, data) \
  27. evdns_add_server_port_with_base(tor_libevent_get_base(), \
  28. (sock),(tcp),(cb),(data));
  29. #else
  30. struct event *tor_event_new(struct event_base * base, evutil_socket_t sock,
  31. short what, void (*cb)(evutil_socket_t, short, void *), void *arg);
  32. struct event *tor_evtimer_new(struct event_base * base,
  33. void (*cb)(evutil_socket_t, short, void *), void *arg);
  34. struct event *tor_evsignal_new(struct event_base * base, int sig,
  35. void (*cb)(evutil_socket_t, short, void *), void *arg);
  36. void tor_event_free(struct event *ev);
  37. #define tor_evdns_add_server_port evdns_add_server_port
  38. #endif
  39. typedef struct periodic_timer_t periodic_timer_t;
  40. periodic_timer_t *periodic_timer_new(struct event_base *base,
  41. const struct timeval *tv,
  42. void (*cb)(periodic_timer_t *timer, void *data),
  43. void *data);
  44. void periodic_timer_free(periodic_timer_t *);
  45. #ifdef HAVE_EVENT_BASE_LOOPEXIT
  46. #define tor_event_base_loopexit event_base_loopexit
  47. #else
  48. struct timeval;
  49. int tor_event_base_loopexit(struct event_base *base, struct timeval *tv);
  50. #endif
  51. /** Defines a configuration for using libevent with Tor: passed as an argument
  52. * to tor_libevent_initialize() to describe how we want to set up. */
  53. typedef struct tor_libevent_cfg {
  54. /** Flag: if true, disable IOCP (assuming that it could be enabled). */
  55. int disable_iocp;
  56. /** How many CPUs should we use (relevant only with IOCP). */
  57. int num_cpus;
  58. /** How many milliseconds should we allow between updating bandwidth limits?
  59. * (relevant only with bufferevents). */
  60. int msec_per_tick;
  61. } tor_libevent_cfg;
  62. void tor_libevent_initialize(tor_libevent_cfg *cfg);
  63. MOCK_DECL(struct event_base *, tor_libevent_get_base, (void));
  64. const char *tor_libevent_get_method(void);
  65. void tor_check_libevent_version(const char *m, int server,
  66. const char **badness_out);
  67. void tor_check_libevent_header_compatibility(void);
  68. const char *tor_libevent_get_version_str(void);
  69. const char *tor_libevent_get_header_version_str(void);
  70. #ifdef USE_BUFFEREVENTS
  71. const struct timeval *tor_libevent_get_one_tick_timeout(void);
  72. int tor_libevent_using_iocp_bufferevents(void);
  73. int tor_set_bufferevent_rate_limit(struct bufferevent *bev,
  74. struct ev_token_bucket_cfg *cfg);
  75. int tor_add_bufferevent_to_rate_limit_group(struct bufferevent *bev,
  76. struct bufferevent_rate_limit_group *g);
  77. #endif
  78. int tor_init_libevent_rng(void);
  79. void tor_gettimeofday_cached(struct timeval *tv);
  80. void tor_gettimeofday_cache_clear(void);
  81. #ifdef TOR_UNIT_TESTS
  82. void tor_gettimeofday_cache_set(const struct timeval *tv);
  83. #endif
  84. void tor_gettimeofday_cached_monotonic(struct timeval *tv);
  85. #endif