Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_GRPC_H |
| 35 | #define GRPC_GRPC_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #include <grpc/status.h> |
| 38 | |
| 39 | #include <stddef.h> |
| 40 | #include <grpc/support/slice.h> |
| 41 | #include <grpc/support/time.h> |
| 42 | |
| 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
Nathaniel Manista | 6d41a05 | 2015-02-16 02:12:48 +0000 | [diff] [blame] | 47 | /* Completion Queues enable notification of the completion of asynchronous |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | actions. */ |
| 49 | typedef struct grpc_completion_queue grpc_completion_queue; |
| 50 | |
| 51 | /* The Channel interface allows creation of Call objects. */ |
| 52 | typedef struct grpc_channel grpc_channel; |
| 53 | |
| 54 | /* A server listens to some port and responds to request calls */ |
| 55 | typedef struct grpc_server grpc_server; |
| 56 | |
| 57 | /* A Call represents an RPC. When created, it is in a configuration state |
| 58 | allowing properties to be set until it is invoked. After invoke, the Call |
| 59 | can have messages written to it and read from it. */ |
| 60 | typedef struct grpc_call grpc_call; |
| 61 | |
| 62 | /* Type specifier for grpc_arg */ |
| 63 | typedef enum { |
| 64 | GRPC_ARG_STRING, |
| 65 | GRPC_ARG_INTEGER, |
| 66 | GRPC_ARG_POINTER |
| 67 | } grpc_arg_type; |
| 68 | |
| 69 | /* A single argument... each argument has a key and a value |
| 70 | |
| 71 | A note on naming keys: |
| 72 | Keys are namespaced into groups, usually grouped by library, and are |
| 73 | keys for module XYZ are named XYZ.key1, XYZ.key2, etc. Module names must |
| 74 | be restricted to the regex [A-Za-z][_A-Za-z0-9]{,15}. |
| 75 | Key names must be restricted to the regex [A-Za-z][_A-Za-z0-9]{,47}. |
| 76 | |
| 77 | GRPC core library keys are prefixed by grpc. |
| 78 | |
| 79 | Library authors are strongly encouraged to #define symbolic constants for |
| 80 | their keys so that it's possible to change them in the future. */ |
| 81 | typedef struct { |
| 82 | grpc_arg_type type; |
| 83 | char *key; |
| 84 | union { |
| 85 | char *string; |
| 86 | int integer; |
| 87 | struct { |
| 88 | void *p; |
| 89 | void *(*copy)(void *p); |
| 90 | void (*destroy)(void *p); |
| 91 | } pointer; |
| 92 | } value; |
| 93 | } grpc_arg; |
| 94 | |
Craig Tiller | 29f2b21 | 2015-02-17 17:01:24 -0800 | [diff] [blame] | 95 | /* An array of arguments that can be passed around. |
| 96 | Used to set optional channel-level configuration. |
| 97 | These configuration options are modelled as key-value pairs as defined |
| 98 | by grpc_arg; keys are strings to allow easy backwards-compatible extension |
| 99 | by arbitrary parties. |
| 100 | All evaluation is performed at channel creation time. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | typedef struct { |
| 102 | size_t num_args; |
| 103 | grpc_arg *args; |
| 104 | } grpc_channel_args; |
| 105 | |
| 106 | /* Channel argument keys: */ |
| 107 | /* Enable census for tracing and stats collection */ |
| 108 | #define GRPC_ARG_ENABLE_CENSUS "grpc.census" |
| 109 | /* Maximum number of concurrent incoming streams to allow on a http2 |
| 110 | connection */ |
| 111 | #define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams" |
| 112 | /* Maximum message length that the channel can receive */ |
| 113 | #define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length" |
Craig Tiller | 8802558 | 2015-05-04 09:41:10 -0700 | [diff] [blame] | 114 | /* Initial sequence number for http2 transports */ |
| 115 | #define GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER \ |
| 116 | "grpc.http2.initial_sequence_number" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | /* Result of a grpc call. If the caller satisfies the prerequisites of a |
| 119 | particular operation, the grpc_call_error returned will be GRPC_CALL_OK. |
| 120 | Receiving any other value listed here is an indication of a bug in the |
| 121 | caller. */ |
| 122 | typedef enum grpc_call_error { |
| 123 | /* everything went ok */ |
| 124 | GRPC_CALL_OK = 0, |
| 125 | /* something failed, we don't know what */ |
| 126 | GRPC_CALL_ERROR, |
| 127 | /* this method is not available on the server */ |
| 128 | GRPC_CALL_ERROR_NOT_ON_SERVER, |
| 129 | /* this method is not available on the client */ |
| 130 | GRPC_CALL_ERROR_NOT_ON_CLIENT, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 131 | /* this method must be called before server_accept */ |
| 132 | GRPC_CALL_ERROR_ALREADY_ACCEPTED, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 133 | /* this method must be called before invoke */ |
| 134 | GRPC_CALL_ERROR_ALREADY_INVOKED, |
| 135 | /* this method must be called after invoke */ |
| 136 | GRPC_CALL_ERROR_NOT_INVOKED, |
| 137 | /* this call is already finished |
| 138 | (writes_done or write_status has already been called) */ |
| 139 | GRPC_CALL_ERROR_ALREADY_FINISHED, |
| 140 | /* there is already an outstanding read/write operation on the call */ |
| 141 | GRPC_CALL_ERROR_TOO_MANY_OPERATIONS, |
| 142 | /* the flags value was illegal for this call */ |
Craig Tiller | b96d001 | 2015-05-06 15:33:23 -0700 | [diff] [blame] | 143 | GRPC_CALL_ERROR_INVALID_FLAGS, |
| 144 | /* invalid metadata was passed to this call */ |
| 145 | GRPC_CALL_ERROR_INVALID_METADATA |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 146 | } grpc_call_error; |
| 147 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 148 | /* Write Flags: */ |
| 149 | /* Hint that the write may be buffered and need not go out on the wire |
| 150 | immediately. GRPC is free to buffer the message until the next non-buffered |
| 151 | write, or until writes_done, but it need not buffer completely or at all. */ |
| 152 | #define GRPC_WRITE_BUFFER_HINT (0x00000001u) |
| 153 | /* Force compression to be disabled for a particular write |
| 154 | (start_write/add_metadata). Illegal on invoke/accept. */ |
| 155 | #define GRPC_WRITE_NO_COMPRESS (0x00000002u) |
| 156 | |
| 157 | /* A buffer of bytes */ |
| 158 | struct grpc_byte_buffer; |
| 159 | typedef struct grpc_byte_buffer grpc_byte_buffer; |
| 160 | |
Nathaniel Manista | 6d41a05 | 2015-02-16 02:12:48 +0000 | [diff] [blame] | 161 | /* Sample helpers to obtain byte buffers (these will certainly move |
| 162 | someplace else) */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 163 | grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices); |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 164 | grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 165 | size_t grpc_byte_buffer_length(grpc_byte_buffer *bb); |
| 166 | void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer); |
| 167 | |
| 168 | /* Reader for byte buffers. Iterates over slices in the byte buffer */ |
| 169 | struct grpc_byte_buffer_reader; |
| 170 | typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader; |
| 171 | |
| 172 | grpc_byte_buffer_reader *grpc_byte_buffer_reader_create( |
| 173 | grpc_byte_buffer *buffer); |
| 174 | /* At the end of the stream, returns 0. Otherwise, returns 1 and sets slice to |
| 175 | be the returned slice. Caller is responsible for calling gpr_slice_unref on |
| 176 | the result. */ |
| 177 | int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader, |
| 178 | gpr_slice *slice); |
| 179 | void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader); |
| 180 | |
| 181 | /* A single metadata element */ |
| 182 | typedef struct grpc_metadata { |
Craig Tiller | 4f97273 | 2015-02-05 12:40:20 -0800 | [diff] [blame] | 183 | const char *key; |
| 184 | const char *value; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | size_t value_length; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 186 | |
| 187 | /* The following fields are reserved for grpc internal use. |
| 188 | There is no need to initialize them, and they will be set to garbage during |
| 189 | calls to grpc. */ |
| 190 | struct { |
Craig Tiller | 9c9d4e0 | 2015-04-20 09:03:29 -0700 | [diff] [blame] | 191 | void *obfuscated[3]; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 192 | } internal_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 193 | } grpc_metadata; |
| 194 | |
| 195 | typedef enum grpc_completion_type { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 196 | GRPC_QUEUE_SHUTDOWN, /* Shutting down */ |
| 197 | GRPC_QUEUE_TIMEOUT, /* No event before timeout */ |
| 198 | GRPC_OP_COMPLETE /* operation completion */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | } grpc_completion_type; |
| 200 | |
| 201 | typedef struct grpc_event { |
| 202 | grpc_completion_type type; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 203 | int success; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 204 | void *tag; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 205 | } grpc_event; |
| 206 | |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 207 | typedef struct { |
| 208 | size_t count; |
| 209 | size_t capacity; |
| 210 | grpc_metadata *metadata; |
| 211 | } grpc_metadata_array; |
| 212 | |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 213 | void grpc_metadata_array_init(grpc_metadata_array *array); |
| 214 | void grpc_metadata_array_destroy(grpc_metadata_array *array); |
| 215 | |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 216 | typedef struct { |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 217 | char *method; |
Craig Tiller | 034929c | 2015-02-02 16:56:15 -0800 | [diff] [blame] | 218 | size_t method_capacity; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 219 | char *host; |
Craig Tiller | 034929c | 2015-02-02 16:56:15 -0800 | [diff] [blame] | 220 | size_t host_capacity; |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 221 | gpr_timespec deadline; |
| 222 | } grpc_call_details; |
| 223 | |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 224 | void grpc_call_details_init(grpc_call_details *details); |
| 225 | void grpc_call_details_destroy(grpc_call_details *details); |
| 226 | |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 227 | typedef enum { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 228 | /* Send initial metadata: one and only one instance MUST be sent for each |
| 229 | call, |
Craig Tiller | b7800c1 | 2015-02-04 18:25:38 -0800 | [diff] [blame] | 230 | unless the call was cancelled - in which case this can be skipped */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 231 | GRPC_OP_SEND_INITIAL_METADATA = 0, |
Craig Tiller | b7800c1 | 2015-02-04 18:25:38 -0800 | [diff] [blame] | 232 | /* Send a message: 0 or more of these operations can occur for each call */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 233 | GRPC_OP_SEND_MESSAGE, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 234 | /* Send a close from the server: one and only one instance MUST be sent from |
| 235 | the client, |
Craig Tiller | b7800c1 | 2015-02-04 18:25:38 -0800 | [diff] [blame] | 236 | unless the call was cancelled - in which case this can be skipped */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 237 | GRPC_OP_SEND_CLOSE_FROM_CLIENT, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 238 | /* Send status from the server: one and only one instance MUST be sent from |
| 239 | the server |
Craig Tiller | b7800c1 | 2015-02-04 18:25:38 -0800 | [diff] [blame] | 240 | unless the call was cancelled - in which case this can be skipped */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 241 | GRPC_OP_SEND_STATUS_FROM_SERVER, |
Craig Tiller | b7800c1 | 2015-02-04 18:25:38 -0800 | [diff] [blame] | 242 | /* Receive initial metadata: one and only one MUST be made on the client, must |
| 243 | not be made on the server */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 244 | GRPC_OP_RECV_INITIAL_METADATA, |
Craig Tiller | b7800c1 | 2015-02-04 18:25:38 -0800 | [diff] [blame] | 245 | /* Receive a message: 0 or more of these operations can occur for each call */ |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 246 | GRPC_OP_RECV_MESSAGE, |
Craig Tiller | 69eed29 | 2015-05-19 10:43:05 -0700 | [diff] [blame] | 247 | /* Receive status on the client: one and only one must be made on the client. |
| 248 | This operation always succeeds, meaning ops paired with this operation |
| 249 | will also appear to succeed, even though they may not have. In that case |
| 250 | the status will indicate some failure. |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 251 | */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 252 | GRPC_OP_RECV_STATUS_ON_CLIENT, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 253 | /* Receive status on the server: one and only one must be made on the server |
| 254 | */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 255 | GRPC_OP_RECV_CLOSE_ON_SERVER |
| 256 | } grpc_op_type; |
| 257 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 258 | /* Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT |
| 259 | which has |
Craig Tiller | b7800c1 | 2015-02-04 18:25:38 -0800 | [diff] [blame] | 260 | no arguments) */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 261 | typedef struct grpc_op { |
| 262 | grpc_op_type op; |
| 263 | union { |
| 264 | struct { |
| 265 | size_t count; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 266 | grpc_metadata *metadata; |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 267 | } send_initial_metadata; |
| 268 | grpc_byte_buffer *send_message; |
| 269 | struct { |
| 270 | size_t trailing_metadata_count; |
| 271 | grpc_metadata *trailing_metadata; |
| 272 | grpc_status_code status; |
| 273 | const char *status_details; |
| 274 | } send_status_from_server; |
Craig Tiller | 4f97273 | 2015-02-05 12:40:20 -0800 | [diff] [blame] | 275 | /* ownership of the array is with the caller, but ownership of the elements |
Craig Tiller | c56b2cb | 2015-02-05 16:35:38 -0800 | [diff] [blame] | 276 | stays with the call object (ie key, value members are owned by the call |
| 277 | object, recv_initial_metadata->array is owned by the caller). |
| 278 | After the operation completes, call grpc_metadata_array_destroy on this |
| 279 | value, or reuse it in a future op. */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 280 | grpc_metadata_array *recv_initial_metadata; |
| 281 | grpc_byte_buffer **recv_message; |
| 282 | struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 283 | /* ownership of the array is with the caller, but ownership of the |
| 284 | elements |
Craig Tiller | c56b2cb | 2015-02-05 16:35:38 -0800 | [diff] [blame] | 285 | stays with the call object (ie key, value members are owned by the call |
| 286 | object, trailing_metadata->array is owned by the caller). |
| 287 | After the operation completes, call grpc_metadata_array_destroy on this |
| 288 | value, or reuse it in a future op. */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 289 | grpc_metadata_array *trailing_metadata; |
| 290 | grpc_status_code *status; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 291 | /* status_details is a buffer owned by the application before the op |
| 292 | completes |
| 293 | and after the op has completed. During the operation status_details may |
| 294 | be |
| 295 | reallocated to a size larger than *status_details_capacity, in which |
| 296 | case |
Craig Tiller | c56b2cb | 2015-02-05 16:35:38 -0800 | [diff] [blame] | 297 | *status_details_capacity will be updated with the new array capacity. |
| 298 | |
| 299 | Pre-allocating space: |
| 300 | size_t my_capacity = 8; |
| 301 | char *my_details = gpr_malloc(my_capacity); |
| 302 | x.status_details = &my_details; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 303 | x.status_details_capacity = &my_capacity; |
Craig Tiller | c56b2cb | 2015-02-05 16:35:38 -0800 | [diff] [blame] | 304 | |
| 305 | Not pre-allocating space: |
| 306 | size_t my_capacity = 0; |
| 307 | char *my_details = NULL; |
| 308 | x.status_details = &my_details; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 309 | x.status_details_capacity = &my_capacity; |
Craig Tiller | c56b2cb | 2015-02-05 16:35:38 -0800 | [diff] [blame] | 310 | |
| 311 | After the call: |
| 312 | gpr_free(my_details); */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 313 | char **status_details; |
| 314 | size_t *status_details_capacity; |
| 315 | } recv_status_on_client; |
| 316 | struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 317 | /* out argument, set to 1 if the call failed in any way (seen as a |
| 318 | cancellation |
Craig Tiller | 0a5bcbc | 2015-02-09 21:39:04 -0800 | [diff] [blame] | 319 | on the server), or 0 if the call succeeded */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 320 | int *cancelled; |
| 321 | } recv_close_on_server; |
| 322 | } data; |
| 323 | } grpc_op; |
| 324 | |
Craig Tiller | a0e34a0 | 2015-02-17 17:06:23 -0800 | [diff] [blame] | 325 | /* Initialize the grpc library. |
| 326 | It is not safe to call any other grpc functions before calling this. |
| 327 | (To avoid overhead, little checking is done, and some things may work. We |
| 328 | do not warrant that they will continue to do so in future revisions of this |
| 329 | library). */ |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 330 | void grpc_init(void); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 331 | |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 332 | /* Shut down the grpc library. |
Craig Tiller | a0e34a0 | 2015-02-17 17:06:23 -0800 | [diff] [blame] | 333 | No memory is used by grpc after this call returns, nor are any instructions |
| 334 | executing within the grpc library. |
| 335 | Prior to calling, all application owned grpc objects must have been |
| 336 | destroyed. */ |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 337 | void grpc_shutdown(void); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 338 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 339 | grpc_completion_queue *grpc_completion_queue_create(void); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 340 | |
Nathaniel Manista | 6d41a05 | 2015-02-16 02:12:48 +0000 | [diff] [blame] | 341 | /* Blocks until an event is available, the completion queue is being shut down, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 342 | or deadline is reached. Returns NULL on timeout, otherwise the event that |
Craig Tiller | 9eb47ad | 2015-05-05 09:35:15 -0700 | [diff] [blame] | 343 | occurred. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 344 | |
| 345 | Callers must not call grpc_completion_queue_next and |
| 346 | grpc_completion_queue_pluck simultaneously on the same completion queue. */ |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 347 | grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, |
| 348 | gpr_timespec deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 349 | |
| 350 | /* Blocks until an event with tag 'tag' is available, the completion queue is |
| 351 | being shutdown or deadline is reached. Returns NULL on timeout, or a pointer |
Craig Tiller | 9eb47ad | 2015-05-05 09:35:15 -0700 | [diff] [blame] | 352 | to the event that occurred. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 353 | |
| 354 | Callers must not call grpc_completion_queue_next and |
| 355 | grpc_completion_queue_pluck simultaneously on the same completion queue. */ |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 356 | grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag, |
| 357 | gpr_timespec deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 358 | |
| 359 | /* Begin destruction of a completion queue. Once all possible events are |
Craig Tiller | 8ac56c9 | 2015-02-17 22:51:36 -0800 | [diff] [blame] | 360 | drained then grpc_completion_queue_next will start to produce |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 361 | GRPC_QUEUE_SHUTDOWN events only. At that point it's safe to call |
| 362 | grpc_completion_queue_destroy. |
| 363 | |
Craig Tiller | 8ac56c9 | 2015-02-17 22:51:36 -0800 | [diff] [blame] | 364 | After calling this function applications should ensure that no |
| 365 | NEW work is added to be published on this completion queue. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 366 | void grpc_completion_queue_shutdown(grpc_completion_queue *cq); |
| 367 | |
| 368 | /* Destroy a completion queue. The caller must ensure that the queue is |
| 369 | drained and no threads are executing grpc_completion_queue_next */ |
| 370 | void grpc_completion_queue_destroy(grpc_completion_queue *cq); |
| 371 | |
| 372 | /* Create a call given a grpc_channel, in order to call 'method'. The request |
| 373 | is not sent until grpc_call_invoke is called. All completions are sent to |
| 374 | 'completion_queue'. */ |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 375 | grpc_call *grpc_channel_create_call(grpc_channel *channel, |
| 376 | grpc_completion_queue *completion_queue, |
| 377 | const char *method, const char *host, |
| 378 | gpr_timespec deadline); |
Craig Tiller | 034929c | 2015-02-02 16:56:15 -0800 | [diff] [blame] | 379 | |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 380 | /* Pre-register a method/host pair on a channel. */ |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 381 | void *grpc_channel_register_call(grpc_channel *channel, const char *method, |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 382 | const char *host); |
| 383 | |
| 384 | /* Create a call given a handle returned from grpc_channel_register_call */ |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 385 | grpc_call *grpc_channel_create_registered_call( |
| 386 | grpc_channel *channel, grpc_completion_queue *completion_queue, |
| 387 | void *registered_call_handle, gpr_timespec deadline); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 388 | |
Craig Tiller | 034929c | 2015-02-02 16:56:15 -0800 | [diff] [blame] | 389 | /* Start a batch of operations defined in the array ops; when complete, post a |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 390 | completion of type 'tag' to the completion queue bound to the call. |
Craig Tiller | b7800c1 | 2015-02-04 18:25:38 -0800 | [diff] [blame] | 391 | The order of ops specified in the batch has no significance. |
| 392 | Only one operation of each type can be active at once in any given |
| 393 | batch. */ |
Craig Tiller | fef7669 | 2015-02-02 16:44:26 -0800 | [diff] [blame] | 394 | grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, |
| 395 | size_t nops, void *tag); |
| 396 | |
Craig Tiller | 29f2b21 | 2015-02-17 17:01:24 -0800 | [diff] [blame] | 397 | /* Create a client channel to 'target'. Additional channel level configuration |
| 398 | MAY be provided by grpc_channel_args, though the expectation is that most |
| 399 | clients will want to simply pass NULL. See grpc_channel_args definition |
| 400 | for more on this. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 401 | grpc_channel *grpc_channel_create(const char *target, |
| 402 | const grpc_channel_args *args); |
| 403 | |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 404 | /* Create a lame client: this client fails every operation attempted on it. */ |
| 405 | grpc_channel *grpc_lame_client_channel_create(void); |
| 406 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 407 | /* Close and destroy a grpc channel */ |
| 408 | void grpc_channel_destroy(grpc_channel *channel); |
| 409 | |
| 410 | /* THREAD-SAFETY for grpc_call |
| 411 | The following functions are thread-compatible for any given call: |
| 412 | grpc_call_add_metadata |
| 413 | grpc_call_invoke |
| 414 | grpc_call_start_write |
| 415 | grpc_call_writes_done |
| 416 | grpc_call_start_read |
| 417 | grpc_call_destroy |
| 418 | The function grpc_call_cancel is thread-safe, and can be called at any |
| 419 | point before grpc_call_destroy is called. */ |
| 420 | |
| 421 | /* Error handling for grpc_call |
| 422 | Most grpc_call functions return a grpc_error. If the error is not GRPC_OK |
| 423 | then the operation failed due to some unsatisfied precondition. |
| 424 | If a grpc_call fails, it's guaranteed that no change to the call state |
| 425 | has been made. */ |
| 426 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 427 | /* Called by clients to cancel an RPC on the server. |
| 428 | Can be called multiple times, from any thread. */ |
| 429 | grpc_call_error grpc_call_cancel(grpc_call *call); |
| 430 | |
Craig Tiller | d248c24 | 2015-01-14 11:49:12 -0800 | [diff] [blame] | 431 | /* Called by clients to cancel an RPC on the server. |
Craig Tiller | 6046dc3 | 2015-01-14 12:55:45 -0800 | [diff] [blame] | 432 | Can be called multiple times, from any thread. |
Craig Tiller | d248c24 | 2015-01-14 11:49:12 -0800 | [diff] [blame] | 433 | If a status has not been received for the call, set it to the status code |
Craig Tiller | 6046dc3 | 2015-01-14 12:55:45 -0800 | [diff] [blame] | 434 | and description passed in. |
Craig Tiller | d248c24 | 2015-01-14 11:49:12 -0800 | [diff] [blame] | 435 | Importantly, this function does not send status nor description to the |
| 436 | remote endpoint. */ |
Craig Tiller | 6046dc3 | 2015-01-14 12:55:45 -0800 | [diff] [blame] | 437 | grpc_call_error grpc_call_cancel_with_status(grpc_call *call, |
| 438 | grpc_status_code status, |
| 439 | const char *description); |
Craig Tiller | d248c24 | 2015-01-14 11:49:12 -0800 | [diff] [blame] | 440 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 441 | /* Destroy a call. */ |
| 442 | void grpc_call_destroy(grpc_call *call); |
| 443 | |
Craig Tiller | fada7d4 | 2015-02-11 23:03:55 -0800 | [diff] [blame] | 444 | /* Request notification of a new call */ |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 445 | grpc_call_error grpc_server_request_call( |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 446 | grpc_server *server, grpc_call **call, grpc_call_details *details, |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 447 | grpc_metadata_array *request_metadata, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 448 | grpc_completion_queue *cq_bound_to_call, |
| 449 | grpc_completion_queue *cq_for_notification, void *tag_new); |
Craig Tiller | 034929c | 2015-02-02 16:56:15 -0800 | [diff] [blame] | 450 | |
Craig Tiller | fada7d4 | 2015-02-11 23:03:55 -0800 | [diff] [blame] | 451 | /* Registers a method in the server. |
| 452 | Methods to this (host, method) pair will not be reported by |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 453 | grpc_server_request_call, but instead be reported by |
Craig Tiller | fada7d4 | 2015-02-11 23:03:55 -0800 | [diff] [blame] | 454 | grpc_server_request_registered_call when passed the appropriate |
| 455 | registered_method (as returned by this function). |
| 456 | Must be called before grpc_server_start. |
| 457 | Returns NULL on failure. */ |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 458 | void *grpc_server_register_method(grpc_server *server, const char *method, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 459 | const char *host); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 460 | |
Craig Tiller | fada7d4 | 2015-02-11 23:03:55 -0800 | [diff] [blame] | 461 | /* Request notification of a new pre-registered call */ |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 462 | grpc_call_error grpc_server_request_registered_call( |
| 463 | grpc_server *server, void *registered_method, grpc_call **call, |
| 464 | gpr_timespec *deadline, grpc_metadata_array *request_metadata, |
| 465 | grpc_byte_buffer **optional_payload, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 466 | grpc_completion_queue *cq_bound_to_call, |
| 467 | grpc_completion_queue *cq_for_notification, void *tag_new); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 468 | |
Craig Tiller | 29f2b21 | 2015-02-17 17:01:24 -0800 | [diff] [blame] | 469 | /* Create a server. Additional configuration for each incoming channel can |
Craig Tiller | e7163ab | 2015-02-17 20:46:08 -0800 | [diff] [blame] | 470 | be specified with args. If no additional configuration is needed, args can |
| 471 | be NULL. See grpc_channel_args for more. */ |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 472 | grpc_server *grpc_server_create(const grpc_channel_args *args); |
| 473 | |
| 474 | /* Register a completion queue with the server. Must be done for any completion |
| 475 | queue that is passed to grpc_server_request_* call. Must be performed prior |
| 476 | to grpc_server_start. */ |
| 477 | void grpc_server_register_completion_queue(grpc_server *server, |
| 478 | grpc_completion_queue *cq); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 479 | |
Craig Tiller | d251ab9 | 2015-02-17 17:22:14 -0800 | [diff] [blame] | 480 | /* Add a HTTP2 over plaintext over tcp listener. |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 481 | Returns bound port number on success, 0 on failure. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 482 | REQUIRES: server not started */ |
| 483 | int grpc_server_add_http2_port(grpc_server *server, const char *addr); |
| 484 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 485 | /* Start a server - tells all listeners to start listening */ |
| 486 | void grpc_server_start(grpc_server *server); |
| 487 | |
ctiller | 9a58df0 | 2014-12-11 16:26:49 -0800 | [diff] [blame] | 488 | /* Begin shutting down a server. |
| 489 | After completion, no new calls or connections will be admitted. |
Craig Tiller | aea2fc0 | 2015-02-17 16:54:53 -0800 | [diff] [blame] | 490 | Existing calls will be allowed to complete. |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame^] | 491 | Send a GRPC_OP_COMPLETE event when there are no more calls being serviced. |
Craig Tiller | aea2fc0 | 2015-02-17 16:54:53 -0800 | [diff] [blame] | 492 | Shutdown is idempotent, and all tags will be notified at once if multiple |
| 493 | grpc_server_shutdown_and_notify calls are made. */ |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 494 | void grpc_server_shutdown_and_notify(grpc_server *server, void *tag); |
| 495 | |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame^] | 496 | /* Cancel all in-progress calls. |
| 497 | Only usable after shutdown. */ |
| 498 | void grpc_server_cancel_all_calls(grpc_server *server); |
| 499 | |
ctiller | 9a58df0 | 2014-12-11 16:26:49 -0800 | [diff] [blame] | 500 | /* Destroy a server. |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame^] | 501 | Shutdown must have completed beforehand. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 502 | void grpc_server_destroy(grpc_server *server); |
| 503 | |
| 504 | #ifdef __cplusplus |
| 505 | } |
| 506 | #endif |
| 507 | |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 508 | #endif /* GRPC_GRPC_H */ |