blob: eee55e9964de81104446e001182fcc3bb77a248d [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_GRPC_H
35#define GRPC_GRPC_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
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
44extern "C" {
45#endif
46
Nathaniel Manista6d41a052015-02-16 02:12:48 +000047/* Completion Queues enable notification of the completion of asynchronous
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048 actions. */
49typedef struct grpc_completion_queue grpc_completion_queue;
50
51/* The Channel interface allows creation of Call objects. */
52typedef struct grpc_channel grpc_channel;
53
54/* A server listens to some port and responds to request calls */
55typedef 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. */
60typedef struct grpc_call grpc_call;
61
62/* Type specifier for grpc_arg */
63typedef 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. */
81typedef 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 Tiller29f2b212015-02-17 17:01:24 -080095/* 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800101typedef 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"
114
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115/* Result of a grpc call. If the caller satisfies the prerequisites of a
116 particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
117 Receiving any other value listed here is an indication of a bug in the
118 caller. */
119typedef enum grpc_call_error {
120 /* everything went ok */
121 GRPC_CALL_OK = 0,
122 /* something failed, we don't know what */
123 GRPC_CALL_ERROR,
124 /* this method is not available on the server */
125 GRPC_CALL_ERROR_NOT_ON_SERVER,
126 /* this method is not available on the client */
127 GRPC_CALL_ERROR_NOT_ON_CLIENT,
nnoble0c475f02014-12-05 15:37:39 -0800128 /* this method must be called before server_accept */
129 GRPC_CALL_ERROR_ALREADY_ACCEPTED,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130 /* this method must be called before invoke */
131 GRPC_CALL_ERROR_ALREADY_INVOKED,
132 /* this method must be called after invoke */
133 GRPC_CALL_ERROR_NOT_INVOKED,
134 /* this call is already finished
135 (writes_done or write_status has already been called) */
136 GRPC_CALL_ERROR_ALREADY_FINISHED,
137 /* there is already an outstanding read/write operation on the call */
138 GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
139 /* the flags value was illegal for this call */
140 GRPC_CALL_ERROR_INVALID_FLAGS
141} grpc_call_error;
142
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800143/* Write Flags: */
144/* Hint that the write may be buffered and need not go out on the wire
145 immediately. GRPC is free to buffer the message until the next non-buffered
146 write, or until writes_done, but it need not buffer completely or at all. */
147#define GRPC_WRITE_BUFFER_HINT (0x00000001u)
148/* Force compression to be disabled for a particular write
149 (start_write/add_metadata). Illegal on invoke/accept. */
150#define GRPC_WRITE_NO_COMPRESS (0x00000002u)
151
152/* A buffer of bytes */
153struct grpc_byte_buffer;
154typedef struct grpc_byte_buffer grpc_byte_buffer;
155
Nathaniel Manista6d41a052015-02-16 02:12:48 +0000156/* Sample helpers to obtain byte buffers (these will certainly move
157 someplace else) */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices);
Craig Tiller80fa15c2015-01-13 16:10:49 -0800159grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
161void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
162
163/* Reader for byte buffers. Iterates over slices in the byte buffer */
164struct grpc_byte_buffer_reader;
165typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
166
167grpc_byte_buffer_reader *grpc_byte_buffer_reader_create(
168 grpc_byte_buffer *buffer);
169/* At the end of the stream, returns 0. Otherwise, returns 1 and sets slice to
170 be the returned slice. Caller is responsible for calling gpr_slice_unref on
171 the result. */
172int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
173 gpr_slice *slice);
174void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
175
176/* A single metadata element */
177typedef struct grpc_metadata {
Craig Tiller4f972732015-02-05 12:40:20 -0800178 const char *key;
179 const char *value;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180 size_t value_length;
Craig Tiller6902ad22015-04-16 08:01:49 -0700181
182 /* The following fields are reserved for grpc internal use.
183 There is no need to initialize them, and they will be set to garbage during
184 calls to grpc. */
185 struct {
Craig Tiller9c9d4e02015-04-20 09:03:29 -0700186 void *obfuscated[3];
Craig Tiller6902ad22015-04-16 08:01:49 -0700187 } internal_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188} grpc_metadata;
189
190typedef enum grpc_completion_type {
Craig Tiller64be9f72015-05-04 14:53:51 -0700191 GRPC_QUEUE_SHUTDOWN, /* Shutting down */
192 GRPC_QUEUE_TIMEOUT, /* No event before timeout */
193 GRPC_OP_COMPLETE /* operation completion */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194} grpc_completion_type;
195
196typedef struct grpc_event {
197 grpc_completion_type type;
Craig Tiller64be9f72015-05-04 14:53:51 -0700198 int success;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199 void *tag;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200} grpc_event;
201
Craig Tillerfef76692015-02-02 16:44:26 -0800202typedef struct {
203 size_t count;
204 size_t capacity;
205 grpc_metadata *metadata;
206} grpc_metadata_array;
207
Craig Tillerea61b072015-02-03 19:19:27 -0800208void grpc_metadata_array_init(grpc_metadata_array *array);
209void grpc_metadata_array_destroy(grpc_metadata_array *array);
210
Craig Tillerfef76692015-02-02 16:44:26 -0800211typedef struct {
Craig Tillerea61b072015-02-03 19:19:27 -0800212 char *method;
Craig Tiller034929c2015-02-02 16:56:15 -0800213 size_t method_capacity;
Craig Tillerea61b072015-02-03 19:19:27 -0800214 char *host;
Craig Tiller034929c2015-02-02 16:56:15 -0800215 size_t host_capacity;
Craig Tillerfef76692015-02-02 16:44:26 -0800216 gpr_timespec deadline;
217} grpc_call_details;
218
Craig Tillerea61b072015-02-03 19:19:27 -0800219void grpc_call_details_init(grpc_call_details *details);
220void grpc_call_details_destroy(grpc_call_details *details);
221
Craig Tillerfef76692015-02-02 16:44:26 -0800222typedef enum {
Craig Tiller24be0f72015-02-10 14:04:22 -0800223 /* Send initial metadata: one and only one instance MUST be sent for each
224 call,
Craig Tillerb7800c12015-02-04 18:25:38 -0800225 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800226 GRPC_OP_SEND_INITIAL_METADATA = 0,
Craig Tillerb7800c12015-02-04 18:25:38 -0800227 /* Send a message: 0 or more of these operations can occur for each call */
Craig Tillerfef76692015-02-02 16:44:26 -0800228 GRPC_OP_SEND_MESSAGE,
Craig Tiller24be0f72015-02-10 14:04:22 -0800229 /* Send a close from the server: one and only one instance MUST be sent from
230 the client,
Craig Tillerb7800c12015-02-04 18:25:38 -0800231 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800232 GRPC_OP_SEND_CLOSE_FROM_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800233 /* Send status from the server: one and only one instance MUST be sent from
234 the server
Craig Tillerb7800c12015-02-04 18:25:38 -0800235 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800236 GRPC_OP_SEND_STATUS_FROM_SERVER,
Craig Tillerb7800c12015-02-04 18:25:38 -0800237 /* Receive initial metadata: one and only one MUST be made on the client, must
238 not be made on the server */
Craig Tillerfef76692015-02-02 16:44:26 -0800239 GRPC_OP_RECV_INITIAL_METADATA,
Craig Tillerb7800c12015-02-04 18:25:38 -0800240 /* Receive a message: 0 or more of these operations can occur for each call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800241 GRPC_OP_RECV_MESSAGE,
Craig Tiller24be0f72015-02-10 14:04:22 -0800242 /* Receive status on the client: one and only one must be made on the client
243 */
Craig Tillerfef76692015-02-02 16:44:26 -0800244 GRPC_OP_RECV_STATUS_ON_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800245 /* Receive status on the server: one and only one must be made on the server
246 */
Craig Tillerfef76692015-02-02 16:44:26 -0800247 GRPC_OP_RECV_CLOSE_ON_SERVER
248} grpc_op_type;
249
Craig Tiller24be0f72015-02-10 14:04:22 -0800250/* Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT
251 which has
Craig Tillerb7800c12015-02-04 18:25:38 -0800252 no arguments) */
Craig Tillerfef76692015-02-02 16:44:26 -0800253typedef struct grpc_op {
254 grpc_op_type op;
255 union {
256 struct {
257 size_t count;
Craig Tiller6902ad22015-04-16 08:01:49 -0700258 grpc_metadata *metadata;
Craig Tillerfef76692015-02-02 16:44:26 -0800259 } send_initial_metadata;
260 grpc_byte_buffer *send_message;
261 struct {
262 size_t trailing_metadata_count;
263 grpc_metadata *trailing_metadata;
264 grpc_status_code status;
265 const char *status_details;
266 } send_status_from_server;
Craig Tiller4f972732015-02-05 12:40:20 -0800267 /* ownership of the array is with the caller, but ownership of the elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800268 stays with the call object (ie key, value members are owned by the call
269 object, recv_initial_metadata->array is owned by the caller).
270 After the operation completes, call grpc_metadata_array_destroy on this
271 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800272 grpc_metadata_array *recv_initial_metadata;
273 grpc_byte_buffer **recv_message;
274 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800275 /* ownership of the array is with the caller, but ownership of the
276 elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800277 stays with the call object (ie key, value members are owned by the call
278 object, trailing_metadata->array is owned by the caller).
279 After the operation completes, call grpc_metadata_array_destroy on this
280 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800281 grpc_metadata_array *trailing_metadata;
282 grpc_status_code *status;
Craig Tiller24be0f72015-02-10 14:04:22 -0800283 /* status_details is a buffer owned by the application before the op
284 completes
285 and after the op has completed. During the operation status_details may
286 be
287 reallocated to a size larger than *status_details_capacity, in which
288 case
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800289 *status_details_capacity will be updated with the new array capacity.
290
291 Pre-allocating space:
292 size_t my_capacity = 8;
293 char *my_details = gpr_malloc(my_capacity);
294 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800295 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800296
297 Not pre-allocating space:
298 size_t my_capacity = 0;
299 char *my_details = NULL;
300 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800301 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800302
303 After the call:
304 gpr_free(my_details); */
Craig Tillerfef76692015-02-02 16:44:26 -0800305 char **status_details;
306 size_t *status_details_capacity;
307 } recv_status_on_client;
308 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800309 /* out argument, set to 1 if the call failed in any way (seen as a
310 cancellation
Craig Tiller0a5bcbc2015-02-09 21:39:04 -0800311 on the server), or 0 if the call succeeded */
Craig Tillerfef76692015-02-02 16:44:26 -0800312 int *cancelled;
313 } recv_close_on_server;
314 } data;
315} grpc_op;
316
Craig Tillera0e34a02015-02-17 17:06:23 -0800317/* Initialize the grpc library.
318 It is not safe to call any other grpc functions before calling this.
319 (To avoid overhead, little checking is done, and some things may work. We
320 do not warrant that they will continue to do so in future revisions of this
321 library). */
Craig Tiller32946d32015-01-15 11:37:30 -0800322void grpc_init(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800323
Craig Tillerb20111c2015-04-10 23:27:11 +0000324/* Shut down the grpc library.
Craig Tillera0e34a02015-02-17 17:06:23 -0800325 No memory is used by grpc after this call returns, nor are any instructions
326 executing within the grpc library.
327 Prior to calling, all application owned grpc objects must have been
328 destroyed. */
Craig Tiller32946d32015-01-15 11:37:30 -0800329void grpc_shutdown(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800330
Craig Tiller32946d32015-01-15 11:37:30 -0800331grpc_completion_queue *grpc_completion_queue_create(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332
Nathaniel Manista6d41a052015-02-16 02:12:48 +0000333/* Blocks until an event is available, the completion queue is being shut down,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800334 or deadline is reached. Returns NULL on timeout, otherwise the event that
335 occurred. Callers should call grpc_event_finish once they have processed
336 the event.
337
338 Callers must not call grpc_completion_queue_next and
339 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700340grpc_event grpc_completion_queue_next(grpc_completion_queue *cq,
341 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800342
343/* Blocks until an event with tag 'tag' is available, the completion queue is
344 being shutdown or deadline is reached. Returns NULL on timeout, or a pointer
345 to the event that occurred. Callers should call grpc_event_finish once they
346 have processed the event.
347
348 Callers must not call grpc_completion_queue_next and
349 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700350grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag,
351 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800352
353/* Begin destruction of a completion queue. Once all possible events are
Craig Tiller8ac56c92015-02-17 22:51:36 -0800354 drained then grpc_completion_queue_next will start to produce
Craig Tillerb20111c2015-04-10 23:27:11 +0000355 GRPC_QUEUE_SHUTDOWN events only. At that point it's safe to call
356 grpc_completion_queue_destroy.
357
Craig Tiller8ac56c92015-02-17 22:51:36 -0800358 After calling this function applications should ensure that no
359 NEW work is added to be published on this completion queue. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800360void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
361
362/* Destroy a completion queue. The caller must ensure that the queue is
363 drained and no threads are executing grpc_completion_queue_next */
364void grpc_completion_queue_destroy(grpc_completion_queue *cq);
365
366/* Create a call given a grpc_channel, in order to call 'method'. The request
367 is not sent until grpc_call_invoke is called. All completions are sent to
368 'completion_queue'. */
Craig Tillerfb189f82015-02-03 12:07:07 -0800369grpc_call *grpc_channel_create_call(grpc_channel *channel,
370 grpc_completion_queue *completion_queue,
371 const char *method, const char *host,
372 gpr_timespec deadline);
Craig Tiller034929c2015-02-02 16:56:15 -0800373
Craig Tiller08453372015-04-10 16:05:38 -0700374/* Pre-register a method/host pair on a channel. */
Craig Tillerb20111c2015-04-10 23:27:11 +0000375void *grpc_channel_register_call(grpc_channel *channel, const char *method,
Craig Tiller08453372015-04-10 16:05:38 -0700376 const char *host);
377
378/* Create a call given a handle returned from grpc_channel_register_call */
Craig Tillerb20111c2015-04-10 23:27:11 +0000379grpc_call *grpc_channel_create_registered_call(
380 grpc_channel *channel, grpc_completion_queue *completion_queue,
381 void *registered_call_handle, gpr_timespec deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700382
Craig Tiller034929c2015-02-02 16:56:15 -0800383/* Start a batch of operations defined in the array ops; when complete, post a
Craig Tiller24be0f72015-02-10 14:04:22 -0800384 completion of type 'tag' to the completion queue bound to the call.
Craig Tillerb7800c12015-02-04 18:25:38 -0800385 The order of ops specified in the batch has no significance.
386 Only one operation of each type can be active at once in any given
387 batch. */
Craig Tillerfef76692015-02-02 16:44:26 -0800388grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
389 size_t nops, void *tag);
390
Craig Tiller29f2b212015-02-17 17:01:24 -0800391/* Create a client channel to 'target'. Additional channel level configuration
392 MAY be provided by grpc_channel_args, though the expectation is that most
393 clients will want to simply pass NULL. See grpc_channel_args definition
394 for more on this. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800395grpc_channel *grpc_channel_create(const char *target,
396 const grpc_channel_args *args);
397
Craig Tiller42bc87c2015-02-23 08:50:19 -0800398/* Create a lame client: this client fails every operation attempted on it. */
399grpc_channel *grpc_lame_client_channel_create(void);
400
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800401/* Close and destroy a grpc channel */
402void grpc_channel_destroy(grpc_channel *channel);
403
404/* THREAD-SAFETY for grpc_call
405 The following functions are thread-compatible for any given call:
406 grpc_call_add_metadata
407 grpc_call_invoke
408 grpc_call_start_write
409 grpc_call_writes_done
410 grpc_call_start_read
411 grpc_call_destroy
412 The function grpc_call_cancel is thread-safe, and can be called at any
413 point before grpc_call_destroy is called. */
414
415/* Error handling for grpc_call
416 Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
417 then the operation failed due to some unsatisfied precondition.
418 If a grpc_call fails, it's guaranteed that no change to the call state
419 has been made. */
420
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800421/* Called by clients to cancel an RPC on the server.
422 Can be called multiple times, from any thread. */
423grpc_call_error grpc_call_cancel(grpc_call *call);
424
Craig Tillerd248c242015-01-14 11:49:12 -0800425/* Called by clients to cancel an RPC on the server.
Craig Tiller6046dc32015-01-14 12:55:45 -0800426 Can be called multiple times, from any thread.
Craig Tillerd248c242015-01-14 11:49:12 -0800427 If a status has not been received for the call, set it to the status code
Craig Tiller6046dc32015-01-14 12:55:45 -0800428 and description passed in.
Craig Tillerd248c242015-01-14 11:49:12 -0800429 Importantly, this function does not send status nor description to the
430 remote endpoint. */
Craig Tiller6046dc32015-01-14 12:55:45 -0800431grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
432 grpc_status_code status,
433 const char *description);
Craig Tillerd248c242015-01-14 11:49:12 -0800434
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800435/* Destroy a call. */
436void grpc_call_destroy(grpc_call *call);
437
Craig Tillerfada7d42015-02-11 23:03:55 -0800438/* Request notification of a new call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800439grpc_call_error grpc_server_request_call(
Craig Tillerea61b072015-02-03 19:19:27 -0800440 grpc_server *server, grpc_call **call, grpc_call_details *details,
Craig Tillerfb189f82015-02-03 12:07:07 -0800441 grpc_metadata_array *request_metadata,
Craig Tillerb20111c2015-04-10 23:27:11 +0000442 grpc_completion_queue *cq_bound_to_call, void *tag_new);
Craig Tiller034929c2015-02-02 16:56:15 -0800443
Craig Tillerfada7d42015-02-11 23:03:55 -0800444/* Registers a method in the server.
445 Methods to this (host, method) pair will not be reported by
Craig Tiller06059952015-02-18 08:34:56 -0800446 grpc_server_request_call, but instead be reported by
Craig Tillerfada7d42015-02-11 23:03:55 -0800447 grpc_server_request_registered_call when passed the appropriate
448 registered_method (as returned by this function).
449 Must be called before grpc_server_start.
450 Returns NULL on failure. */
Craig Tiller24be0f72015-02-10 14:04:22 -0800451void *grpc_server_register_method(grpc_server *server, const char *method,
Craig Tiller06059952015-02-18 08:34:56 -0800452 const char *host,
Craig Tiller20bc56d2015-02-12 09:02:56 -0800453 grpc_completion_queue *new_call_cq);
Craig Tiller24be0f72015-02-10 14:04:22 -0800454
Craig Tillerfada7d42015-02-11 23:03:55 -0800455/* Request notification of a new pre-registered call */
Craig Tiller24be0f72015-02-10 14:04:22 -0800456grpc_call_error grpc_server_request_registered_call(
457 grpc_server *server, void *registered_method, grpc_call **call,
458 gpr_timespec *deadline, grpc_metadata_array *request_metadata,
459 grpc_byte_buffer **optional_payload,
Craig Tiller8e8fd892015-02-10 17:02:08 -0800460 grpc_completion_queue *cq_bound_to_call, void *tag_new);
Craig Tiller24be0f72015-02-10 14:04:22 -0800461
Craig Tiller29f2b212015-02-17 17:01:24 -0800462/* Create a server. Additional configuration for each incoming channel can
Craig Tillere7163ab2015-02-17 20:46:08 -0800463 be specified with args. If no additional configuration is needed, args can
464 be NULL. See grpc_channel_args for more. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800465grpc_server *grpc_server_create(grpc_completion_queue *cq,
466 const grpc_channel_args *args);
467
Craig Tillerd251ab92015-02-17 17:22:14 -0800468/* Add a HTTP2 over plaintext over tcp listener.
ctiller570d1f42015-01-12 16:29:52 -0800469 Returns bound port number on success, 0 on failure.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800470 REQUIRES: server not started */
471int grpc_server_add_http2_port(grpc_server *server, const char *addr);
472
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800473/* Start a server - tells all listeners to start listening */
474void grpc_server_start(grpc_server *server);
475
ctiller9a58df02014-12-11 16:26:49 -0800476/* Begin shutting down a server.
477 After completion, no new calls or connections will be admitted.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800478 Existing calls will be allowed to complete.
479 Shutdown is idempotent. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800480void grpc_server_shutdown(grpc_server *server);
481
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800482/* As per grpc_server_shutdown, but send a GRPC_SERVER_SHUTDOWN event when
Craig Tilleraea2fc02015-02-17 16:54:53 -0800483 there are no more calls being serviced.
484 Shutdown is idempotent, and all tags will be notified at once if multiple
485 grpc_server_shutdown_and_notify calls are made. */
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800486void grpc_server_shutdown_and_notify(grpc_server *server, void *tag);
487
ctiller9a58df02014-12-11 16:26:49 -0800488/* Destroy a server.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800489 Forcefully cancels all existing calls.
490 Implies grpc_server_shutdown() if one was not previously performed. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800491void grpc_server_destroy(grpc_server *server);
492
493#ifdef __cplusplus
494}
495#endif
496
Craig Tillerb20111c2015-04-10 23:27:11 +0000497#endif /* GRPC_GRPC_H */