blob: 6dd0fdaf4687d8ca0fde74b749bb6782ec68adf5 [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"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include "src/core/transport/stream_op.h"
41
42/* forward declarations */
43typedef struct grpc_transport grpc_transport;
44typedef 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. */
49typedef struct grpc_stream grpc_stream;
50
51/* Represents the send/recv closed state of a stream. */
52typedef 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. */
64struct grpc_transport_callbacks {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065 /* Initialize a new stream on behalf of the transport.
66 Must result in a call to
67 grpc_transport_init_stream(transport, ..., request) in the same call
68 stack.
69 Must not result in any other calls to the transport.
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 request - request parameters for this stream (owned by the caller)
75 server_data - opaque transport dependent argument that should be passed
76 to grpc_transport_init_stream
77 */
78 void (*accept_stream)(void *user_data, grpc_transport *transport,
79 const void *server_data);
80
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 /* The transport has been closed */
82 void (*closed)(void *user_data, grpc_transport *transport);
83};
84
85/* Returns the amount of memory required to store a grpc_stream for this
86 transport */
87size_t grpc_transport_stream_size(grpc_transport *transport);
88
89/* Initialize transport data for a stream.
90
91 Returns 0 on success, any other (transport-defined) value for failure.
92
93 Arguments:
94 transport - the transport on which to create this stream
95 stream - a pointer to uninitialized memory to initialize
96 server_data - either NULL for a client initiated stream, or a pointer
97 supplied from the accept_stream callback function */
98int grpc_transport_init_stream(grpc_transport *transport, grpc_stream *stream,
99 const void *server_data);
100
101/* Destroy transport data for a stream.
102
103 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
104 received by the up-layer. Must not be called in the same call stack as
105 recv_frame.
106
107 Arguments:
108 transport - the transport on which to create this stream
109 stream - the grpc_stream to destroy (memory is still owned by the
110 caller, but any child memory must be cleaned up) */
111void grpc_transport_destroy_stream(grpc_transport *transport,
112 grpc_stream *stream);
113
114/* Enable/disable incoming data for a stream.
115
116 This effectively disables new window becoming available for a given stream,
117 but does not prevent existing window from being consumed by a sender: the
118 caller must still be prepared to receive some additional data after this
119 call.
120
121 Arguments:
122 transport - the transport on which to create this stream
123 stream - the grpc_stream to destroy (memory is still owned by the
124 caller, but any child memory must be cleaned up)
125 allow - is it allowed that new window be opened up? */
126void grpc_transport_set_allow_window_updates(grpc_transport *transport,
127 grpc_stream *stream, int allow);
128
Craig Tiller65582322015-04-21 09:24:41 -0700129/* Transport op: a set of operations to perform on a transport */
130typedef struct grpc_transport_op {
131 grpc_stream_op_buffer *send_ops;
132 int is_last_send;
133 void (*on_done_send)(void *user_data, int success);
134 void *send_user_data;
135
136 grpc_stream_op_buffer *recv_ops;
137 grpc_stream_state *recv_state;
138 void (*on_done_recv)(void *user_data, int success);
139 void *recv_user_data;
140
141 grpc_pollset *bind_pollset;
142} grpc_transport_op;
143
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144/* Send a batch of operations on a transport
145
146 Takes ownership of any objects contained in ops.
147
148 Arguments:
149 transport - the transport on which to initiate the stream
150 stream - the stream on which to send the operations. This must be
151 non-NULL and previously initialized by the same transport.
Craig Tiller65582322015-04-21 09:24:41 -0700152 op - a grpc_transport_op specifying the op to perform */
153void grpc_transport_perform_op(grpc_transport *transport, grpc_stream *stream,
154 grpc_transport_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800155
156/* Send a ping on a transport
157
158 Calls cb with user data when a response is received.
159 cb *MAY* be called with arbitrary transport level locks held. It is not safe
160 to call into the transport during cb. */
161void grpc_transport_ping(grpc_transport *transport, void (*cb)(void *user_data),
162 void *user_data);
163
164/* Abort a stream
165
166 Terminate reading and writing for a stream. A final recv_batch with no
167 operations and final_state == GRPC_STREAM_CLOSED will be received locally,
168 and no more data will be presented to the up-layer.
169
170 TODO(ctiller): consider adding a HTTP/2 reason to this function. */
171void grpc_transport_abort_stream(grpc_transport *transport, grpc_stream *stream,
172 grpc_status_code status);
173
ctillerd79b4862014-12-17 16:36:59 -0800174void grpc_transport_add_to_pollset(grpc_transport *transport,
175 grpc_pollset *pollset);
176
nnoble0c475f02014-12-05 15:37:39 -0800177/* Advise peer of pending connection termination. */
ctillerd79b4862014-12-17 16:36:59 -0800178void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status,
179 gpr_slice debug_data);
nnoble0c475f02014-12-05 15:37:39 -0800180
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181/* Close a transport. Aborts all open streams. */
ctillerd79b4862014-12-17 16:36:59 -0800182void grpc_transport_close(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800183
184/* Destroy the transport */
ctillerd79b4862014-12-17 16:36:59 -0800185void grpc_transport_destroy(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800186
187/* Return type for grpc_transport_setup_callback */
188typedef struct grpc_transport_setup_result {
189 void *user_data;
190 const grpc_transport_callbacks *callbacks;
191} grpc_transport_setup_result;
192
193/* Given a transport, return callbacks for that transport. Used to finalize
194 setup as a transport is being created */
195typedef grpc_transport_setup_result (*grpc_transport_setup_callback)(
196 void *setup_arg, grpc_transport *transport, grpc_mdctx *mdctx);
197
198typedef struct grpc_transport_setup grpc_transport_setup;
199typedef struct grpc_transport_setup_vtable grpc_transport_setup_vtable;
200
201struct grpc_transport_setup_vtable {
202 void (*initiate)(grpc_transport_setup *setup);
203 void (*cancel)(grpc_transport_setup *setup);
204};
205
206/* Transport setup is an asynchronous utility interface for client channels to
207 establish connections. It's transport agnostic. */
208struct grpc_transport_setup {
209 const grpc_transport_setup_vtable *vtable;
210};
211
212/* Initiate transport setup: e.g. for TCP+DNS trigger a resolve of the name
213 given at transport construction time, create the tcp connection, perform
214 handshakes, and call some grpc_transport_setup_result function provided at
215 setup construction time.
216 This *may* be implemented as a no-op if the setup process monitors something
217 continuously. */
218void grpc_transport_setup_initiate(grpc_transport_setup *setup);
219/* Cancel transport setup. After this returns, no new transports should be
220 created, and all pending transport setup callbacks should be completed.
221 After this call completes, setup should be considered invalid (this can be
222 used as a destruction call by setup). */
223void grpc_transport_setup_cancel(grpc_transport_setup *setup);
224
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100225#endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */