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