blob: e98cfe9515cfceb57f2eb7a9dfecba86c9583cf3 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Robbie Shadecdc182e2016-01-29 09:39:57 -05003 * Copyright 2015-2016, 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"
40#include "src/core/lib/iomgr/pollset.h"
41#include "src/core/lib/iomgr/pollset_set.h"
42#include "src/core/lib/transport/byte_stream.h"
43#include "src/core/lib/transport/metadata_batch.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044
45/* forward declarations */
46typedef struct grpc_transport grpc_transport;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047
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
Craig Tiller54914ee2015-12-01 06:23:44 -080053/*#define GRPC_STREAM_REFCOUNT_DEBUG*/
Craig Tiller48613042015-11-29 14:45:11 -080054
Craig Tiller9d35a1f2015-11-02 14:16:12 -080055typedef struct grpc_stream_refcount {
56 gpr_refcount refs;
57 grpc_closure destroy;
Craig Tiller27e5aa42015-11-24 16:28:54 -080058#ifdef GRPC_STREAM_REFCOUNT_DEBUG
59 const char *object_type;
60#endif
Craig Tiller9d35a1f2015-11-02 14:16:12 -080061} grpc_stream_refcount;
62
Craig Tiller9d35a1f2015-11-02 14:16:12 -080063#ifdef GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller27e5aa42015-11-24 16:28:54 -080064void grpc_stream_ref_init(grpc_stream_refcount *refcount, int initial_refs,
65 grpc_iomgr_cb_func cb, void *cb_arg,
66 const char *object_type);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080067void grpc_stream_ref(grpc_stream_refcount *refcount, const char *reason);
68void grpc_stream_unref(grpc_exec_ctx *exec_ctx, grpc_stream_refcount *refcount,
69 const char *reason);
Craig Tiller27e5aa42015-11-24 16:28:54 -080070#define GRPC_STREAM_REF_INIT(rc, ir, cb, cb_arg, objtype) \
71 grpc_stream_ref_init(rc, ir, cb, cb_arg, objtype)
Craig Tiller9d35a1f2015-11-02 14:16:12 -080072#else
Craig Tiller27e5aa42015-11-24 16:28:54 -080073void grpc_stream_ref_init(grpc_stream_refcount *refcount, int initial_refs,
74 grpc_iomgr_cb_func cb, void *cb_arg);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080075void grpc_stream_ref(grpc_stream_refcount *refcount);
76void grpc_stream_unref(grpc_exec_ctx *exec_ctx, grpc_stream_refcount *refcount);
Craig Tiller27e5aa42015-11-24 16:28:54 -080077#define GRPC_STREAM_REF_INIT(rc, ir, cb, cb_arg, objtype) \
78 grpc_stream_ref_init(rc, ir, cb, cb_arg)
Craig Tiller9d35a1f2015-11-02 14:16:12 -080079#endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080
Craig Tiller3f475422015-06-25 10:43:05 -070081/* Transport stream op: a set of operations to perform on a transport
82 against a single stream */
Craig Tillera82950e2015-09-22 12:33:20 -070083typedef struct grpc_transport_stream_op {
Robbie Shade9891e8f2016-01-25 13:19:32 -050084 /** Send initial metadata to the peer, from the provided metadata batch. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -080085 grpc_metadata_batch *send_initial_metadata;
Robbie Shade9891e8f2016-01-25 13:19:32 -050086
87 /** Send trailing metadata to the peer, from the provided metadata batch. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -080088 grpc_metadata_batch *send_trailing_metadata;
Craig Tiller5dde66e2015-06-02 09:05:23 -070089
Robbie Shade9891e8f2016-01-25 13:19:32 -050090 /** Send message data to the peer, from the provided byte stream. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -080091 grpc_byte_stream *send_message;
Craig Tiller3f2c2212015-04-23 07:56:33 -070092
Robbie Shade9891e8f2016-01-25 13:19:32 -050093 /** Receive initial metadata from the stream, into provided metadata batch. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -080094 grpc_metadata_batch *recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -080095 /** Should be enqueued when initial metadata is ready to be processed. */
96 grpc_closure *recv_initial_metadata_ready;
Robbie Shade9891e8f2016-01-25 13:19:32 -050097
98 /** Receive message data from the stream, into provided byte stream. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -080099 grpc_byte_stream **recv_message;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500100 /** Should be enqueued when one message is ready to be processed. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800101 grpc_closure *recv_message_ready;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500102
Robbie Shadecdc182e2016-01-29 09:39:57 -0500103 /** Receive trailing metadata from the stream, into provided metadata batch.
104 */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800105 grpc_metadata_batch *recv_trailing_metadata;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700106
Robbie Shade9891e8f2016-01-25 13:19:32 -0500107 /** Should be enqueued when all requested operations (excluding recv_message
Craig Tillera44cbfc2016-02-03 16:02:49 -0800108 and recv_initial_metadata which have their own closures) in a given batch
109 have been completed. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800110 grpc_closure *on_complete;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700111
Craig Tiller45ce9272015-07-31 11:22:35 -0700112 /** If != GRPC_STATUS_OK, cancel this stream */
Craig Tiller3f2c2212015-04-23 07:56:33 -0700113 grpc_status_code cancel_with_status;
Craig Tiller935cf422015-05-01 14:10:46 -0700114
Craig Tiller45ce9272015-07-31 11:22:35 -0700115 /** If != GRPC_STATUS_OK, send grpc-status, grpc-message, and close this
116 stream for both reading and writing */
117 grpc_status_code close_with_status;
118 gpr_slice *optional_close_message;
119
Craig Tiller935cf422015-05-01 14:10:46 -0700120 /* Indexes correspond to grpc_context_index enum values */
Julien Boeuf83b02972015-05-20 22:50:34 -0700121 grpc_call_context_element *context;
Craig Tillerb7959a02015-06-25 08:50:54 -0700122} grpc_transport_stream_op;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700123
Craig Tiller3f475422015-06-25 10:43:05 -0700124/** Transport op: a set of operations to perform on a transport as a whole */
Craig Tillera82950e2015-09-22 12:33:20 -0700125typedef struct grpc_transport_op {
Craig Tiller1bd9ea42016-03-03 21:09:31 -0800126 /** Called when processing of this op is done. */
Craig Tiller33825112015-09-18 07:44:19 -0700127 grpc_closure *on_consumed;
Craig Tillerb5585d42015-11-17 07:18:31 -0800128 /** connectivity monitoring - set connectivity_state to NULL to unsubscribe */
Craig Tiller33825112015-09-18 07:44:19 -0700129 grpc_closure *on_connectivity_state_change;
Craig Tiller3f475422015-06-25 10:43:05 -0700130 grpc_connectivity_state *connectivity_state;
131 /** should the transport be disconnected */
132 int disconnect;
Craig Tiller9188d7a2015-07-05 12:44:37 -0700133 /** should we send a goaway?
134 after a goaway is sent, once there are no more active calls on
135 the transport, the transport should disconnect */
Craig Tiller3f475422015-06-25 10:43:05 -0700136 int send_goaway;
137 /** what should the goaway contain? */
138 grpc_status_code goaway_status;
139 gpr_slice *goaway_message;
140 /** set the callback for accepting new streams;
Craig Tiller389297d2016-03-03 21:02:54 -0800141 this is a permanent callback, unlike the other one-shot closures.
142 If true, the callback is set to set_accept_stream_fn, with its
143 user_data argument set to set_accept_stream_user_data */
Craig Tillerd7f12e32016-03-03 10:08:31 -0800144 bool set_accept_stream;
145 void (*set_accept_stream_fn)(grpc_exec_ctx *exec_ctx, void *user_data,
146 grpc_transport *transport,
147 const void *server_data);
Craig Tiller3f475422015-06-25 10:43:05 -0700148 void *set_accept_stream_user_data;
149 /** add this transport to a pollset */
150 grpc_pollset *bind_pollset;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700151 /** add this transport to a pollset_set */
152 grpc_pollset_set *bind_pollset_set;
Craig Tiller3f475422015-06-25 10:43:05 -0700153 /** send a ping, call this back if not NULL */
Craig Tiller33825112015-09-18 07:44:19 -0700154 grpc_closure *send_ping;
Craig Tiller3f475422015-06-25 10:43:05 -0700155} grpc_transport_op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800156
157/* Returns the amount of memory required to store a grpc_stream for this
158 transport */
Craig Tillera82950e2015-09-22 12:33:20 -0700159size_t grpc_transport_stream_size(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160
161/* Initialize transport data for a stream.
162
163 Returns 0 on success, any other (transport-defined) value for failure.
164
165 Arguments:
166 transport - the transport on which to create this stream
167 stream - a pointer to uninitialized memory to initialize
168 server_data - either NULL for a client initiated stream, or a pointer
169 supplied from the accept_stream callback function */
Craig Tillera82950e2015-09-22 12:33:20 -0700170int grpc_transport_init_stream(grpc_exec_ctx *exec_ctx,
171 grpc_transport *transport, grpc_stream *stream,
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800172 grpc_stream_refcount *refcount,
173 const void *server_data);
174
175void grpc_transport_set_pollset(grpc_exec_ctx *exec_ctx,
176 grpc_transport *transport, grpc_stream *stream,
177 grpc_pollset *pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178
179/* Destroy transport data for a stream.
180
181 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
182 received by the up-layer. Must not be called in the same call stack as
183 recv_frame.
184
185 Arguments:
186 transport - the transport on which to create this stream
187 stream - the grpc_stream to destroy (memory is still owned by the
188 caller, but any child memory must be cleaned up) */
Craig Tillera82950e2015-09-22 12:33:20 -0700189void grpc_transport_destroy_stream(grpc_exec_ctx *exec_ctx,
190 grpc_transport *transport,
191 grpc_stream *stream);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192
Craig Tillera82950e2015-09-22 12:33:20 -0700193void grpc_transport_stream_op_finish_with_failure(grpc_exec_ctx *exec_ctx,
194 grpc_transport_stream_op *op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700195
Craig Tillera82950e2015-09-22 12:33:20 -0700196void grpc_transport_stream_op_add_cancellation(grpc_transport_stream_op *op,
197 grpc_status_code status);
Craig Tiller45ce9272015-07-31 11:22:35 -0700198
Craig Tillera82950e2015-09-22 12:33:20 -0700199void grpc_transport_stream_op_add_close(grpc_transport_stream_op *op,
200 grpc_status_code status,
201 gpr_slice *optional_message);
Craig Tiller2ea37fd2015-04-24 13:03:49 -0700202
Craig Tillera82950e2015-09-22 12:33:20 -0700203char *grpc_transport_stream_op_string(grpc_transport_stream_op *op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700204
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205/* Send a batch of operations on a transport
206
207 Takes ownership of any objects contained in ops.
208
209 Arguments:
210 transport - the transport on which to initiate the stream
211 stream - the stream on which to send the operations. This must be
212 non-NULL and previously initialized by the same transport.
Craig Tillerb7959a02015-06-25 08:50:54 -0700213 op - a grpc_transport_stream_op specifying the op to perform */
Craig Tillera82950e2015-09-22 12:33:20 -0700214void grpc_transport_perform_stream_op(grpc_exec_ctx *exec_ctx,
215 grpc_transport *transport,
216 grpc_stream *stream,
217 grpc_transport_stream_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218
Craig Tillera82950e2015-09-22 12:33:20 -0700219void grpc_transport_perform_op(grpc_exec_ctx *exec_ctx,
220 grpc_transport *transport,
221 grpc_transport_op *op);
Craig Tiller3f475422015-06-25 10:43:05 -0700222
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223/* Send a ping on a transport
224
Craig Tiller4aa71a12015-06-15 13:00:55 -0700225 Calls cb with user data when a response is received. */
Craig Tillera82950e2015-09-22 12:33:20 -0700226void grpc_transport_ping(grpc_transport *transport, grpc_closure *cb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800227
nnoble0c475f02014-12-05 15:37:39 -0800228/* Advise peer of pending connection termination. */
Craig Tillera82950e2015-09-22 12:33:20 -0700229void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status,
230 gpr_slice debug_data);
nnoble0c475f02014-12-05 15:37:39 -0800231
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800232/* Close a transport. Aborts all open streams. */
Craig Tillera82950e2015-09-22 12:33:20 -0700233void grpc_transport_close(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800234
235/* Destroy the transport */
Craig Tillera82950e2015-09-22 12:33:20 -0700236void grpc_transport_destroy(grpc_exec_ctx *exec_ctx, grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800237
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700238/* Get the transports peer */
Craig Tillera82950e2015-09-22 12:33:20 -0700239char *grpc_transport_get_peer(grpc_exec_ctx *exec_ctx,
240 grpc_transport *transport);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700241
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700242#endif /* GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H */