blob: 6f8d39e352f040fff7b5adc413ba6d779d2e528b [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"
Julien Boeufc6f8d0a2015-05-11 22:40:02 -070041#include "src/core/channel/context.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042
43/* forward declarations */
44typedef struct grpc_transport grpc_transport;
45typedef struct grpc_transport_callbacks grpc_transport_callbacks;
46
47/* grpc_stream doesn't actually exist. It's used as a typesafe
48 opaque pointer for whatever data the transport wants to track
49 for a stream. */
50typedef struct grpc_stream grpc_stream;
51
52/* Represents the send/recv closed state of a stream. */
53typedef enum grpc_stream_state {
54 /* the stream is open for sends and receives */
55 GRPC_STREAM_OPEN,
56 /* the stream is closed for sends, but may still receive data */
57 GRPC_STREAM_SEND_CLOSED,
58 /* the stream is closed for receives, but may still send data */
59 GRPC_STREAM_RECV_CLOSED,
60 /* the stream is closed for both sends and receives */
61 GRPC_STREAM_CLOSED
62} grpc_stream_state;
63
Craig Tiller3f2c2212015-04-23 07:56:33 -070064/* Transport op: a set of operations to perform on a transport */
65typedef struct grpc_transport_op {
66 grpc_stream_op_buffer *send_ops;
67 int is_last_send;
68 void (*on_done_send)(void *user_data, int success);
69 void *send_user_data;
70
71 grpc_stream_op_buffer *recv_ops;
72 grpc_stream_state *recv_state;
73 void (*on_done_recv)(void *user_data, int success);
74 void *recv_user_data;
75
76 grpc_pollset *bind_pollset;
77
78 grpc_status_code cancel_with_status;
Craig Tiller2ea37fd2015-04-24 13:03:49 -070079 grpc_mdstr *cancel_message;
Craig Tiller935cf422015-05-01 14:10:46 -070080
81 /* Indexes correspond to grpc_context_index enum values */
Julien Boeuf83b02972015-05-20 22:50:34 -070082 grpc_call_context_element *context;
Craig Tiller3f2c2212015-04-23 07:56:33 -070083} grpc_transport_op;
84
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085/* Callbacks made from the transport to the upper layers of grpc. */
86struct grpc_transport_callbacks {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087 /* Initialize a new stream on behalf of the transport.
88 Must result in a call to
89 grpc_transport_init_stream(transport, ..., request) in the same call
90 stack.
91 Must not result in any other calls to the transport.
92
93 Arguments:
94 user_data - the transport user data set at transport creation time
95 transport - the grpc_transport instance making this call
96 request - request parameters for this stream (owned by the caller)
97 server_data - opaque transport dependent argument that should be passed
98 to grpc_transport_init_stream
99 */
100 void (*accept_stream)(void *user_data, grpc_transport *transport,
101 const void *server_data);
102
Craig Tiller06aeea72015-04-23 10:54:45 -0700103 void (*goaway)(void *user_data, grpc_transport *transport,
104 grpc_status_code status, gpr_slice debug);
Craig Tiller83f88d92015-04-21 16:02:05 -0700105
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 /* The transport has been closed */
107 void (*closed)(void *user_data, grpc_transport *transport);
108};
109
110/* Returns the amount of memory required to store a grpc_stream for this
111 transport */
112size_t grpc_transport_stream_size(grpc_transport *transport);
113
114/* Initialize transport data for a stream.
115
116 Returns 0 on success, any other (transport-defined) value for failure.
117
118 Arguments:
119 transport - the transport on which to create this stream
120 stream - a pointer to uninitialized memory to initialize
121 server_data - either NULL for a client initiated stream, or a pointer
122 supplied from the accept_stream callback function */
123int grpc_transport_init_stream(grpc_transport *transport, grpc_stream *stream,
Craig Tiller06aeea72015-04-23 10:54:45 -0700124 const void *server_data,
125 grpc_transport_op *initial_op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
127/* Destroy transport data for a stream.
128
129 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
130 received by the up-layer. Must not be called in the same call stack as
131 recv_frame.
132
133 Arguments:
134 transport - the transport on which to create this stream
135 stream - the grpc_stream to destroy (memory is still owned by the
136 caller, but any child memory must be cleaned up) */
137void grpc_transport_destroy_stream(grpc_transport *transport,
138 grpc_stream *stream);
139
Craig Tiller83f88d92015-04-21 16:02:05 -0700140void grpc_transport_op_finish_with_failure(grpc_transport_op *op);
141
Craig Tiller1a727fd2015-04-24 13:21:22 -0700142void grpc_transport_op_add_cancellation(grpc_transport_op *op,
143 grpc_status_code status,
144 grpc_mdstr *message);
Craig Tiller2ea37fd2015-04-24 13:03:49 -0700145
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700146/* TODO(ctiller): remove this */
Craig Tiller06aeea72015-04-23 10:54:45 -0700147void grpc_transport_add_to_pollset(grpc_transport *transport,
148 grpc_pollset *pollset);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700149
Craig Tiller83f88d92015-04-21 16:02:05 -0700150char *grpc_transport_op_string(grpc_transport_op *op);
151
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152/* Send a batch of operations on a transport
153
154 Takes ownership of any objects contained in ops.
155
156 Arguments:
157 transport - the transport on which to initiate the stream
158 stream - the stream on which to send the operations. This must be
159 non-NULL and previously initialized by the same transport.
Craig Tiller65582322015-04-21 09:24:41 -0700160 op - a grpc_transport_op specifying the op to perform */
161void grpc_transport_perform_op(grpc_transport *transport, grpc_stream *stream,
162 grpc_transport_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800163
164/* Send a ping on a transport
165
166 Calls cb with user data when a response is received.
167 cb *MAY* be called with arbitrary transport level locks held. It is not safe
168 to call into the transport during cb. */
169void grpc_transport_ping(grpc_transport *transport, void (*cb)(void *user_data),
170 void *user_data);
171
nnoble0c475f02014-12-05 15:37:39 -0800172/* Advise peer of pending connection termination. */
ctillerd79b4862014-12-17 16:36:59 -0800173void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status,
174 gpr_slice debug_data);
nnoble0c475f02014-12-05 15:37:39 -0800175
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176/* Close a transport. Aborts all open streams. */
ctillerd79b4862014-12-17 16:36:59 -0800177void grpc_transport_close(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178
179/* Destroy the transport */
ctillerd79b4862014-12-17 16:36:59 -0800180void grpc_transport_destroy(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181
182/* Return type for grpc_transport_setup_callback */
183typedef struct grpc_transport_setup_result {
184 void *user_data;
185 const grpc_transport_callbacks *callbacks;
186} grpc_transport_setup_result;
187
188/* Given a transport, return callbacks for that transport. Used to finalize
189 setup as a transport is being created */
190typedef grpc_transport_setup_result (*grpc_transport_setup_callback)(
191 void *setup_arg, grpc_transport *transport, grpc_mdctx *mdctx);
192
193typedef struct grpc_transport_setup grpc_transport_setup;
194typedef struct grpc_transport_setup_vtable grpc_transport_setup_vtable;
195
196struct grpc_transport_setup_vtable {
197 void (*initiate)(grpc_transport_setup *setup);
198 void (*cancel)(grpc_transport_setup *setup);
199};
200
201/* Transport setup is an asynchronous utility interface for client channels to
202 establish connections. It's transport agnostic. */
203struct grpc_transport_setup {
204 const grpc_transport_setup_vtable *vtable;
205};
206
207/* Initiate transport setup: e.g. for TCP+DNS trigger a resolve of the name
208 given at transport construction time, create the tcp connection, perform
209 handshakes, and call some grpc_transport_setup_result function provided at
210 setup construction time.
211 This *may* be implemented as a no-op if the setup process monitors something
212 continuously. */
213void grpc_transport_setup_initiate(grpc_transport_setup *setup);
214/* Cancel transport setup. After this returns, no new transports should be
215 created, and all pending transport setup callbacks should be completed.
216 After this call completes, setup should be considered invalid (this can be
217 used as a destruction call by setup). */
218void grpc_transport_setup_cancel(grpc_transport_setup *setup);
219
Craig Tiller06aeea72015-04-23 10:54:45 -0700220#endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */