blob: 1872947208adc0b75968d117b285635677bb645d [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
119 /* The transport has been closed */
120 void (*closed)(void *user_data, grpc_transport *transport);
121};
122
123/* Returns the amount of memory required to store a grpc_stream for this
124 transport */
125size_t grpc_transport_stream_size(grpc_transport *transport);
126
127/* Initialize transport data for a stream.
128
129 Returns 0 on success, any other (transport-defined) value for failure.
130
131 Arguments:
132 transport - the transport on which to create this stream
133 stream - a pointer to uninitialized memory to initialize
134 server_data - either NULL for a client initiated stream, or a pointer
135 supplied from the accept_stream callback function */
136int grpc_transport_init_stream(grpc_transport *transport, grpc_stream *stream,
137 const void *server_data);
138
139/* Destroy transport data for a stream.
140
141 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
142 received by the up-layer. Must not be called in the same call stack as
143 recv_frame.
144
145 Arguments:
146 transport - the transport on which to create this stream
147 stream - the grpc_stream to destroy (memory is still owned by the
148 caller, but any child memory must be cleaned up) */
149void grpc_transport_destroy_stream(grpc_transport *transport,
150 grpc_stream *stream);
151
152/* Enable/disable incoming data for a stream.
153
154 This effectively disables new window becoming available for a given stream,
155 but does not prevent existing window from being consumed by a sender: the
156 caller must still be prepared to receive some additional data after this
157 call.
158
159 Arguments:
160 transport - the transport on which to create this stream
161 stream - the grpc_stream to destroy (memory is still owned by the
162 caller, but any child memory must be cleaned up)
163 allow - is it allowed that new window be opened up? */
164void grpc_transport_set_allow_window_updates(grpc_transport *transport,
165 grpc_stream *stream, int allow);
166
167/* Send a batch of operations on a transport
168
169 Takes ownership of any objects contained in ops.
170
171 Arguments:
172 transport - the transport on which to initiate the stream
173 stream - the stream on which to send the operations. This must be
174 non-NULL and previously initialized by the same transport.
175 ops - an array of operations to apply to the stream - can be NULL
176 if ops_count == 0.
177 ops_count - the number of elements in ops
178 is_last - is this the last batch of operations to be sent out */
179void grpc_transport_send_batch(grpc_transport *transport, grpc_stream *stream,
180 grpc_stream_op *ops, size_t ops_count,
181 int is_last);
182
183/* Send a ping on a transport
184
185 Calls cb with user data when a response is received.
186 cb *MAY* be called with arbitrary transport level locks held. It is not safe
187 to call into the transport during cb. */
188void grpc_transport_ping(grpc_transport *transport, void (*cb)(void *user_data),
189 void *user_data);
190
191/* Abort a stream
192
193 Terminate reading and writing for a stream. A final recv_batch with no
194 operations and final_state == GRPC_STREAM_CLOSED will be received locally,
195 and no more data will be presented to the up-layer.
196
197 TODO(ctiller): consider adding a HTTP/2 reason to this function. */
198void grpc_transport_abort_stream(grpc_transport *transport, grpc_stream *stream,
199 grpc_status_code status);
200
201/* Close a transport. Aborts all open streams. */
202void grpc_transport_close(struct grpc_transport *transport);
203
204/* Destroy the transport */
205void grpc_transport_destroy(struct grpc_transport *transport);
206
207/* Return type for grpc_transport_setup_callback */
208typedef struct grpc_transport_setup_result {
209 void *user_data;
210 const grpc_transport_callbacks *callbacks;
211} grpc_transport_setup_result;
212
213/* Given a transport, return callbacks for that transport. Used to finalize
214 setup as a transport is being created */
215typedef grpc_transport_setup_result (*grpc_transport_setup_callback)(
216 void *setup_arg, grpc_transport *transport, grpc_mdctx *mdctx);
217
218typedef struct grpc_transport_setup grpc_transport_setup;
219typedef struct grpc_transport_setup_vtable grpc_transport_setup_vtable;
220
221struct grpc_transport_setup_vtable {
222 void (*initiate)(grpc_transport_setup *setup);
223 void (*cancel)(grpc_transport_setup *setup);
224};
225
226/* Transport setup is an asynchronous utility interface for client channels to
227 establish connections. It's transport agnostic. */
228struct grpc_transport_setup {
229 const grpc_transport_setup_vtable *vtable;
230};
231
232/* Initiate transport setup: e.g. for TCP+DNS trigger a resolve of the name
233 given at transport construction time, create the tcp connection, perform
234 handshakes, and call some grpc_transport_setup_result function provided at
235 setup construction time.
236 This *may* be implemented as a no-op if the setup process monitors something
237 continuously. */
238void grpc_transport_setup_initiate(grpc_transport_setup *setup);
239/* Cancel transport setup. After this returns, no new transports should be
240 created, and all pending transport setup callbacks should be completed.
241 After this call completes, setup should be considered invalid (this can be
242 used as a destruction call by setup). */
243void grpc_transport_setup_cancel(grpc_transport_setup *setup);
244
245#endif /* __GRPC_INTERNAL_TRANSPORT_TRANSPORT_H__ */