blob: 1429737721c85af08a3e8399851cdaebc7cc4a78 [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"
Craig Tiller928cd772015-05-08 09:52:54 -070040#include "src/core/iomgr/pollset_set.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include "src/core/transport/stream_op.h"
Julien Boeufc6f8d0a2015-05-11 22:40:02 -070042#include "src/core/channel/context.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
44/* forward declarations */
45typedef struct grpc_transport grpc_transport;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
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 Tiller3f475422015-06-25 10:43:05 -070064/* Transport stream op: a set of operations to perform on a transport
65 against a single stream */
Craig Tillerb7959a02015-06-25 08:50:54 -070066typedef struct grpc_transport_stream_op {
Craig Tiller1e6facb2015-06-11 22:47:11 -070067 grpc_iomgr_closure *on_consumed;
Craig Tiller5dde66e2015-06-02 09:05:23 -070068
Craig Tiller3f2c2212015-04-23 07:56:33 -070069 grpc_stream_op_buffer *send_ops;
70 int is_last_send;
Craig Tiller1e6facb2015-06-11 22:47:11 -070071 grpc_iomgr_closure *on_done_send;
Craig Tiller3f2c2212015-04-23 07:56:33 -070072
73 grpc_stream_op_buffer *recv_ops;
74 grpc_stream_state *recv_state;
Craig Tiller1e6facb2015-06-11 22:47:11 -070075 grpc_iomgr_closure *on_done_recv;
Craig Tiller3f2c2212015-04-23 07:56:33 -070076
77 grpc_pollset *bind_pollset;
78
79 grpc_status_code cancel_with_status;
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 Tillerb7959a02015-06-25 08:50:54 -070083} grpc_transport_stream_op;
Craig Tiller3f2c2212015-04-23 07:56:33 -070084
Craig Tiller3f475422015-06-25 10:43:05 -070085/** Transport op: a set of operations to perform on a transport as a whole */
86typedef struct grpc_transport_op {
Craig Tillere039f032015-06-25 12:54:23 -070087 /** called when processing of this op is done */
88 grpc_iomgr_closure *on_consumed;
Craig Tiller3f475422015-06-25 10:43:05 -070089 /** connectivity monitoring */
90 grpc_iomgr_closure *on_connectivity_state_change;
91 grpc_connectivity_state *connectivity_state;
92 /** should the transport be disconnected */
93 int disconnect;
Craig Tiller9188d7a2015-07-05 12:44:37 -070094 /** should we send a goaway?
95 after a goaway is sent, once there are no more active calls on
96 the transport, the transport should disconnect */
Craig Tiller3f475422015-06-25 10:43:05 -070097 int send_goaway;
98 /** what should the goaway contain? */
99 grpc_status_code goaway_status;
100 gpr_slice *goaway_message;
101 /** set the callback for accepting new streams;
102 this is a permanent callback, unlike the other one-shot closures */
Craig Tiller079a11b2015-06-30 10:07:15 -0700103 void (*set_accept_stream)(void *user_data, grpc_transport *transport,
104 const void *server_data);
Craig Tiller3f475422015-06-25 10:43:05 -0700105 void *set_accept_stream_user_data;
106 /** add this transport to a pollset */
107 grpc_pollset *bind_pollset;
108 /** send a ping, call this back if not NULL */
109 grpc_iomgr_closure *send_ping;
110} grpc_transport_op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111
112/* Returns the amount of memory required to store a grpc_stream for this
113 transport */
114size_t grpc_transport_stream_size(grpc_transport *transport);
115
116/* Initialize transport data for a stream.
117
118 Returns 0 on success, any other (transport-defined) value for failure.
119
120 Arguments:
121 transport - the transport on which to create this stream
122 stream - a pointer to uninitialized memory to initialize
123 server_data - either NULL for a client initiated stream, or a pointer
124 supplied from the accept_stream callback function */
125int grpc_transport_init_stream(grpc_transport *transport, grpc_stream *stream,
Craig Tiller06aeea72015-04-23 10:54:45 -0700126 const void *server_data,
Craig Tillerb7959a02015-06-25 08:50:54 -0700127 grpc_transport_stream_op *initial_op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128
129/* Destroy transport data for a stream.
130
131 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
132 received by the up-layer. Must not be called in the same call stack as
133 recv_frame.
134
135 Arguments:
136 transport - the transport on which to create this stream
137 stream - the grpc_stream to destroy (memory is still owned by the
138 caller, but any child memory must be cleaned up) */
139void grpc_transport_destroy_stream(grpc_transport *transport,
140 grpc_stream *stream);
141
Craig Tillerb7959a02015-06-25 08:50:54 -0700142void grpc_transport_stream_op_finish_with_failure(grpc_transport_stream_op *op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700143
Craig Tillerb7959a02015-06-25 08:50:54 -0700144void grpc_transport_stream_op_add_cancellation(grpc_transport_stream_op *op,
145 grpc_status_code status,
146 grpc_mdstr *message);
Craig Tiller2ea37fd2015-04-24 13:03:49 -0700147
Craig Tillerb7959a02015-06-25 08:50:54 -0700148char *grpc_transport_stream_op_string(grpc_transport_stream_op *op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700149
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150/* Send a batch of operations on a transport
151
152 Takes ownership of any objects contained in ops.
153
154 Arguments:
155 transport - the transport on which to initiate the stream
156 stream - the stream on which to send the operations. This must be
157 non-NULL and previously initialized by the same transport.
Craig Tillerb7959a02015-06-25 08:50:54 -0700158 op - a grpc_transport_stream_op specifying the op to perform */
Craig Tiller079a11b2015-06-30 10:07:15 -0700159void grpc_transport_perform_stream_op(grpc_transport *transport,
160 grpc_stream *stream,
161 grpc_transport_stream_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800162
Craig Tiller079a11b2015-06-30 10:07:15 -0700163void grpc_transport_perform_op(grpc_transport *transport,
164 grpc_transport_op *op);
Craig Tiller3f475422015-06-25 10:43:05 -0700165
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166/* Send a ping on a transport
167
Craig Tiller4aa71a12015-06-15 13:00:55 -0700168 Calls cb with user data when a response is received. */
169void grpc_transport_ping(grpc_transport *transport, grpc_iomgr_closure *cb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170
nnoble0c475f02014-12-05 15:37:39 -0800171/* Advise peer of pending connection termination. */
ctillerd79b4862014-12-17 16:36:59 -0800172void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status,
173 gpr_slice debug_data);
nnoble0c475f02014-12-05 15:37:39 -0800174
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175/* Close a transport. Aborts all open streams. */
ctillerd79b4862014-12-17 16:36:59 -0800176void grpc_transport_close(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177
178/* Destroy the transport */
ctillerd79b4862014-12-17 16:36:59 -0800179void grpc_transport_destroy(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180
Craig Tiller06aeea72015-04-23 10:54:45 -0700181#endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */