Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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 | |
| 34 | #ifndef __GRPC_INTERNAL_TRANSPORT_TRANSPORT_H__ |
| 35 | #define __GRPC_INTERNAL_TRANSPORT_TRANSPORT_H__ |
| 36 | |
| 37 | #include <stddef.h> |
| 38 | |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 39 | #include "src/core/iomgr/pollset.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include "src/core/transport/stream_op.h" |
| 41 | |
| 42 | /* forward declarations */ |
| 43 | typedef struct grpc_transport grpc_transport; |
| 44 | typedef struct grpc_transport_callbacks grpc_transport_callbacks; |
| 45 | |
| 46 | /* grpc_stream doesn't actually exist. It's used as a typesafe |
| 47 | opaque pointer for whatever data the transport wants to track |
| 48 | for a stream. */ |
| 49 | typedef struct grpc_stream grpc_stream; |
| 50 | |
| 51 | /* Represents the send/recv closed state of a stream. */ |
| 52 | typedef enum grpc_stream_state { |
| 53 | /* the stream is open for sends and receives */ |
| 54 | GRPC_STREAM_OPEN, |
| 55 | /* the stream is closed for sends, but may still receive data */ |
| 56 | GRPC_STREAM_SEND_CLOSED, |
| 57 | /* the stream is closed for receives, but may still send data */ |
| 58 | GRPC_STREAM_RECV_CLOSED, |
| 59 | /* the stream is closed for both sends and receives */ |
| 60 | GRPC_STREAM_CLOSED |
| 61 | } grpc_stream_state; |
| 62 | |
| 63 | /* Callbacks made from the transport to the upper layers of grpc. */ |
| 64 | struct grpc_transport_callbacks { |
| 65 | /* Allocate a buffer to receive data into. |
| 66 | It's safe to call grpc_slice_new() to do this, but performance minded |
| 67 | proxies may want to carefully place data into optimal locations for |
| 68 | transports. |
| 69 | This function must return a valid, non-empty slice. |
| 70 | |
| 71 | Arguments: |
| 72 | user_data - the transport user data set at transport creation time |
| 73 | transport - the grpc_transport instance making this call |
| 74 | stream - the grpc_stream instance the buffer will be used for, or |
| 75 | NULL if this is not known |
| 76 | size_hint - how big of a buffer would the transport optimally like? |
| 77 | the actual returned buffer can be smaller or larger than |
| 78 | size_hint as the implementation finds convenient */ |
| 79 | struct gpr_slice (*alloc_recv_buffer)(void *user_data, |
| 80 | grpc_transport *transport, |
| 81 | grpc_stream *stream, size_t size_hint); |
| 82 | |
| 83 | /* Initialize a new stream on behalf of the transport. |
| 84 | Must result in a call to |
| 85 | grpc_transport_init_stream(transport, ..., request) in the same call |
| 86 | stack. |
| 87 | Must not result in any other calls to the transport. |
| 88 | |
| 89 | Arguments: |
| 90 | user_data - the transport user data set at transport creation time |
| 91 | transport - the grpc_transport instance making this call |
| 92 | request - request parameters for this stream (owned by the caller) |
| 93 | server_data - opaque transport dependent argument that should be passed |
| 94 | to grpc_transport_init_stream |
| 95 | */ |
| 96 | void (*accept_stream)(void *user_data, grpc_transport *transport, |
| 97 | const void *server_data); |
| 98 | |
| 99 | /* Process a set of stream ops that have been received by the transport. |
| 100 | Called by network threads, so must be careful not to block on network |
| 101 | activity. |
| 102 | |
| 103 | If final_state == GRPC_STREAM_CLOSED, the upper layers should arrange to |
| 104 | call grpc_transport_destroy_stream. |
| 105 | |
| 106 | Ownership of any objects contained in ops is transferred to the callee. |
| 107 | |
| 108 | Arguments: |
| 109 | user_data - the transport user data set at transport creation time |
| 110 | transport - the grpc_transport instance making this call |
| 111 | stream - the stream this data was received for |
| 112 | ops - stream operations that are part of this batch |
| 113 | ops_count - the number of stream operations in this batch |
| 114 | final_state - the state of the stream as of the final operation in this |
| 115 | batch */ |
| 116 | void (*recv_batch)(void *user_data, grpc_transport *transport, |
| 117 | grpc_stream *stream, grpc_stream_op *ops, size_t ops_count, |
| 118 | grpc_stream_state final_state); |
| 119 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 120 | /* The transport received a goaway */ |
| 121 | void (*goaway)(void *user_data, grpc_transport *transport, |
| 122 | grpc_status_code status, gpr_slice debug); |
| 123 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 124 | /* The transport has been closed */ |
| 125 | void (*closed)(void *user_data, grpc_transport *transport); |
| 126 | }; |
| 127 | |
| 128 | /* Returns the amount of memory required to store a grpc_stream for this |
| 129 | transport */ |
| 130 | size_t grpc_transport_stream_size(grpc_transport *transport); |
| 131 | |
| 132 | /* Initialize transport data for a stream. |
| 133 | |
| 134 | Returns 0 on success, any other (transport-defined) value for failure. |
| 135 | |
| 136 | Arguments: |
| 137 | transport - the transport on which to create this stream |
| 138 | stream - a pointer to uninitialized memory to initialize |
| 139 | server_data - either NULL for a client initiated stream, or a pointer |
| 140 | supplied from the accept_stream callback function */ |
| 141 | int grpc_transport_init_stream(grpc_transport *transport, grpc_stream *stream, |
| 142 | const void *server_data); |
| 143 | |
| 144 | /* Destroy transport data for a stream. |
| 145 | |
| 146 | Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been |
| 147 | received by the up-layer. Must not be called in the same call stack as |
| 148 | recv_frame. |
| 149 | |
| 150 | Arguments: |
| 151 | transport - the transport on which to create this stream |
| 152 | stream - the grpc_stream to destroy (memory is still owned by the |
| 153 | caller, but any child memory must be cleaned up) */ |
| 154 | void grpc_transport_destroy_stream(grpc_transport *transport, |
| 155 | grpc_stream *stream); |
| 156 | |
| 157 | /* Enable/disable incoming data for a stream. |
| 158 | |
| 159 | This effectively disables new window becoming available for a given stream, |
| 160 | but does not prevent existing window from being consumed by a sender: the |
| 161 | caller must still be prepared to receive some additional data after this |
| 162 | call. |
| 163 | |
| 164 | Arguments: |
| 165 | transport - the transport on which to create this stream |
| 166 | stream - the grpc_stream to destroy (memory is still owned by the |
| 167 | caller, but any child memory must be cleaned up) |
| 168 | allow - is it allowed that new window be opened up? */ |
| 169 | void grpc_transport_set_allow_window_updates(grpc_transport *transport, |
| 170 | grpc_stream *stream, int allow); |
| 171 | |
| 172 | /* Send a batch of operations on a transport |
| 173 | |
| 174 | Takes ownership of any objects contained in ops. |
| 175 | |
| 176 | Arguments: |
| 177 | transport - the transport on which to initiate the stream |
| 178 | stream - the stream on which to send the operations. This must be |
| 179 | non-NULL and previously initialized by the same transport. |
| 180 | ops - an array of operations to apply to the stream - can be NULL |
| 181 | if ops_count == 0. |
| 182 | ops_count - the number of elements in ops |
| 183 | is_last - is this the last batch of operations to be sent out */ |
| 184 | void grpc_transport_send_batch(grpc_transport *transport, grpc_stream *stream, |
| 185 | grpc_stream_op *ops, size_t ops_count, |
| 186 | int is_last); |
| 187 | |
| 188 | /* Send a ping on a transport |
| 189 | |
| 190 | Calls cb with user data when a response is received. |
| 191 | cb *MAY* be called with arbitrary transport level locks held. It is not safe |
| 192 | to call into the transport during cb. */ |
| 193 | void grpc_transport_ping(grpc_transport *transport, void (*cb)(void *user_data), |
| 194 | void *user_data); |
| 195 | |
| 196 | /* Abort a stream |
| 197 | |
| 198 | Terminate reading and writing for a stream. A final recv_batch with no |
| 199 | operations and final_state == GRPC_STREAM_CLOSED will be received locally, |
| 200 | and no more data will be presented to the up-layer. |
| 201 | |
| 202 | TODO(ctiller): consider adding a HTTP/2 reason to this function. */ |
| 203 | void grpc_transport_abort_stream(grpc_transport *transport, grpc_stream *stream, |
| 204 | grpc_status_code status); |
| 205 | |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 206 | void grpc_transport_add_to_pollset(grpc_transport *transport, |
| 207 | grpc_pollset *pollset); |
| 208 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 209 | /* Advise peer of pending connection termination. */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 210 | void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status, |
| 211 | gpr_slice debug_data); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 212 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 213 | /* Close a transport. Aborts all open streams. */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 214 | void grpc_transport_close(grpc_transport *transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 215 | |
| 216 | /* Destroy the transport */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 217 | void grpc_transport_destroy(grpc_transport *transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 218 | |
| 219 | /* Return type for grpc_transport_setup_callback */ |
| 220 | typedef struct grpc_transport_setup_result { |
| 221 | void *user_data; |
| 222 | const grpc_transport_callbacks *callbacks; |
| 223 | } grpc_transport_setup_result; |
| 224 | |
| 225 | /* Given a transport, return callbacks for that transport. Used to finalize |
| 226 | setup as a transport is being created */ |
| 227 | typedef grpc_transport_setup_result (*grpc_transport_setup_callback)( |
| 228 | void *setup_arg, grpc_transport *transport, grpc_mdctx *mdctx); |
| 229 | |
| 230 | typedef struct grpc_transport_setup grpc_transport_setup; |
| 231 | typedef struct grpc_transport_setup_vtable grpc_transport_setup_vtable; |
| 232 | |
| 233 | struct grpc_transport_setup_vtable { |
| 234 | void (*initiate)(grpc_transport_setup *setup); |
| 235 | void (*cancel)(grpc_transport_setup *setup); |
| 236 | }; |
| 237 | |
| 238 | /* Transport setup is an asynchronous utility interface for client channels to |
| 239 | establish connections. It's transport agnostic. */ |
| 240 | struct grpc_transport_setup { |
| 241 | const grpc_transport_setup_vtable *vtable; |
| 242 | }; |
| 243 | |
| 244 | /* Initiate transport setup: e.g. for TCP+DNS trigger a resolve of the name |
| 245 | given at transport construction time, create the tcp connection, perform |
| 246 | handshakes, and call some grpc_transport_setup_result function provided at |
| 247 | setup construction time. |
| 248 | This *may* be implemented as a no-op if the setup process monitors something |
| 249 | continuously. */ |
| 250 | void grpc_transport_setup_initiate(grpc_transport_setup *setup); |
| 251 | /* Cancel transport setup. After this returns, no new transports should be |
| 252 | created, and all pending transport setup callbacks should be completed. |
| 253 | After this call completes, setup should be considered invalid (this can be |
| 254 | used as a destruction call by setup). */ |
| 255 | void grpc_transport_setup_cancel(grpc_transport_setup *setup); |
| 256 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 257 | #endif /* __GRPC_INTERNAL_TRANSPORT_TRANSPORT_H__ */ |