blob: c7bcd2de7b4954ed259a9b740d602c2ff9b7548d [file] [log] [blame]
Craig Tiller62c7a5a2016-04-13 22:25:03 -07001/*
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
39typedef struct passthru_endpoint passthru_endpoint;
40
41typedef 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
49struct passthru_endpoint {
50 gpr_mu mu;
51 int halves;
52 bool shutdown;
53 half client;
54 half server;
55};
56
57static 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 Tillerd2fd7692016-04-13 22:44:48 -070063 } else if (m->read_buffer.count > 0) {
Craig Tiller62c7a5a2016-04-13 22:25:03 -070064 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
73static half *other_half(half *h) {
Craig Tillerd2fd7692016-04-13 22:44:48 -070074 if (h == &h->parent->client) return &h->parent->server;
Craig Tiller62c7a5a2016-04-13 22:25:03 -070075 return &h->parent->client;
76}
77
78static 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 Tillerd2fd7692016-04-13 22:44:48 -070082 bool ok = true;
Craig Tiller62c7a5a2016-04-13 22:25:03 -070083 if (m->parent->shutdown) {
Craig Tillerd2fd7692016-04-13 22:44:48 -070084 ok = false;
85 } else if (m->on_read != NULL) {
Craig Tiller62c7a5a2016-04-13 22:25:03 -070086 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
96static void me_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
97 grpc_pollset *pollset) {}
98
99static void me_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
100 grpc_pollset_set *pollset) {}
101
102static void me_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
Craig Tillerd2fd7692016-04-13 22:44:48 -0700103 half *m = (half *)ep;
Craig Tiller62c7a5a2016-04-13 22:25:03 -0700104 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
118static void me_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
Craig Tillerd2fd7692016-04-13 22:44:48 -0700119 passthru_endpoint *p = ((half *)ep)->parent;
Craig Tiller62c7a5a2016-04-13 22:25:03 -0700120 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
132static char *me_get_peer(grpc_endpoint *ep) {
133 return gpr_strdup("fake:mock_endpoint");
134}
135
136static 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 Tiller4add20c2016-04-21 23:44:20 -0700141static void half_init(half *m, passthru_endpoint *parent) {
Craig Tiller62c7a5a2016-04-13 22:25:03 -0700142 m->base.vtable = &vtable;
Craig Tiller4add20c2016-04-21 23:44:20 -0700143 m->parent = parent;
Craig Tiller62c7a5a2016-04-13 22:25:03 -0700144 gpr_slice_buffer_init(&m->read_buffer);
145 m->on_read = NULL;
146}
147
Craig Tillerd2fd7692016-04-13 22:44:48 -0700148void grpc_passthru_endpoint_create(grpc_endpoint **client,
149 grpc_endpoint **server) {
Craig Tiller62c7a5a2016-04-13 22:25:03 -0700150 passthru_endpoint *m = gpr_malloc(sizeof(*m));
Craig Tillerd78ca882016-04-22 14:47:05 -0700151 m->halves = 2;
Craig Tiller4add20c2016-04-21 23:44:20 -0700152 half_init(&m->client, m);
153 half_init(&m->server, m);
Craig Tiller62c7a5a2016-04-13 22:25:03 -0700154 gpr_mu_init(&m->mu);
155 *client = &m->client.base;
156 *server = &m->server.base;
157}