You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rdns_ev.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2014, Vsevolod Stakhov
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef RDNS_EV_H_
  26. #define RDNS_EV_H_
  27. #include "contrib/libev/ev.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "rdns.h"
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. static void* rdns_libev_add_read (void *priv_data, int fd, void *user_data);
  35. static void rdns_libev_del_read(void *priv_data, void *ev_data);
  36. static void* rdns_libev_add_write (void *priv_data, int fd, void *user_data);
  37. static void rdns_libev_del_write (void *priv_data, void *ev_data);
  38. static void* rdns_libev_add_timer (void *priv_data, double after, void *user_data);
  39. static void* rdns_libev_add_periodic (void *priv_data, double after,
  40. rdns_periodic_callback cb, void *user_data);
  41. static void rdns_libev_del_periodic (void *priv_data, void *ev_data);
  42. static void rdns_libev_repeat_timer (void *priv_data, void *ev_data);
  43. static void rdns_libev_del_timer (void *priv_data, void *ev_data);
  44. struct rdns_ev_periodic_cbdata {
  45. ev_timer *ev;
  46. rdns_periodic_callback cb;
  47. void *cbdata;
  48. };
  49. static void
  50. rdns_bind_libev (struct rdns_resolver *resolver, struct ev_loop *loop)
  51. {
  52. static struct rdns_async_context ev_ctx = {
  53. .data = NULL,
  54. .add_read = rdns_libev_add_read,
  55. .del_read = rdns_libev_del_read,
  56. .add_write = rdns_libev_add_write,
  57. .del_write = rdns_libev_del_write,
  58. .add_timer = rdns_libev_add_timer,
  59. .repeat_timer = rdns_libev_repeat_timer,
  60. .del_timer = rdns_libev_del_timer,
  61. .add_periodic = rdns_libev_add_periodic,
  62. .del_periodic = rdns_libev_del_periodic,
  63. .cleanup = NULL
  64. }, *nctx;
  65. void *ptr;
  66. /* XXX: never got freed */
  67. ptr = malloc (sizeof (struct rdns_async_context));
  68. if (ptr != NULL) {
  69. nctx = (struct rdns_async_context *)ptr;
  70. memcpy (ptr, (void *)&ev_ctx, sizeof (struct rdns_async_context));
  71. nctx->data = (void*)loop;
  72. rdns_resolver_async_bind (resolver, nctx);
  73. }
  74. }
  75. static void
  76. rdns_libev_read_event (struct ev_loop *loop, ev_io *ev, int revents)
  77. {
  78. rdns_process_read (ev->fd, ev->data);
  79. }
  80. static void
  81. rdns_libev_write_event (struct ev_loop *loop, ev_io *ev, int revents)
  82. {
  83. rdns_process_write(ev->fd, ev->data);
  84. }
  85. static void
  86. rdns_libev_timer_event (struct ev_loop *loop, ev_timer *ev, int revents)
  87. {
  88. rdns_process_timer (ev->data);
  89. }
  90. static void
  91. rdns_libev_periodic_event (struct ev_loop *loop, ev_timer *ev, int revents)
  92. {
  93. struct rdns_ev_periodic_cbdata *cbdata = (struct rdns_ev_periodic_cbdata *)
  94. ev->data;
  95. cbdata->cb (cbdata->cbdata);
  96. }
  97. static void*
  98. rdns_libev_add_read (void *priv_data, int fd, void *user_data)
  99. {
  100. ev_io *ev;
  101. void *ptr;
  102. ptr = malloc (sizeof (ev_io));
  103. if (ptr != NULL) {
  104. ev = (ev_io *)ptr;
  105. ev_io_init (ev, rdns_libev_read_event, fd, EV_READ);
  106. ev->data = user_data;
  107. ev_io_start ((struct ev_loop *)priv_data, ev);
  108. }
  109. return ptr;
  110. }
  111. static void
  112. rdns_libev_del_read (void *priv_data, void *ev_data)
  113. {
  114. ev_io *ev = (ev_io*)ev_data;
  115. if (ev != NULL) {
  116. ev_io_stop ((struct ev_loop *)priv_data, ev);
  117. free ((void *)ev);
  118. }
  119. }
  120. static void*
  121. rdns_libev_add_write (void *priv_data, int fd, void *user_data)
  122. {
  123. ev_io *ev;
  124. ev = (ev_io *)malloc (sizeof (ev_io));
  125. if (ev != NULL) {
  126. ev_io_init (ev, rdns_libev_write_event, fd, EV_WRITE);
  127. ev->data = user_data;
  128. ev_io_start ((struct ev_loop *)priv_data, ev);
  129. }
  130. return (void *)ev;
  131. }
  132. static void
  133. rdns_libev_del_write (void *priv_data, void *ev_data)
  134. {
  135. ev_io *ev = (ev_io *)ev_data;
  136. if (ev != NULL) {
  137. ev_io_stop ((struct ev_loop *)priv_data, ev);
  138. free ((void *)ev);
  139. }
  140. }
  141. static void*
  142. rdns_libev_add_timer (void *priv_data, double after, void *user_data)
  143. {
  144. ev_timer *ev;
  145. ev = (ev_timer *)malloc (sizeof (ev_timer));
  146. if (ev != NULL) {
  147. ev_timer_init (ev, rdns_libev_timer_event, after, after);
  148. ev->data = user_data;
  149. ev_now_update_if_cheap ((struct ev_loop *)priv_data);
  150. ev_timer_start ((struct ev_loop *)priv_data, ev);
  151. }
  152. return (void *)ev;
  153. }
  154. static void*
  155. rdns_libev_add_periodic (void *priv_data, double after,
  156. rdns_periodic_callback cb, void *user_data)
  157. {
  158. ev_timer *ev;
  159. struct rdns_ev_periodic_cbdata *cbdata = NULL;
  160. ev = (ev_timer *)malloc (sizeof (ev_timer));
  161. if (ev != NULL) {
  162. cbdata = (struct rdns_ev_periodic_cbdata *)
  163. malloc (sizeof (struct rdns_ev_periodic_cbdata));
  164. if (cbdata != NULL) {
  165. cbdata->cb = cb;
  166. cbdata->cbdata = user_data;
  167. cbdata->ev = ev;
  168. ev_timer_init (ev, rdns_libev_periodic_event, after, after);
  169. ev->data = cbdata;
  170. ev_now_update_if_cheap ((struct ev_loop *)priv_data);
  171. ev_timer_start ((struct ev_loop *)priv_data, ev);
  172. }
  173. else {
  174. free ((void *)ev);
  175. return NULL;
  176. }
  177. }
  178. return (void *)cbdata;
  179. }
  180. static void
  181. rdns_libev_del_periodic (void *priv_data, void *ev_data)
  182. {
  183. struct rdns_ev_periodic_cbdata *cbdata = (struct rdns_ev_periodic_cbdata *)
  184. ev_data;
  185. if (cbdata != NULL) {
  186. ev_timer_stop ((struct ev_loop *)priv_data, (ev_timer *)cbdata->ev);
  187. free ((void *)cbdata->ev);
  188. free ((void *)cbdata);
  189. }
  190. }
  191. static void
  192. rdns_libev_repeat_timer (void *priv_data, void *ev_data)
  193. {
  194. ev_timer *ev = (ev_timer *)ev_data;
  195. if (ev != NULL) {
  196. ev_now_update_if_cheap ((struct ev_loop *)priv_data);
  197. ev_timer_again ((struct ev_loop *)priv_data, ev);
  198. }
  199. }
  200. static void
  201. rdns_libev_del_timer (void *priv_data, void *ev_data)
  202. {
  203. ev_timer *ev = (ev_timer *)ev_data;
  204. if (ev != NULL) {
  205. ev_timer_stop ((struct ev_loop *)priv_data, ev);
  206. free ((void *)ev);
  207. }
  208. }
  209. #ifdef __cplusplus
  210. }
  211. #endif
  212. #endif /* RDNS_EV_H_ */