blob: 80d842b4be0c607e7634a60eabeefe17d985c04f [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;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
47/* grpc_stream doesn't actually exist. It's used as a typesafe
48 opaque pointer for whatever data the transport wants to track
49 for a stream. */
50typedef struct grpc_stream grpc_stream;
51
52/* Represents the send/recv closed state of a stream. */
Craig Tiller45724b32015-09-22 10:42:19 -070053typedef enum grpc_stream_state
54{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055 /* 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 Tiller3f475422015-06-25 10:43:05 -070065/* Transport stream op: a set of operations to perform on a transport
66 against a single stream */
Craig Tiller45724b32015-09-22 10:42:19 -070067typedef struct grpc_transport_stream_op
68{
Craig Tiller33825112015-09-18 07:44:19 -070069 grpc_closure *on_consumed;
Craig Tiller5dde66e2015-06-02 09:05:23 -070070
Craig Tiller3f2c2212015-04-23 07:56:33 -070071 grpc_stream_op_buffer *send_ops;
72 int is_last_send;
Craig Tiller33825112015-09-18 07:44:19 -070073 grpc_closure *on_done_send;
Craig Tiller3f2c2212015-04-23 07:56:33 -070074
75 grpc_stream_op_buffer *recv_ops;
76 grpc_stream_state *recv_state;
Craig Tiller4efb6962015-06-03 09:32:41 -070077 /** The number of bytes this peer is currently prepared to receive.
Craig Tiller5065d722015-07-15 16:58:59 -070078 These bytes will be eventually used to replenish per-stream flow control
79 windows. */
Craig Tillerf96dfc32015-09-10 14:43:18 -070080 size_t max_recv_bytes;
Craig Tiller33825112015-09-18 07:44:19 -070081 grpc_closure *on_done_recv;
Craig Tiller3f2c2212015-04-23 07:56:33 -070082
83 grpc_pollset *bind_pollset;
84
Craig Tiller45ce9272015-07-31 11:22:35 -070085 /** If != GRPC_STATUS_OK, cancel this stream */
Craig Tiller3f2c2212015-04-23 07:56:33 -070086 grpc_status_code cancel_with_status;
Craig Tiller935cf422015-05-01 14:10:46 -070087
Craig Tiller45ce9272015-07-31 11:22:35 -070088 /** If != GRPC_STATUS_OK, send grpc-status, grpc-message, and close this
89 stream for both reading and writing */
90 grpc_status_code close_with_status;
91 gpr_slice *optional_close_message;
92
Craig Tiller935cf422015-05-01 14:10:46 -070093 /* Indexes correspond to grpc_context_index enum values */
Julien Boeuf83b02972015-05-20 22:50:34 -070094 grpc_call_context_element *context;
Craig Tillerb7959a02015-06-25 08:50:54 -070095} grpc_transport_stream_op;
Craig Tiller3f2c2212015-04-23 07:56:33 -070096
Craig Tiller3f475422015-06-25 10:43:05 -070097/** Transport op: a set of operations to perform on a transport as a whole */
Craig Tiller45724b32015-09-22 10:42:19 -070098typedef struct grpc_transport_op
99{
Craig Tillere039f032015-06-25 12:54:23 -0700100 /** called when processing of this op is done */
Craig Tiller33825112015-09-18 07:44:19 -0700101 grpc_closure *on_consumed;
Craig Tiller3f475422015-06-25 10:43:05 -0700102 /** connectivity monitoring */
Craig Tiller33825112015-09-18 07:44:19 -0700103 grpc_closure *on_connectivity_state_change;
Craig Tiller3f475422015-06-25 10:43:05 -0700104 grpc_connectivity_state *connectivity_state;
105 /** should the transport be disconnected */
106 int disconnect;
Craig Tiller9188d7a2015-07-05 12:44:37 -0700107 /** should we send a goaway?
108 after a goaway is sent, once there are no more active calls on
109 the transport, the transport should disconnect */
Craig Tiller3f475422015-06-25 10:43:05 -0700110 int send_goaway;
111 /** what should the goaway contain? */
112 grpc_status_code goaway_status;
113 gpr_slice *goaway_message;
114 /** set the callback for accepting new streams;
115 this is a permanent callback, unlike the other one-shot closures */
Craig Tiller45724b32015-09-22 10:42:19 -0700116 void (*set_accept_stream) (void *user_data, grpc_transport * transport, const void *server_data);
Craig Tiller3f475422015-06-25 10:43:05 -0700117 void *set_accept_stream_user_data;
118 /** add this transport to a pollset */
119 grpc_pollset *bind_pollset;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700120 /** add this transport to a pollset_set */
121 grpc_pollset_set *bind_pollset_set;
Craig Tiller3f475422015-06-25 10:43:05 -0700122 /** send a ping, call this back if not NULL */
Craig Tiller33825112015-09-18 07:44:19 -0700123 grpc_closure *send_ping;
Craig Tiller3f475422015-06-25 10:43:05 -0700124} grpc_transport_op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125
126/* Returns the amount of memory required to store a grpc_stream for this
127 transport */
Craig Tiller45724b32015-09-22 10:42:19 -0700128size_t grpc_transport_stream_size (grpc_transport * transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129
130/* Initialize transport data for a stream.
131
132 Returns 0 on success, any other (transport-defined) value for failure.
133
134 Arguments:
135 transport - the transport on which to create this stream
136 stream - a pointer to uninitialized memory to initialize
137 server_data - either NULL for a client initiated stream, or a pointer
138 supplied from the accept_stream callback function */
Craig Tiller1be70cc2015-09-22 10:45:28 -0700139int grpc_transport_init_stream (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream, const void *server_data, grpc_transport_stream_op * initial_op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800140
141/* Destroy transport data for a stream.
142
143 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
144 received by the up-layer. Must not be called in the same call stack as
145 recv_frame.
146
147 Arguments:
148 transport - the transport on which to create this stream
149 stream - the grpc_stream to destroy (memory is still owned by the
150 caller, but any child memory must be cleaned up) */
Craig Tiller1be70cc2015-09-22 10:45:28 -0700151void grpc_transport_destroy_stream (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152
Craig Tiller1be70cc2015-09-22 10:45:28 -0700153void grpc_transport_stream_op_finish_with_failure (grpc_exec_ctx * exec_ctx, grpc_transport_stream_op * op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700154
Craig Tiller45724b32015-09-22 10:42:19 -0700155void grpc_transport_stream_op_add_cancellation (grpc_transport_stream_op * op, grpc_status_code status);
Craig Tiller45ce9272015-07-31 11:22:35 -0700156
Craig Tiller45724b32015-09-22 10:42:19 -0700157void grpc_transport_stream_op_add_close (grpc_transport_stream_op * op, grpc_status_code status, gpr_slice * optional_message);
Craig Tiller2ea37fd2015-04-24 13:03:49 -0700158
Craig Tiller45724b32015-09-22 10:42:19 -0700159char *grpc_transport_stream_op_string (grpc_transport_stream_op * op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700160
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800161/* Send a batch of operations on a transport
162
163 Takes ownership of any objects contained in ops.
164
165 Arguments:
166 transport - the transport on which to initiate the stream
167 stream - the stream on which to send the operations. This must be
168 non-NULL and previously initialized by the same transport.
Craig Tillerb7959a02015-06-25 08:50:54 -0700169 op - a grpc_transport_stream_op specifying the op to perform */
Craig Tiller1be70cc2015-09-22 10:45:28 -0700170void grpc_transport_perform_stream_op (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream, grpc_transport_stream_op * op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800171
Craig Tiller1be70cc2015-09-22 10:45:28 -0700172void grpc_transport_perform_op (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_transport_op * op);
Craig Tiller3f475422015-06-25 10:43:05 -0700173
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174/* Send a ping on a transport
175
Craig Tiller4aa71a12015-06-15 13:00:55 -0700176 Calls cb with user data when a response is received. */
Craig Tiller45724b32015-09-22 10:42:19 -0700177void grpc_transport_ping (grpc_transport * transport, grpc_closure * cb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178
nnoble0c475f02014-12-05 15:37:39 -0800179/* Advise peer of pending connection termination. */
Craig Tiller45724b32015-09-22 10:42:19 -0700180void grpc_transport_goaway (grpc_transport * transport, grpc_status_code status, gpr_slice debug_data);
nnoble0c475f02014-12-05 15:37:39 -0800181
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182/* Close a transport. Aborts all open streams. */
Craig Tiller45724b32015-09-22 10:42:19 -0700183void grpc_transport_close (grpc_transport * transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184
185/* Destroy the transport */
Craig Tiller1be70cc2015-09-22 10:45:28 -0700186void grpc_transport_destroy (grpc_exec_ctx * exec_ctx, grpc_transport * transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700188/* Get the transports peer */
Craig Tiller1be70cc2015-09-22 10:45:28 -0700189char *grpc_transport_get_peer (grpc_exec_ctx * exec_ctx, grpc_transport * transport);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700190
Craig Tiller06aeea72015-04-23 10:54:45 -0700191#endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */