Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H |
| 35 | #define GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #include <stddef.h> |
| 38 | |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 39 | #include "src/core/iomgr/pollset.h" |
Craig Tiller | 928cd77 | 2015-05-08 09:52:54 -0700 | [diff] [blame] | 40 | #include "src/core/iomgr/pollset_set.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include "src/core/transport/stream_op.h" |
Julien Boeuf | c6f8d0a | 2015-05-11 22:40:02 -0700 | [diff] [blame] | 42 | #include "src/core/channel/context.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | |
| 44 | /* forward declarations */ |
| 45 | typedef struct grpc_transport grpc_transport; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | |
| 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. */ |
| 50 | typedef struct grpc_stream grpc_stream; |
| 51 | |
| 52 | /* Represents the send/recv closed state of a stream. */ |
| 53 | typedef enum grpc_stream_state { |
| 54 | /* the stream is open for sends and receives */ |
| 55 | GRPC_STREAM_OPEN, |
| 56 | /* the stream is closed for sends, but may still receive data */ |
| 57 | GRPC_STREAM_SEND_CLOSED, |
| 58 | /* the stream is closed for receives, but may still send data */ |
| 59 | GRPC_STREAM_RECV_CLOSED, |
| 60 | /* the stream is closed for both sends and receives */ |
| 61 | GRPC_STREAM_CLOSED |
| 62 | } grpc_stream_state; |
| 63 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 64 | /* Transport stream op: a set of operations to perform on a transport |
| 65 | against a single stream */ |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 66 | typedef struct grpc_transport_stream_op { |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 67 | grpc_iomgr_closure *on_consumed; |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 68 | |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 69 | grpc_stream_op_buffer *send_ops; |
| 70 | int is_last_send; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 71 | grpc_iomgr_closure *on_done_send; |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 72 | |
| 73 | grpc_stream_op_buffer *recv_ops; |
| 74 | grpc_stream_state *recv_state; |
Craig Tiller | 4efb696 | 2015-06-03 09:32:41 -0700 | [diff] [blame] | 75 | /** The number of bytes this peer is currently prepared to receive. |
Craig Tiller | 5065d72 | 2015-07-15 16:58:59 -0700 | [diff] [blame] | 76 | These bytes will be eventually used to replenish per-stream flow control |
| 77 | windows. */ |
Craig Tiller | 4efb696 | 2015-06-03 09:32:41 -0700 | [diff] [blame] | 78 | gpr_uint32 max_recv_bytes; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 79 | grpc_iomgr_closure *on_done_recv; |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 80 | |
| 81 | grpc_pollset *bind_pollset; |
| 82 | |
| 83 | grpc_status_code cancel_with_status; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 84 | |
| 85 | /* Indexes correspond to grpc_context_index enum values */ |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 86 | grpc_call_context_element *context; |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 87 | } grpc_transport_stream_op; |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 88 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 89 | /** Transport op: a set of operations to perform on a transport as a whole */ |
| 90 | typedef struct grpc_transport_op { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 91 | /** called when processing of this op is done */ |
| 92 | grpc_iomgr_closure *on_consumed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 93 | /** connectivity monitoring */ |
| 94 | grpc_iomgr_closure *on_connectivity_state_change; |
| 95 | grpc_connectivity_state *connectivity_state; |
| 96 | /** should the transport be disconnected */ |
| 97 | int disconnect; |
Craig Tiller | 9188d7a | 2015-07-05 12:44:37 -0700 | [diff] [blame] | 98 | /** should we send a goaway? |
| 99 | after a goaway is sent, once there are no more active calls on |
| 100 | the transport, the transport should disconnect */ |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 101 | int send_goaway; |
| 102 | /** what should the goaway contain? */ |
| 103 | grpc_status_code goaway_status; |
| 104 | gpr_slice *goaway_message; |
| 105 | /** set the callback for accepting new streams; |
| 106 | this is a permanent callback, unlike the other one-shot closures */ |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 107 | void (*set_accept_stream)(void *user_data, grpc_transport *transport, |
| 108 | const void *server_data); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 109 | void *set_accept_stream_user_data; |
| 110 | /** add this transport to a pollset */ |
| 111 | grpc_pollset *bind_pollset; |
| 112 | /** send a ping, call this back if not NULL */ |
| 113 | grpc_iomgr_closure *send_ping; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | } grpc_transport_op; |
| 115 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 116 | /* Returns the amount of memory required to store a grpc_stream for this |
| 117 | transport */ |
| 118 | size_t grpc_transport_stream_size(grpc_transport *transport); |
| 119 | |
| 120 | /* Initialize transport data for a stream. |
| 121 | |
| 122 | Returns 0 on success, any other (transport-defined) value for failure. |
| 123 | |
| 124 | Arguments: |
| 125 | transport - the transport on which to create this stream |
| 126 | stream - a pointer to uninitialized memory to initialize |
| 127 | server_data - either NULL for a client initiated stream, or a pointer |
| 128 | supplied from the accept_stream callback function */ |
| 129 | int grpc_transport_init_stream(grpc_transport *transport, grpc_stream *stream, |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 130 | const void *server_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 131 | grpc_transport_stream_op *initial_op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 132 | |
| 133 | /* Destroy transport data for a stream. |
| 134 | |
| 135 | Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been |
| 136 | received by the up-layer. Must not be called in the same call stack as |
| 137 | recv_frame. |
| 138 | |
| 139 | Arguments: |
| 140 | transport - the transport on which to create this stream |
| 141 | stream - the grpc_stream to destroy (memory is still owned by the |
| 142 | caller, but any child memory must be cleaned up) */ |
| 143 | void grpc_transport_destroy_stream(grpc_transport *transport, |
| 144 | grpc_stream *stream); |
| 145 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 146 | void grpc_transport_stream_op_finish_with_failure(grpc_transport_stream_op *op); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 147 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 148 | void grpc_transport_stream_op_add_cancellation(grpc_transport_stream_op *op, |
| 149 | grpc_status_code status, |
| 150 | grpc_mdstr *message); |
Craig Tiller | 2ea37fd | 2015-04-24 13:03:49 -0700 | [diff] [blame] | 151 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 152 | char *grpc_transport_stream_op_string(grpc_transport_stream_op *op); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 153 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | /* Send a batch of operations on a transport |
| 155 | |
| 156 | Takes ownership of any objects contained in ops. |
| 157 | |
| 158 | Arguments: |
| 159 | transport - the transport on which to initiate the stream |
| 160 | stream - the stream on which to send the operations. This must be |
| 161 | non-NULL and previously initialized by the same transport. |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 162 | op - a grpc_transport_stream_op specifying the op to perform */ |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 163 | void grpc_transport_perform_stream_op(grpc_transport *transport, |
| 164 | grpc_stream *stream, |
| 165 | grpc_transport_stream_op *op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 167 | void grpc_transport_perform_op(grpc_transport *transport, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 168 | grpc_transport_op *op); |
| 169 | |
| 170 | /* Send a ping on a transport |
| 171 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 172 | Calls cb with user data when a response is received. */ |
| 173 | void grpc_transport_ping(grpc_transport *transport, grpc_iomgr_closure *cb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 175 | /* Advise peer of pending connection termination. */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 176 | void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status, |
| 177 | gpr_slice debug_data); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 178 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 179 | /* Close a transport. Aborts all open streams. */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 180 | void grpc_transport_close(grpc_transport *transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | |
| 182 | /* Destroy the transport */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 183 | void grpc_transport_destroy(grpc_transport *transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 184 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 185 | /* Get the transports peer */ |
| 186 | char *grpc_transport_get_peer(grpc_transport *transport); |
| 187 | |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 188 | #endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */ |