Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 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 |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "test/core/util/passthru_endpoint.h" |
| 35 | |
| 36 | #include <grpc/support/alloc.h> |
| 37 | #include <grpc/support/string_util.h> |
| 38 | |
| 39 | typedef struct passthru_endpoint passthru_endpoint; |
| 40 | |
| 41 | typedef struct { |
| 42 | grpc_endpoint base; |
| 43 | passthru_endpoint *parent; |
| 44 | gpr_slice_buffer read_buffer; |
| 45 | gpr_slice_buffer *on_read_out; |
| 46 | grpc_closure *on_read; |
| 47 | } half; |
| 48 | |
| 49 | struct passthru_endpoint { |
| 50 | gpr_mu mu; |
| 51 | int halves; |
| 52 | bool shutdown; |
| 53 | half client; |
| 54 | half server; |
| 55 | }; |
| 56 | |
| 57 | static void me_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 58 | gpr_slice_buffer *slices, grpc_closure *cb) { |
| 59 | half *m = (half *)ep; |
| 60 | gpr_mu_lock(&m->parent->mu); |
| 61 | if (m->parent->shutdown) { |
| 62 | grpc_exec_ctx_enqueue(exec_ctx, cb, false, NULL); |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 63 | } else if (m->read_buffer.count > 0) { |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 64 | gpr_slice_buffer_swap(&m->read_buffer, slices); |
| 65 | grpc_exec_ctx_enqueue(exec_ctx, cb, true, NULL); |
| 66 | } else { |
| 67 | m->on_read = cb; |
| 68 | m->on_read_out = slices; |
| 69 | } |
| 70 | gpr_mu_unlock(&m->parent->mu); |
| 71 | } |
| 72 | |
| 73 | static half *other_half(half *h) { |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 74 | if (h == &h->parent->client) return &h->parent->server; |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 75 | return &h->parent->client; |
| 76 | } |
| 77 | |
| 78 | static void me_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 79 | gpr_slice_buffer *slices, grpc_closure *cb) { |
| 80 | half *m = other_half((half *)ep); |
| 81 | gpr_mu_lock(&m->parent->mu); |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 82 | bool ok = true; |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 83 | if (m->parent->shutdown) { |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 84 | ok = false; |
| 85 | } else if (m->on_read != NULL) { |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 86 | gpr_slice_buffer_addn(m->on_read_out, slices->slices, slices->count); |
| 87 | grpc_exec_ctx_enqueue(exec_ctx, m->on_read, true, NULL); |
| 88 | m->on_read = NULL; |
| 89 | } else { |
| 90 | gpr_slice_buffer_addn(&m->read_buffer, slices->slices, slices->count); |
| 91 | } |
| 92 | gpr_mu_unlock(&m->parent->mu); |
| 93 | grpc_exec_ctx_enqueue(exec_ctx, cb, ok, NULL); |
| 94 | } |
| 95 | |
| 96 | static void me_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 97 | grpc_pollset *pollset) {} |
| 98 | |
| 99 | static void me_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 100 | grpc_pollset_set *pollset) {} |
| 101 | |
| 102 | static void me_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 103 | half *m = (half *)ep; |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 104 | gpr_mu_lock(&m->parent->mu); |
| 105 | m->parent->shutdown = true; |
| 106 | if (m->on_read) { |
| 107 | grpc_exec_ctx_enqueue(exec_ctx, m->on_read, false, NULL); |
| 108 | m->on_read = NULL; |
| 109 | } |
| 110 | m = other_half(m); |
| 111 | if (m->on_read) { |
| 112 | grpc_exec_ctx_enqueue(exec_ctx, m->on_read, false, NULL); |
| 113 | m->on_read = NULL; |
| 114 | } |
| 115 | gpr_mu_unlock(&m->parent->mu); |
| 116 | } |
| 117 | |
| 118 | static void me_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 119 | passthru_endpoint *p = ((half *)ep)->parent; |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 120 | gpr_mu_lock(&p->mu); |
| 121 | if (0 == --p->halves) { |
| 122 | gpr_mu_unlock(&p->mu); |
| 123 | gpr_mu_destroy(&p->mu); |
| 124 | gpr_slice_buffer_destroy(&p->client.read_buffer); |
| 125 | gpr_slice_buffer_destroy(&p->server.read_buffer); |
| 126 | gpr_free(p); |
| 127 | } else { |
| 128 | gpr_mu_unlock(&p->mu); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static char *me_get_peer(grpc_endpoint *ep) { |
| 133 | return gpr_strdup("fake:mock_endpoint"); |
| 134 | } |
| 135 | |
| 136 | static const grpc_endpoint_vtable vtable = { |
| 137 | me_read, me_write, me_add_to_pollset, me_add_to_pollset_set, |
| 138 | me_shutdown, me_destroy, me_get_peer, |
| 139 | }; |
| 140 | |
Craig Tiller | 4add20c | 2016-04-21 23:44:20 -0700 | [diff] [blame] | 141 | static void half_init(half *m, passthru_endpoint *parent) { |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 142 | m->base.vtable = &vtable; |
Craig Tiller | 4add20c | 2016-04-21 23:44:20 -0700 | [diff] [blame] | 143 | m->parent = parent; |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 144 | gpr_slice_buffer_init(&m->read_buffer); |
| 145 | m->on_read = NULL; |
| 146 | } |
| 147 | |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 148 | void grpc_passthru_endpoint_create(grpc_endpoint **client, |
| 149 | grpc_endpoint **server) { |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 150 | passthru_endpoint *m = gpr_malloc(sizeof(*m)); |
Craig Tiller | d78ca88 | 2016-04-22 14:47:05 -0700 | [diff] [blame] | 151 | m->halves = 2; |
Craig Tiller | 4add20c | 2016-04-21 23:44:20 -0700 | [diff] [blame] | 152 | half_init(&m->client, m); |
| 153 | half_init(&m->server, m); |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 154 | gpr_mu_init(&m->mu); |
| 155 | *client = &m->client.base; |
| 156 | *server = &m->server.base; |
| 157 | } |