blob: fe47fea3062407f4f5cfde38aedf3f27255825be [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * 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
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H
35#define GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <stddef.h>
38
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/channel/context.h"
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070040#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070041#include "src/core/lib/iomgr/pollset.h"
42#include "src/core/lib/iomgr/pollset_set.h"
43#include "src/core/lib/transport/byte_stream.h"
44#include "src/core/lib/transport/metadata_batch.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045
Mark D. Roth035cb3a2016-06-16 14:52:41 -070046#ifdef __cplusplus
47extern "C" {
48#endif
49
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050/* forward declarations */
51typedef struct grpc_transport grpc_transport;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
53/* grpc_stream doesn't actually exist. It's used as a typesafe
54 opaque pointer for whatever data the transport wants to track
55 for a stream. */
56typedef struct grpc_stream grpc_stream;
57
Craig Tillerf0e11192016-08-19 11:32:28 -070058//#define GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller48613042015-11-29 14:45:11 -080059
Craig Tiller9d35a1f2015-11-02 14:16:12 -080060typedef struct grpc_stream_refcount {
61 gpr_refcount refs;
62 grpc_closure destroy;
Craig Tiller27e5aa42015-11-24 16:28:54 -080063#ifdef GRPC_STREAM_REFCOUNT_DEBUG
64 const char *object_type;
65#endif
Craig Tiller9d35a1f2015-11-02 14:16:12 -080066} grpc_stream_refcount;
67
Craig Tiller9d35a1f2015-11-02 14:16:12 -080068#ifdef GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller27e5aa42015-11-24 16:28:54 -080069void grpc_stream_ref_init(grpc_stream_refcount *refcount, int initial_refs,
70 grpc_iomgr_cb_func cb, void *cb_arg,
71 const char *object_type);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080072void grpc_stream_ref(grpc_stream_refcount *refcount, const char *reason);
73void grpc_stream_unref(grpc_exec_ctx *exec_ctx, grpc_stream_refcount *refcount,
74 const char *reason);
Craig Tiller27e5aa42015-11-24 16:28:54 -080075#define GRPC_STREAM_REF_INIT(rc, ir, cb, cb_arg, objtype) \
76 grpc_stream_ref_init(rc, ir, cb, cb_arg, objtype)
Craig Tiller9d35a1f2015-11-02 14:16:12 -080077#else
Craig Tiller27e5aa42015-11-24 16:28:54 -080078void grpc_stream_ref_init(grpc_stream_refcount *refcount, int initial_refs,
79 grpc_iomgr_cb_func cb, void *cb_arg);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080080void grpc_stream_ref(grpc_stream_refcount *refcount);
81void grpc_stream_unref(grpc_exec_ctx *exec_ctx, grpc_stream_refcount *refcount);
Craig Tiller27e5aa42015-11-24 16:28:54 -080082#define GRPC_STREAM_REF_INIT(rc, ir, cb, cb_arg, objtype) \
83 grpc_stream_ref_init(rc, ir, cb, cb_arg)
Craig Tiller9d35a1f2015-11-02 14:16:12 -080084#endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085
Craig Tiller466129e2016-03-09 14:43:18 -080086typedef struct {
87 uint64_t framing_bytes;
88 uint64_t data_bytes;
89 uint64_t header_bytes;
90} grpc_transport_one_way_stats;
91
92typedef struct grpc_transport_stream_stats {
93 grpc_transport_one_way_stats incoming;
94 grpc_transport_one_way_stats outgoing;
95} grpc_transport_stream_stats;
96
97void grpc_transport_move_one_way_stats(grpc_transport_one_way_stats *from,
98 grpc_transport_one_way_stats *to);
99
100void grpc_transport_move_stats(grpc_transport_stream_stats *from,
101 grpc_transport_stream_stats *to);
102
Craig Tiller93023e42016-07-11 12:43:23 -0700103typedef struct {
104 grpc_closure closure;
105 void *args[2];
106} grpc_transport_private_op_data;
107
Craig Tiller3f475422015-06-25 10:43:05 -0700108/* Transport stream op: a set of operations to perform on a transport
109 against a single stream */
Craig Tillera82950e2015-09-22 12:33:20 -0700110typedef struct grpc_transport_stream_op {
Craig Tillera67a83e2016-03-22 21:43:59 -0700111 /** Should be enqueued when all requested operations (excluding recv_message
112 and recv_initial_metadata which have their own closures) in a given batch
113 have been completed. */
114 grpc_closure *on_complete;
115
Craig Tillerc6549762016-03-09 17:10:43 -0800116 /** Send initial metadata to the peer, from the provided metadata batch.
117 idempotent_request MUST be set if this is non-null */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800118 grpc_metadata_batch *send_initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800119 /** Iff send_initial_metadata != NULL, flags associated with
120 send_initial_metadata: a bitfield of GRPC_INITIAL_METADATA_xxx */
121 uint32_t send_initial_metadata_flags;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500122
123 /** Send trailing metadata to the peer, from the provided metadata batch. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800124 grpc_metadata_batch *send_trailing_metadata;
Craig Tiller5dde66e2015-06-02 09:05:23 -0700125
Robbie Shade9891e8f2016-01-25 13:19:32 -0500126 /** Send message data to the peer, from the provided byte stream. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800127 grpc_byte_stream *send_message;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700128
Robbie Shade9891e8f2016-01-25 13:19:32 -0500129 /** Receive initial metadata from the stream, into provided metadata batch. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800130 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800131 bool *recv_idempotent_request;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800132 /** Should be enqueued when initial metadata is ready to be processed. */
133 grpc_closure *recv_initial_metadata_ready;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500134
135 /** Receive message data from the stream, into provided byte stream. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800136 grpc_byte_stream **recv_message;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500137 /** Should be enqueued when one message is ready to be processed. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800138 grpc_closure *recv_message_ready;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500139
Robbie Shadecdc182e2016-01-29 09:39:57 -0500140 /** Receive trailing metadata from the stream, into provided metadata batch.
141 */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800142 grpc_metadata_batch *recv_trailing_metadata;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700143
Craig Tiller466129e2016-03-09 14:43:18 -0800144 /** Collect any stats into provided buffer, zero internal stat counters */
145 grpc_transport_stream_stats *collect_stats;
146
Craig Tillerf0f70a82016-06-23 13:55:06 -0700147 /** If != GRPC_ERROR_NONE, cancel this stream */
148 grpc_error *cancel_error;
Craig Tiller935cf422015-05-01 14:10:46 -0700149
Nicolas "Pixel" Noble4b4266a2016-07-23 00:20:02 +0200150 /** If != GRPC_ERROR_NONE, send grpc-status, grpc-message, and close this
Craig Tiller45ce9272015-07-31 11:22:35 -0700151 stream for both reading and writing */
Craig Tillerf0f70a82016-06-23 13:55:06 -0700152 grpc_error *close_error;
Craig Tiller45ce9272015-07-31 11:22:35 -0700153
Craig Tiller935cf422015-05-01 14:10:46 -0700154 /* Indexes correspond to grpc_context_index enum values */
Julien Boeuf83b02972015-05-20 22:50:34 -0700155 grpc_call_context_element *context;
Craig Tiller93023e42016-07-11 12:43:23 -0700156
157 /***************************************************************************
158 * remaining fields are initialized and used at the discretion of the
159 * transport implementation */
160
161 grpc_transport_private_op_data transport_private;
Craig Tillerb7959a02015-06-25 08:50:54 -0700162} grpc_transport_stream_op;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700163
Craig Tiller3f475422015-06-25 10:43:05 -0700164/** Transport op: a set of operations to perform on a transport as a whole */
Craig Tillera82950e2015-09-22 12:33:20 -0700165typedef struct grpc_transport_op {
Craig Tiller1bd9ea42016-03-03 21:09:31 -0800166 /** Called when processing of this op is done. */
Craig Tiller33825112015-09-18 07:44:19 -0700167 grpc_closure *on_consumed;
Craig Tillerb5585d42015-11-17 07:18:31 -0800168 /** connectivity monitoring - set connectivity_state to NULL to unsubscribe */
Craig Tiller33825112015-09-18 07:44:19 -0700169 grpc_closure *on_connectivity_state_change;
Craig Tiller3f475422015-06-25 10:43:05 -0700170 grpc_connectivity_state *connectivity_state;
171 /** should the transport be disconnected */
Craig Tiller804ff712016-05-05 16:25:40 -0700172 grpc_error *disconnect_with_error;
Craig Tiller9188d7a2015-07-05 12:44:37 -0700173 /** should we send a goaway?
174 after a goaway is sent, once there are no more active calls on
175 the transport, the transport should disconnect */
Mark D. Roth7f8db252016-06-24 08:24:15 -0700176 bool send_goaway;
Craig Tiller3f475422015-06-25 10:43:05 -0700177 /** what should the goaway contain? */
178 grpc_status_code goaway_status;
179 gpr_slice *goaway_message;
180 /** set the callback for accepting new streams;
Craig Tiller389297d2016-03-03 21:02:54 -0800181 this is a permanent callback, unlike the other one-shot closures.
182 If true, the callback is set to set_accept_stream_fn, with its
183 user_data argument set to set_accept_stream_user_data */
Craig Tillerd7f12e32016-03-03 10:08:31 -0800184 bool set_accept_stream;
185 void (*set_accept_stream_fn)(grpc_exec_ctx *exec_ctx, void *user_data,
186 grpc_transport *transport,
187 const void *server_data);
Craig Tiller3f475422015-06-25 10:43:05 -0700188 void *set_accept_stream_user_data;
189 /** add this transport to a pollset */
190 grpc_pollset *bind_pollset;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700191 /** add this transport to a pollset_set */
192 grpc_pollset_set *bind_pollset_set;
Craig Tiller3f475422015-06-25 10:43:05 -0700193 /** send a ping, call this back if not NULL */
Craig Tiller33825112015-09-18 07:44:19 -0700194 grpc_closure *send_ping;
Craig Tiller93023e42016-07-11 12:43:23 -0700195
196 /***************************************************************************
197 * remaining fields are initialized and used at the discretion of the
198 * transport implementation */
199
200 grpc_transport_private_op_data transport_private;
Craig Tiller3f475422015-06-25 10:43:05 -0700201} grpc_transport_op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800202
203/* Returns the amount of memory required to store a grpc_stream for this
204 transport */
Craig Tillera82950e2015-09-22 12:33:20 -0700205size_t grpc_transport_stream_size(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206
207/* Initialize transport data for a stream.
208
209 Returns 0 on success, any other (transport-defined) value for failure.
210
211 Arguments:
212 transport - the transport on which to create this stream
213 stream - a pointer to uninitialized memory to initialize
214 server_data - either NULL for a client initiated stream, or a pointer
215 supplied from the accept_stream callback function */
Craig Tillera82950e2015-09-22 12:33:20 -0700216int grpc_transport_init_stream(grpc_exec_ctx *exec_ctx,
217 grpc_transport *transport, grpc_stream *stream,
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800218 grpc_stream_refcount *refcount,
219 const void *server_data);
220
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700221void grpc_transport_set_pops(grpc_exec_ctx *exec_ctx, grpc_transport *transport,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700222 grpc_stream *stream, grpc_polling_entity *pollent);
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700223
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224/* Destroy transport data for a stream.
225
226 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
227 received by the up-layer. Must not be called in the same call stack as
228 recv_frame.
229
230 Arguments:
231 transport - the transport on which to create this stream
232 stream - the grpc_stream to destroy (memory is still owned by the
233 caller, but any child memory must be cleaned up) */
Craig Tillera82950e2015-09-22 12:33:20 -0700234void grpc_transport_destroy_stream(grpc_exec_ctx *exec_ctx,
235 grpc_transport *transport,
Craig Tiller2c8063c2016-03-22 22:12:15 -0700236 grpc_stream *stream, void *and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800237
Craig Tillera82950e2015-09-22 12:33:20 -0700238void grpc_transport_stream_op_finish_with_failure(grpc_exec_ctx *exec_ctx,
Craig Tillerf51457b2016-05-03 17:06:32 -0700239 grpc_transport_stream_op *op,
240 grpc_error *error);
Craig Tiller83f88d92015-04-21 16:02:05 -0700241
Craig Tillera82950e2015-09-22 12:33:20 -0700242void grpc_transport_stream_op_add_cancellation(grpc_transport_stream_op *op,
243 grpc_status_code status);
Craig Tiller45ce9272015-07-31 11:22:35 -0700244
Yuchen Zengec066b32016-06-13 18:10:23 -0700245void grpc_transport_stream_op_add_cancellation_with_message(
246 grpc_transport_stream_op *op, grpc_status_code status,
247 gpr_slice *optional_message);
248
Craig Tillera82950e2015-09-22 12:33:20 -0700249void grpc_transport_stream_op_add_close(grpc_transport_stream_op *op,
250 grpc_status_code status,
251 gpr_slice *optional_message);
Craig Tiller2ea37fd2015-04-24 13:03:49 -0700252
Craig Tillera82950e2015-09-22 12:33:20 -0700253char *grpc_transport_stream_op_string(grpc_transport_stream_op *op);
Craig Tillere194ff02016-08-29 15:48:41 -0700254char *grpc_transport_op_string(grpc_transport_op *op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700255
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800256/* Send a batch of operations on a transport
257
258 Takes ownership of any objects contained in ops.
259
260 Arguments:
261 transport - the transport on which to initiate the stream
262 stream - the stream on which to send the operations. This must be
263 non-NULL and previously initialized by the same transport.
Craig Tillerb7959a02015-06-25 08:50:54 -0700264 op - a grpc_transport_stream_op specifying the op to perform */
Craig Tillera82950e2015-09-22 12:33:20 -0700265void grpc_transport_perform_stream_op(grpc_exec_ctx *exec_ctx,
266 grpc_transport *transport,
267 grpc_stream *stream,
268 grpc_transport_stream_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269
Craig Tillera82950e2015-09-22 12:33:20 -0700270void grpc_transport_perform_op(grpc_exec_ctx *exec_ctx,
271 grpc_transport *transport,
272 grpc_transport_op *op);
Craig Tiller3f475422015-06-25 10:43:05 -0700273
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800274/* Send a ping on a transport
275
Craig Tiller4aa71a12015-06-15 13:00:55 -0700276 Calls cb with user data when a response is received. */
Craig Tillera82950e2015-09-22 12:33:20 -0700277void grpc_transport_ping(grpc_transport *transport, grpc_closure *cb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278
nnoble0c475f02014-12-05 15:37:39 -0800279/* Advise peer of pending connection termination. */
Craig Tillera82950e2015-09-22 12:33:20 -0700280void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status,
281 gpr_slice debug_data);
nnoble0c475f02014-12-05 15:37:39 -0800282
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800283/* Close a transport. Aborts all open streams. */
Craig Tillera82950e2015-09-22 12:33:20 -0700284void grpc_transport_close(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800285
286/* Destroy the transport */
Craig Tillera82950e2015-09-22 12:33:20 -0700287void grpc_transport_destroy(grpc_exec_ctx *exec_ctx, grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800288
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700289/* Get the transports peer */
Craig Tillera82950e2015-09-22 12:33:20 -0700290char *grpc_transport_get_peer(grpc_exec_ctx *exec_ctx,
291 grpc_transport *transport);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700292
Craig Tillere0221ff2016-07-11 15:56:08 -0700293/* Allocate a grpc_transport_op, and preconfigure the on_consumed closure to
294 \a on_consumed and then delete the returned transport op */
295grpc_transport_op *grpc_make_transport_op(grpc_closure *on_consumed);
Craig Tillerdfd3a8f2016-08-24 09:43:45 -0700296/* Allocate a grpc_transport_stream_op, and preconfigure the on_consumed closure
297 to \a on_consumed and then delete the returned transport op */
298grpc_transport_stream_op *grpc_make_transport_stream_op(
299 grpc_closure *on_consumed);
Craig Tillere0221ff2016-07-11 15:56:08 -0700300
Mark D. Roth035cb3a2016-06-16 14:52:41 -0700301#ifdef __cplusplus
302}
303#endif
304
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700305#endif /* GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H */