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" |
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 | |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 63 | /* Transport op: a set of operations to perform on a transport */ |
| 64 | typedef struct grpc_transport_op { |
| 65 | grpc_stream_op_buffer *send_ops; |
| 66 | int is_last_send; |
| 67 | void (*on_done_send)(void *user_data, int success); |
| 68 | void *send_user_data; |
| 69 | |
| 70 | grpc_stream_op_buffer *recv_ops; |
| 71 | grpc_stream_state *recv_state; |
| 72 | void (*on_done_recv)(void *user_data, int success); |
| 73 | void *recv_user_data; |
| 74 | |
| 75 | grpc_pollset *bind_pollset; |
| 76 | |
| 77 | grpc_status_code cancel_with_status; |
Craig Tiller | 2ea37fd | 2015-04-24 13:03:49 -0700 | [diff] [blame] | 78 | grpc_mdstr *cancel_message; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 79 | |
| 80 | /* Indexes correspond to grpc_context_index enum values */ |
| 81 | void *const *context; |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 82 | } grpc_transport_op; |
| 83 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | /* Callbacks made from the transport to the upper layers of grpc. */ |
| 85 | struct grpc_transport_callbacks { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | /* Initialize a new stream on behalf of the transport. |
| 87 | Must result in a call to |
| 88 | grpc_transport_init_stream(transport, ..., request) in the same call |
| 89 | stack. |
| 90 | Must not result in any other calls to the transport. |
| 91 | |
| 92 | Arguments: |
| 93 | user_data - the transport user data set at transport creation time |
| 94 | transport - the grpc_transport instance making this call |
| 95 | request - request parameters for this stream (owned by the caller) |
| 96 | server_data - opaque transport dependent argument that should be passed |
| 97 | to grpc_transport_init_stream |
| 98 | */ |
| 99 | void (*accept_stream)(void *user_data, grpc_transport *transport, |
| 100 | const void *server_data); |
| 101 | |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 102 | void (*goaway)(void *user_data, grpc_transport *transport, |
| 103 | grpc_status_code status, gpr_slice debug); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 104 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | /* The transport has been closed */ |
| 106 | void (*closed)(void *user_data, grpc_transport *transport); |
| 107 | }; |
| 108 | |
| 109 | /* Returns the amount of memory required to store a grpc_stream for this |
| 110 | transport */ |
| 111 | size_t grpc_transport_stream_size(grpc_transport *transport); |
| 112 | |
| 113 | /* Initialize transport data for a stream. |
| 114 | |
| 115 | Returns 0 on success, any other (transport-defined) value for failure. |
| 116 | |
| 117 | Arguments: |
| 118 | transport - the transport on which to create this stream |
| 119 | stream - a pointer to uninitialized memory to initialize |
| 120 | server_data - either NULL for a client initiated stream, or a pointer |
| 121 | supplied from the accept_stream callback function */ |
| 122 | int grpc_transport_init_stream(grpc_transport *transport, grpc_stream *stream, |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 123 | const void *server_data, |
| 124 | grpc_transport_op *initial_op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 125 | |
| 126 | /* Destroy transport data for a stream. |
| 127 | |
| 128 | Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been |
| 129 | received by the up-layer. Must not be called in the same call stack as |
| 130 | recv_frame. |
| 131 | |
| 132 | Arguments: |
| 133 | transport - the transport on which to create this stream |
| 134 | stream - the grpc_stream to destroy (memory is still owned by the |
| 135 | caller, but any child memory must be cleaned up) */ |
| 136 | void grpc_transport_destroy_stream(grpc_transport *transport, |
| 137 | grpc_stream *stream); |
| 138 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 139 | void grpc_transport_op_finish_with_failure(grpc_transport_op *op); |
| 140 | |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 141 | void grpc_transport_op_add_cancellation(grpc_transport_op *op, |
| 142 | grpc_status_code status, |
| 143 | grpc_mdstr *message); |
Craig Tiller | 2ea37fd | 2015-04-24 13:03:49 -0700 | [diff] [blame] | 144 | |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 145 | /* TODO(ctiller): remove this */ |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 146 | void grpc_transport_add_to_pollset(grpc_transport *transport, |
| 147 | grpc_pollset *pollset); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 148 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 149 | char *grpc_transport_op_string(grpc_transport_op *op); |
| 150 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | /* Send a batch of operations on a transport |
| 152 | |
| 153 | Takes ownership of any objects contained in ops. |
| 154 | |
| 155 | Arguments: |
| 156 | transport - the transport on which to initiate the stream |
| 157 | stream - the stream on which to send the operations. This must be |
| 158 | non-NULL and previously initialized by the same transport. |
Craig Tiller | 6558232 | 2015-04-21 09:24:41 -0700 | [diff] [blame] | 159 | op - a grpc_transport_op specifying the op to perform */ |
| 160 | void grpc_transport_perform_op(grpc_transport *transport, grpc_stream *stream, |
| 161 | grpc_transport_op *op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 162 | |
| 163 | /* Send a ping on a transport |
| 164 | |
| 165 | Calls cb with user data when a response is received. |
| 166 | cb *MAY* be called with arbitrary transport level locks held. It is not safe |
| 167 | to call into the transport during cb. */ |
| 168 | void grpc_transport_ping(grpc_transport *transport, void (*cb)(void *user_data), |
| 169 | void *user_data); |
| 170 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 171 | /* Advise peer of pending connection termination. */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 172 | void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status, |
| 173 | gpr_slice debug_data); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 174 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 175 | /* Close a transport. Aborts all open streams. */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 176 | void grpc_transport_close(grpc_transport *transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 177 | |
| 178 | /* Destroy the transport */ |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 179 | void grpc_transport_destroy(grpc_transport *transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 180 | |
| 181 | /* Return type for grpc_transport_setup_callback */ |
| 182 | typedef struct grpc_transport_setup_result { |
| 183 | void *user_data; |
| 184 | const grpc_transport_callbacks *callbacks; |
| 185 | } grpc_transport_setup_result; |
| 186 | |
| 187 | /* Given a transport, return callbacks for that transport. Used to finalize |
| 188 | setup as a transport is being created */ |
| 189 | typedef grpc_transport_setup_result (*grpc_transport_setup_callback)( |
| 190 | void *setup_arg, grpc_transport *transport, grpc_mdctx *mdctx); |
| 191 | |
| 192 | typedef struct grpc_transport_setup grpc_transport_setup; |
| 193 | typedef struct grpc_transport_setup_vtable grpc_transport_setup_vtable; |
| 194 | |
| 195 | struct grpc_transport_setup_vtable { |
| 196 | void (*initiate)(grpc_transport_setup *setup); |
| 197 | void (*cancel)(grpc_transport_setup *setup); |
| 198 | }; |
| 199 | |
| 200 | /* Transport setup is an asynchronous utility interface for client channels to |
| 201 | establish connections. It's transport agnostic. */ |
| 202 | struct grpc_transport_setup { |
| 203 | const grpc_transport_setup_vtable *vtable; |
| 204 | }; |
| 205 | |
| 206 | /* Initiate transport setup: e.g. for TCP+DNS trigger a resolve of the name |
| 207 | given at transport construction time, create the tcp connection, perform |
| 208 | handshakes, and call some grpc_transport_setup_result function provided at |
| 209 | setup construction time. |
| 210 | This *may* be implemented as a no-op if the setup process monitors something |
| 211 | continuously. */ |
| 212 | void grpc_transport_setup_initiate(grpc_transport_setup *setup); |
| 213 | /* Cancel transport setup. After this returns, no new transports should be |
| 214 | created, and all pending transport setup callbacks should be completed. |
| 215 | After this call completes, setup should be considered invalid (this can be |
| 216 | used as a destruction call by setup). */ |
| 217 | void grpc_transport_setup_cancel(grpc_transport_setup *setup); |
| 218 | |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 219 | #endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */ |