bufferevent_pair.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (c) 2009-2012 Niels Provos, Nick Mathewson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "event2/event-config.h"
  27. #include "evconfig-private.h"
  28. #include <sys/types.h>
  29. #ifdef _WIN32
  30. #include <winsock2.h>
  31. #endif
  32. #include "event2/util.h"
  33. #include "event2/buffer.h"
  34. #include "event2/bufferevent.h"
  35. #include "event2/bufferevent_struct.h"
  36. #include "event2/event.h"
  37. #include "defer-internal.h"
  38. #include "bufferevent-internal.h"
  39. #include "mm-internal.h"
  40. #include "util-internal.h"
  41. struct bufferevent_pair {
  42. struct bufferevent_private bev;
  43. struct bufferevent_pair *partner;
  44. /* For ->destruct() lock checking */
  45. struct bufferevent_pair *unlinked_partner;
  46. };
  47. /* Given a bufferevent that's really a bev part of a bufferevent_pair,
  48. * return that bufferevent_filtered. Returns NULL otherwise.*/
  49. static inline struct bufferevent_pair *
  50. upcast(struct bufferevent *bev)
  51. {
  52. struct bufferevent_pair *bev_p;
  53. if (!BEV_IS_PAIR(bev))
  54. return NULL;
  55. bev_p = EVUTIL_UPCAST(bev, struct bufferevent_pair, bev.bev);
  56. EVUTIL_ASSERT(BEV_IS_PAIR(&bev_p->bev.bev));
  57. return bev_p;
  58. }
  59. #define downcast(bev_pair) (&(bev_pair)->bev.bev)
  60. static inline void
  61. incref_and_lock(struct bufferevent *b)
  62. {
  63. struct bufferevent_pair *bevp;
  64. bufferevent_incref_and_lock_(b);
  65. bevp = upcast(b);
  66. if (bevp->partner)
  67. bufferevent_incref_and_lock_(downcast(bevp->partner));
  68. }
  69. static inline void
  70. decref_and_unlock(struct bufferevent *b)
  71. {
  72. struct bufferevent_pair *bevp = upcast(b);
  73. if (bevp->partner)
  74. bufferevent_decref_and_unlock_(downcast(bevp->partner));
  75. bufferevent_decref_and_unlock_(b);
  76. }
  77. /* XXX Handle close */
  78. static void be_pair_outbuf_cb(struct evbuffer *,
  79. const struct evbuffer_cb_info *, void *);
  80. static struct bufferevent_pair *
  81. bufferevent_pair_elt_new(struct event_base *base,
  82. int options)
  83. {
  84. struct bufferevent_pair *bufev;
  85. if (! (bufev = mm_calloc(1, sizeof(struct bufferevent_pair))))
  86. return NULL;
  87. if (bufferevent_init_common_(&bufev->bev, base, &bufferevent_ops_pair,
  88. options)) {
  89. mm_free(bufev);
  90. return NULL;
  91. }
  92. if (!evbuffer_add_cb(bufev->bev.bev.output, be_pair_outbuf_cb, bufev)) {
  93. bufferevent_free(downcast(bufev));
  94. return NULL;
  95. }
  96. bufferevent_init_generic_timeout_cbs_(&bufev->bev.bev);
  97. return bufev;
  98. }
  99. int
  100. bufferevent_pair_new(struct event_base *base, int options,
  101. struct bufferevent *pair[2])
  102. {
  103. struct bufferevent_pair *bufev1 = NULL, *bufev2 = NULL;
  104. int tmp_options;
  105. options |= BEV_OPT_DEFER_CALLBACKS;
  106. tmp_options = options & ~BEV_OPT_THREADSAFE;
  107. bufev1 = bufferevent_pair_elt_new(base, options);
  108. if (!bufev1)
  109. return -1;
  110. bufev2 = bufferevent_pair_elt_new(base, tmp_options);
  111. if (!bufev2) {
  112. bufferevent_free(downcast(bufev1));
  113. return -1;
  114. }
  115. if (options & BEV_OPT_THREADSAFE) {
  116. /*XXXX check return */
  117. bufferevent_enable_locking_(downcast(bufev2), bufev1->bev.lock);
  118. }
  119. bufev1->partner = bufev2;
  120. bufev2->partner = bufev1;
  121. evbuffer_freeze(downcast(bufev1)->input, 0);
  122. evbuffer_freeze(downcast(bufev1)->output, 1);
  123. evbuffer_freeze(downcast(bufev2)->input, 0);
  124. evbuffer_freeze(downcast(bufev2)->output, 1);
  125. pair[0] = downcast(bufev1);
  126. pair[1] = downcast(bufev2);
  127. return 0;
  128. }
  129. static void
  130. be_pair_transfer(struct bufferevent *src, struct bufferevent *dst,
  131. int ignore_wm)
  132. {
  133. size_t dst_size;
  134. size_t n;
  135. evbuffer_unfreeze(src->output, 1);
  136. evbuffer_unfreeze(dst->input, 0);
  137. if (dst->wm_read.high) {
  138. dst_size = evbuffer_get_length(dst->input);
  139. if (dst_size < dst->wm_read.high) {
  140. n = dst->wm_read.high - dst_size;
  141. evbuffer_remove_buffer(src->output, dst->input, n);
  142. } else {
  143. if (!ignore_wm)
  144. goto done;
  145. n = evbuffer_get_length(src->output);
  146. evbuffer_add_buffer(dst->input, src->output);
  147. }
  148. } else {
  149. n = evbuffer_get_length(src->output);
  150. evbuffer_add_buffer(dst->input, src->output);
  151. }
  152. if (n) {
  153. BEV_RESET_GENERIC_READ_TIMEOUT(dst);
  154. if (evbuffer_get_length(dst->output))
  155. BEV_RESET_GENERIC_WRITE_TIMEOUT(dst);
  156. else
  157. BEV_DEL_GENERIC_WRITE_TIMEOUT(dst);
  158. }
  159. bufferevent_trigger_nolock_(dst, EV_READ, 0);
  160. bufferevent_trigger_nolock_(src, EV_WRITE, 0);
  161. done:
  162. evbuffer_freeze(src->output, 1);
  163. evbuffer_freeze(dst->input, 0);
  164. }
  165. static inline int
  166. be_pair_wants_to_talk(struct bufferevent_pair *src,
  167. struct bufferevent_pair *dst)
  168. {
  169. return (downcast(src)->enabled & EV_WRITE) &&
  170. (downcast(dst)->enabled & EV_READ) &&
  171. !dst->bev.read_suspended &&
  172. evbuffer_get_length(downcast(src)->output);
  173. }
  174. static void
  175. be_pair_outbuf_cb(struct evbuffer *outbuf,
  176. const struct evbuffer_cb_info *info, void *arg)
  177. {
  178. struct bufferevent_pair *bev_pair = arg;
  179. struct bufferevent_pair *partner = bev_pair->partner;
  180. incref_and_lock(downcast(bev_pair));
  181. if (info->n_added > info->n_deleted && partner) {
  182. /* We got more data. If the other side's reading, then
  183. hand it over. */
  184. if (be_pair_wants_to_talk(bev_pair, partner)) {
  185. be_pair_transfer(downcast(bev_pair), downcast(partner), 0);
  186. }
  187. }
  188. decref_and_unlock(downcast(bev_pair));
  189. }
  190. static int
  191. be_pair_enable(struct bufferevent *bufev, short events)
  192. {
  193. struct bufferevent_pair *bev_p = upcast(bufev);
  194. struct bufferevent_pair *partner = bev_p->partner;
  195. incref_and_lock(bufev);
  196. if (events & EV_READ) {
  197. BEV_RESET_GENERIC_READ_TIMEOUT(bufev);
  198. }
  199. if ((events & EV_WRITE) && evbuffer_get_length(bufev->output))
  200. BEV_RESET_GENERIC_WRITE_TIMEOUT(bufev);
  201. /* We're starting to read! Does the other side have anything to write?*/
  202. if ((events & EV_READ) && partner &&
  203. be_pair_wants_to_talk(partner, bev_p)) {
  204. be_pair_transfer(downcast(partner), bufev, 0);
  205. }
  206. /* We're starting to write! Does the other side want to read? */
  207. if ((events & EV_WRITE) && partner &&
  208. be_pair_wants_to_talk(bev_p, partner)) {
  209. be_pair_transfer(bufev, downcast(partner), 0);
  210. }
  211. decref_and_unlock(bufev);
  212. return 0;
  213. }
  214. static int
  215. be_pair_disable(struct bufferevent *bev, short events)
  216. {
  217. if (events & EV_READ) {
  218. BEV_DEL_GENERIC_READ_TIMEOUT(bev);
  219. }
  220. if (events & EV_WRITE) {
  221. BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
  222. }
  223. return 0;
  224. }
  225. static void
  226. be_pair_unlink(struct bufferevent *bev)
  227. {
  228. struct bufferevent_pair *bev_p = upcast(bev);
  229. if (bev_p->partner) {
  230. bev_p->unlinked_partner = bev_p->partner;
  231. bev_p->partner->partner = NULL;
  232. bev_p->partner = NULL;
  233. }
  234. }
  235. /* Free *shared* lock in the latest be (since we share it between two of them). */
  236. static void
  237. be_pair_destruct(struct bufferevent *bev)
  238. {
  239. struct bufferevent_pair *bev_p = upcast(bev);
  240. /* Transfer ownership of the lock into partner, otherwise we will use
  241. * already free'd lock during freeing second bev, see next example:
  242. *
  243. * bev1->own_lock = 1
  244. * bev2->own_lock = 0
  245. * bev2->lock = bev1->lock
  246. *
  247. * bufferevent_free(bev1) # refcnt == 0 -> unlink
  248. * bufferevent_free(bev2) # refcnt == 0 -> unlink
  249. *
  250. * event_base_free() -> finilizers -> EVTHREAD_FREE_LOCK(bev1->lock)
  251. * -> BEV_LOCK(bev2->lock) <-- already freed
  252. *
  253. * Where bev1 == pair[0], bev2 == pair[1].
  254. */
  255. if (bev_p->unlinked_partner && bev_p->bev.own_lock) {
  256. bev_p->unlinked_partner->bev.own_lock = 1;
  257. bev_p->bev.own_lock = 0;
  258. }
  259. bev_p->unlinked_partner = NULL;
  260. }
  261. static int
  262. be_pair_flush(struct bufferevent *bev, short iotype,
  263. enum bufferevent_flush_mode mode)
  264. {
  265. struct bufferevent_pair *bev_p = upcast(bev);
  266. struct bufferevent *partner;
  267. if (!bev_p->partner)
  268. return -1;
  269. if (mode == BEV_NORMAL)
  270. return 0;
  271. incref_and_lock(bev);
  272. partner = downcast(bev_p->partner);
  273. if ((iotype & EV_READ) != 0)
  274. be_pair_transfer(partner, bev, 1);
  275. if ((iotype & EV_WRITE) != 0)
  276. be_pair_transfer(bev, partner, 1);
  277. if (mode == BEV_FINISHED) {
  278. short what = BEV_EVENT_EOF;
  279. if (iotype & EV_READ)
  280. what |= BEV_EVENT_WRITING;
  281. if (iotype & EV_WRITE)
  282. what |= BEV_EVENT_READING;
  283. bufferevent_run_eventcb_(partner, what, 0);
  284. }
  285. decref_and_unlock(bev);
  286. return 0;
  287. }
  288. struct bufferevent *
  289. bufferevent_pair_get_partner(struct bufferevent *bev)
  290. {
  291. struct bufferevent_pair *bev_p;
  292. struct bufferevent *partner = NULL;
  293. bev_p = upcast(bev);
  294. if (! bev_p)
  295. return NULL;
  296. incref_and_lock(bev);
  297. if (bev_p->partner)
  298. partner = downcast(bev_p->partner);
  299. decref_and_unlock(bev);
  300. return partner;
  301. }
  302. const struct bufferevent_ops bufferevent_ops_pair = {
  303. "pair_elt",
  304. evutil_offsetof(struct bufferevent_pair, bev.bev),
  305. be_pair_enable,
  306. be_pair_disable,
  307. be_pair_unlink,
  308. be_pair_destruct,
  309. bufferevent_generic_adj_timeouts_,
  310. be_pair_flush,
  311. NULL, /* ctrl */
  312. };