blob: 72bc492d8004cd99a11d9a989251b31f3cb1b3f1 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H
35#define GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <stddef.h>
38
ctillerd79b4862014-12-17 16:36:59 -080039#include "src/core/iomgr/pollset.h"
Craig Tiller928cd772015-05-08 09:52:54 -070040#include "src/core/iomgr/pollset_set.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include "src/core/transport/stream_op.h"
Julien Boeufc6f8d0a2015-05-11 22:40:02 -070042#include "src/core/channel/context.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
44/* forward declarations */
45typedef struct grpc_transport grpc_transport;
46typedef struct grpc_transport_callbacks grpc_transport_callbacks;
47
48/* grpc_stream doesn't actually exist. It's used as a typesafe
49 opaque pointer for whatever data the transport wants to track
50 for a stream. */
51typedef struct grpc_stream grpc_stream;
52
53/* Represents the send/recv closed state of a stream. */
54typedef enum grpc_stream_state {
55 /* the stream is open for sends and receives */
56 GRPC_STREAM_OPEN,
57 /* the stream is closed for sends, but may still receive data */
58 GRPC_STREAM_SEND_CLOSED,
59 /* the stream is closed for receives, but may still send data */
60 GRPC_STREAM_RECV_CLOSED,
61 /* the stream is closed for both sends and receives */
62 GRPC_STREAM_CLOSED
63} grpc_stream_state;
64
Craig Tiller3f2c2212015-04-23 07:56:33 -070065/* Transport op: a set of operations to perform on a transport */
Craig Tillerb7959a02015-06-25 08:50:54 -070066typedef struct grpc_transport_stream_op {
Craig Tiller5dde66e2015-06-02 09:05:23 -070067 void (*on_consumed)(void *user_data, int success);
68 void *on_consumed_user_data;
69
Craig Tiller3f2c2212015-04-23 07:56:33 -070070 grpc_stream_op_buffer *send_ops;
71 int is_last_send;
72 void (*on_done_send)(void *user_data, int success);
73 void *send_user_data;
74
75 grpc_stream_op_buffer *recv_ops;
76 grpc_stream_state *recv_state;
77 void (*on_done_recv)(void *user_data, int success);
78 void *recv_user_data;
79
80 grpc_pollset *bind_pollset;
81
82 grpc_status_code cancel_with_status;
Craig Tiller2ea37fd2015-04-24 13:03:49 -070083 grpc_mdstr *cancel_message;
Craig Tiller935cf422015-05-01 14:10:46 -070084
85 /* Indexes correspond to grpc_context_index enum values */
Julien Boeuf83b02972015-05-20 22:50:34 -070086 grpc_call_context_element *context;
Craig Tillerb7959a02015-06-25 08:50:54 -070087} grpc_transport_stream_op;
Craig Tiller3f2c2212015-04-23 07:56:33 -070088
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089/* Callbacks made from the transport to the upper layers of grpc. */
90struct grpc_transport_callbacks {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091 /* Initialize a new stream on behalf of the transport.
92 Must result in a call to
93 grpc_transport_init_stream(transport, ..., request) in the same call
94 stack.
95 Must not result in any other calls to the transport.
96
97 Arguments:
98 user_data - the transport user data set at transport creation time
99 transport - the grpc_transport instance making this call
100 request - request parameters for this stream (owned by the caller)
101 server_data - opaque transport dependent argument that should be passed
102 to grpc_transport_init_stream
103 */
104 void (*accept_stream)(void *user_data, grpc_transport *transport,
105 const void *server_data);
106
Craig Tiller06aeea72015-04-23 10:54:45 -0700107 void (*goaway)(void *user_data, grpc_transport *transport,
108 grpc_status_code status, gpr_slice debug);
Craig Tiller83f88d92015-04-21 16:02:05 -0700109
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 /* The transport has been closed */
111 void (*closed)(void *user_data, grpc_transport *transport);
112};
113
114/* Returns the amount of memory required to store a grpc_stream for this
115 transport */
116size_t grpc_transport_stream_size(grpc_transport *transport);
117
118/* Initialize transport data for a stream.
119
120 Returns 0 on success, any other (transport-defined) value for failure.
121
122 Arguments:
123 transport - the transport on which to create this stream
124 stream - a pointer to uninitialized memory to initialize
125 server_data - either NULL for a client initiated stream, or a pointer
126 supplied from the accept_stream callback function */
127int grpc_transport_init_stream(grpc_transport *transport, grpc_stream *stream,
Craig Tiller06aeea72015-04-23 10:54:45 -0700128 const void *server_data,
Craig Tillerb7959a02015-06-25 08:50:54 -0700129 grpc_transport_stream_op *initial_op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130
131/* Destroy transport data for a stream.
132
133 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
134 received by the up-layer. Must not be called in the same call stack as
135 recv_frame.
136
137 Arguments:
138 transport - the transport on which to create this stream
139 stream - the grpc_stream to destroy (memory is still owned by the
140 caller, but any child memory must be cleaned up) */
141void grpc_transport_destroy_stream(grpc_transport *transport,
142 grpc_stream *stream);
143
Craig Tillerb7959a02015-06-25 08:50:54 -0700144void grpc_transport_stream_op_finish_with_failure(grpc_transport_stream_op *op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700145
Craig Tillerb7959a02015-06-25 08:50:54 -0700146void grpc_transport_stream_op_add_cancellation(grpc_transport_stream_op *op,
147 grpc_status_code status,
148 grpc_mdstr *message);
Craig Tiller2ea37fd2015-04-24 13:03:49 -0700149
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700150/* TODO(ctiller): remove this */
Craig Tiller06aeea72015-04-23 10:54:45 -0700151void grpc_transport_add_to_pollset(grpc_transport *transport,
152 grpc_pollset *pollset);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700153
Craig Tillerb7959a02015-06-25 08:50:54 -0700154char *grpc_transport_stream_op_string(grpc_transport_stream_op *op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700155
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800156/* Send a batch of operations on a transport
157
158 Takes ownership of any objects contained in ops.
159
160 Arguments:
161 transport - the transport on which to initiate the stream
162 stream - the stream on which to send the operations. This must be
163 non-NULL and previously initialized by the same transport.
Craig Tillerb7959a02015-06-25 08:50:54 -0700164 op - a grpc_transport_stream_op specifying the op to perform */
Craig Tiller65582322015-04-21 09:24:41 -0700165void grpc_transport_perform_op(grpc_transport *transport, grpc_stream *stream,
Craig Tillerb7959a02015-06-25 08:50:54 -0700166 grpc_transport_stream_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800167
168/* Send a ping on a transport
169
170 Calls cb with user data when a response is received.
171 cb *MAY* be called with arbitrary transport level locks held. It is not safe
172 to call into the transport during cb. */
173void grpc_transport_ping(grpc_transport *transport, void (*cb)(void *user_data),
174 void *user_data);
175
nnoble0c475f02014-12-05 15:37:39 -0800176/* Advise peer of pending connection termination. */
ctillerd79b4862014-12-17 16:36:59 -0800177void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status,
178 gpr_slice debug_data);
nnoble0c475f02014-12-05 15:37:39 -0800179
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180/* Close a transport. Aborts all open streams. */
ctillerd79b4862014-12-17 16:36:59 -0800181void grpc_transport_close(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182
183/* Destroy the transport */
ctillerd79b4862014-12-17 16:36:59 -0800184void grpc_transport_destroy(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185
186/* Return type for grpc_transport_setup_callback */
187typedef struct grpc_transport_setup_result {
188 void *user_data;
189 const grpc_transport_callbacks *callbacks;
190} grpc_transport_setup_result;
191
192/* Given a transport, return callbacks for that transport. Used to finalize
193 setup as a transport is being created */
194typedef grpc_transport_setup_result (*grpc_transport_setup_callback)(
195 void *setup_arg, grpc_transport *transport, grpc_mdctx *mdctx);
196
197typedef struct grpc_transport_setup grpc_transport_setup;
198typedef struct grpc_transport_setup_vtable grpc_transport_setup_vtable;
199
200struct grpc_transport_setup_vtable {
Craig Tiller8b4a8742015-05-12 13:33:18 -0700201 void (*initiate)(grpc_transport_setup *setup);
Craig Tiller83b826a2015-05-13 13:43:01 -0700202 void (*add_interested_party)(grpc_transport_setup *setup,
203 grpc_pollset *pollset);
204 void (*del_interested_party)(grpc_transport_setup *setup,
205 grpc_pollset *pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206 void (*cancel)(grpc_transport_setup *setup);
207};
208
209/* Transport setup is an asynchronous utility interface for client channels to
210 establish connections. It's transport agnostic. */
211struct grpc_transport_setup {
212 const grpc_transport_setup_vtable *vtable;
213};
214
215/* Initiate transport setup: e.g. for TCP+DNS trigger a resolve of the name
216 given at transport construction time, create the tcp connection, perform
217 handshakes, and call some grpc_transport_setup_result function provided at
218 setup construction time.
219 This *may* be implemented as a no-op if the setup process monitors something
220 continuously. */
Craig Tiller8b4a8742015-05-12 13:33:18 -0700221void grpc_transport_setup_initiate(grpc_transport_setup *setup);
222
Craig Tiller83b826a2015-05-13 13:43:01 -0700223void grpc_transport_setup_add_interested_party(grpc_transport_setup *setup,
224 grpc_pollset *pollset);
225void grpc_transport_setup_del_interested_party(grpc_transport_setup *setup,
226 grpc_pollset *pollset);
Craig Tiller8b4a8742015-05-12 13:33:18 -0700227
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800228/* Cancel transport setup. After this returns, no new transports should be
229 created, and all pending transport setup callbacks should be completed.
230 After this call completes, setup should be considered invalid (this can be
231 used as a destruction call by setup). */
232void grpc_transport_setup_cancel(grpc_transport_setup *setup);
233
Craig Tiller06aeea72015-04-23 10:54:45 -0700234#endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */