blob: 40a3d2acb55f207c2f6d1da03dabec3c209963b6 [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_GRPC_H__
35#define __GRPC_GRPC_H__
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
44extern "C" {
45#endif
46
47/* Completion Channels enable notification of the completion of asynchronous
48 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
95/* An array of arguments that can be passed around */
96typedef struct {
97 size_t num_args;
98 grpc_arg *args;
99} grpc_channel_args;
100
101/* Channel argument keys: */
102/* Enable census for tracing and stats collection */
103#define GRPC_ARG_ENABLE_CENSUS "grpc.census"
104/* Maximum number of concurrent incoming streams to allow on a http2
105 connection */
106#define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams"
107/* Maximum message length that the channel can receive */
108#define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length"
109
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110/* Result of a grpc call. If the caller satisfies the prerequisites of a
111 particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
112 Receiving any other value listed here is an indication of a bug in the
113 caller. */
114typedef enum grpc_call_error {
115 /* everything went ok */
116 GRPC_CALL_OK = 0,
117 /* something failed, we don't know what */
118 GRPC_CALL_ERROR,
119 /* this method is not available on the server */
120 GRPC_CALL_ERROR_NOT_ON_SERVER,
121 /* this method is not available on the client */
122 GRPC_CALL_ERROR_NOT_ON_CLIENT,
nnoble0c475f02014-12-05 15:37:39 -0800123 /* this method must be called before server_accept */
124 GRPC_CALL_ERROR_ALREADY_ACCEPTED,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125 /* this method must be called before invoke */
126 GRPC_CALL_ERROR_ALREADY_INVOKED,
127 /* this method must be called after invoke */
128 GRPC_CALL_ERROR_NOT_INVOKED,
129 /* this call is already finished
130 (writes_done or write_status has already been called) */
131 GRPC_CALL_ERROR_ALREADY_FINISHED,
132 /* there is already an outstanding read/write operation on the call */
133 GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
134 /* the flags value was illegal for this call */
135 GRPC_CALL_ERROR_INVALID_FLAGS
136} grpc_call_error;
137
138/* Result of a grpc operation */
139typedef enum grpc_op_error {
140 /* everything went ok */
141 GRPC_OP_OK = 0,
142 /* something failed, we don't know what */
143 GRPC_OP_ERROR
144} grpc_op_error;
145
146/* Write Flags: */
147/* Hint that the write may be buffered and need not go out on the wire
148 immediately. GRPC is free to buffer the message until the next non-buffered
149 write, or until writes_done, but it need not buffer completely or at all. */
150#define GRPC_WRITE_BUFFER_HINT (0x00000001u)
151/* Force compression to be disabled for a particular write
152 (start_write/add_metadata). Illegal on invoke/accept. */
153#define GRPC_WRITE_NO_COMPRESS (0x00000002u)
154
155/* A buffer of bytes */
156struct grpc_byte_buffer;
157typedef struct grpc_byte_buffer grpc_byte_buffer;
158
159/* Sample helpers to obtain byte buffers (these will certainly move place */
160grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices);
161size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
162void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
163
164/* Reader for byte buffers. Iterates over slices in the byte buffer */
165struct grpc_byte_buffer_reader;
166typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
167
168grpc_byte_buffer_reader *grpc_byte_buffer_reader_create(
169 grpc_byte_buffer *buffer);
170/* At the end of the stream, returns 0. Otherwise, returns 1 and sets slice to
171 be the returned slice. Caller is responsible for calling gpr_slice_unref on
172 the result. */
173int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
174 gpr_slice *slice);
175void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
176
177/* A single metadata element */
178typedef struct grpc_metadata {
179 char *key;
180 char *value;
181 size_t value_length;
182} grpc_metadata;
183
184typedef enum grpc_completion_type {
185 GRPC_QUEUE_SHUTDOWN, /* Shutting down */
186 GRPC_READ, /* A read has completed */
187 GRPC_INVOKE_ACCEPTED, /* An invoke call has been accepted by flow
188 control */
189 GRPC_WRITE_ACCEPTED, /* A write has been accepted by
190 flow control */
191 GRPC_FINISH_ACCEPTED, /* writes_done or write_status has been accepted */
192 GRPC_CLIENT_METADATA_READ, /* The metadata array sent by server received at
193 client */
194 GRPC_FINISHED, /* An RPC has finished. The event contains status.
195 On the server this will be OK or Cancelled. */
196 GRPC_SERVER_RPC_NEW, /* A new RPC has arrived at the server */
197 GRPC_COMPLETION_DO_NOT_USE /* must be last, forces users to include
198 a default: case */
199} grpc_completion_type;
200
201typedef struct grpc_event {
202 grpc_completion_type type;
203 void *tag;
204 grpc_call *call;
205 /* Data associated with the completion type. Field names match the type of
206 completion as listed in grpc_completion_type. */
207 union {
208 /* Contains a pointer to the buffer that was read, or NULL at the end of a
209 stream. */
210 grpc_byte_buffer *read;
211 grpc_op_error write_accepted;
212 grpc_op_error finish_accepted;
213 grpc_op_error invoke_accepted;
214 struct {
215 size_t count;
216 grpc_metadata *elements;
217 } client_metadata_read;
ctiller2845cad2014-12-15 15:14:12 -0800218 struct {
219 grpc_status_code status;
220 const char *details;
221 size_t metadata_count;
222 grpc_metadata *metadata_elements;
223 } finished;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224 struct {
225 const char *method;
226 const char *host;
227 gpr_timespec deadline;
228 size_t metadata_count;
229 grpc_metadata *metadata_elements;
230 } server_rpc_new;
231 } data;
232} grpc_event;
233
234/* Initialize the grpc library */
Craig Tiller32946d32015-01-15 11:37:30 -0800235void grpc_init(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800236
237/* Shutdown the grpc library */
Craig Tiller32946d32015-01-15 11:37:30 -0800238void grpc_shutdown(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239
Craig Tiller32946d32015-01-15 11:37:30 -0800240grpc_completion_queue *grpc_completion_queue_create(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800241
242/* Blocks until an event is available, the completion queue is being shutdown,
243 or deadline is reached. Returns NULL on timeout, otherwise the event that
244 occurred. Callers should call grpc_event_finish once they have processed
245 the event.
246
247 Callers must not call grpc_completion_queue_next and
248 grpc_completion_queue_pluck simultaneously on the same completion queue. */
249grpc_event *grpc_completion_queue_next(grpc_completion_queue *cq,
250 gpr_timespec deadline);
251
252/* Blocks until an event with tag 'tag' is available, the completion queue is
253 being shutdown or deadline is reached. Returns NULL on timeout, or a pointer
254 to the event that occurred. Callers should call grpc_event_finish once they
255 have processed the event.
256
257 Callers must not call grpc_completion_queue_next and
258 grpc_completion_queue_pluck simultaneously on the same completion queue. */
259grpc_event *grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag,
260 gpr_timespec deadline);
261
262/* Cleanup any data owned by the event */
263void grpc_event_finish(grpc_event *event);
264
265/* Begin destruction of a completion queue. Once all possible events are
266 drained it's safe to call grpc_completion_queue_destroy. */
267void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
268
269/* Destroy a completion queue. The caller must ensure that the queue is
270 drained and no threads are executing grpc_completion_queue_next */
271void grpc_completion_queue_destroy(grpc_completion_queue *cq);
272
273/* Create a call given a grpc_channel, in order to call 'method'. The request
274 is not sent until grpc_call_invoke is called. All completions are sent to
275 'completion_queue'. */
276grpc_call *grpc_channel_create_call(grpc_channel *channel, const char *method,
277 const char *host, gpr_timespec deadline);
278
279/* Create a client channel */
280grpc_channel *grpc_channel_create(const char *target,
281 const grpc_channel_args *args);
282
283/* Close and destroy a grpc channel */
284void grpc_channel_destroy(grpc_channel *channel);
285
286/* THREAD-SAFETY for grpc_call
287 The following functions are thread-compatible for any given call:
288 grpc_call_add_metadata
289 grpc_call_invoke
290 grpc_call_start_write
291 grpc_call_writes_done
292 grpc_call_start_read
293 grpc_call_destroy
294 The function grpc_call_cancel is thread-safe, and can be called at any
295 point before grpc_call_destroy is called. */
296
297/* Error handling for grpc_call
298 Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
299 then the operation failed due to some unsatisfied precondition.
300 If a grpc_call fails, it's guaranteed that no change to the call state
301 has been made. */
302
303/* Add a single metadata element to the call, to be sent upon invocation.
304 flags is a bit-field combination of the write flags defined above.
yanggfb3aa262014-12-16 15:29:57 -0800305 REQUIRES: grpc_call_start_invoke/grpc_call_server_end_initial_metadata have
306 not been called on this call.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307 Produces no events. */
308grpc_call_error grpc_call_add_metadata(grpc_call *call, grpc_metadata *metadata,
309 gpr_uint32 flags);
310
311/* Invoke the RPC. Starts sending metadata and request headers on the wire.
312 flags is a bit-field combination of the write flags defined above.
313 REQUIRES: Can be called at most once per call.
314 Can only be called on the client.
315 Produces a GRPC_INVOKE_ACCEPTED event with invoke_accepted_tag when the
316 call has been invoked (meaning bytes can start flowing to the wire).
317 Produces a GRPC_CLIENT_METADATA_READ event with metadata_read_tag when
318 the servers initial metadata has been read.
319 Produces a GRPC_FINISHED event with finished_tag when the call has been
320 completed (there may be other events for the call pending at this
321 time) */
322grpc_call_error grpc_call_start_invoke(grpc_call *call,
323 grpc_completion_queue *cq,
324 void *invoke_accepted_tag,
325 void *metadata_read_tag,
326 void *finished_tag, gpr_uint32 flags);
327
nnoble0c475f02014-12-05 15:37:39 -0800328/* Accept an incoming RPC, binding a completion queue to it.
329 To be called before sending or receiving messages.
nnoble0c475f02014-12-05 15:37:39 -0800330 REQUIRES: Can be called at most once per call.
331 Can only be called on the server.
332 Produces a GRPC_FINISHED event with finished_tag when the call has been
333 completed (there may be other events for the call pending at this
334 time) */
335grpc_call_error grpc_call_server_accept(grpc_call *call,
336 grpc_completion_queue *cq,
337 void *finished_tag);
338
yanggfb3aa262014-12-16 15:29:57 -0800339/* Start sending metadata.
nnoble0c475f02014-12-05 15:37:39 -0800340 To be called before sending messages.
341 flags is a bit-field combination of the write flags defined above.
342 REQUIRES: Can be called at most once per call.
343 Can only be called on the server.
344 Must be called after grpc_call_server_accept */
345grpc_call_error grpc_call_server_end_initial_metadata(grpc_call *call,
346 gpr_uint32 flags);
347
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800348/* Called by clients to cancel an RPC on the server.
349 Can be called multiple times, from any thread. */
350grpc_call_error grpc_call_cancel(grpc_call *call);
351
Craig Tillerd248c242015-01-14 11:49:12 -0800352/* Called by clients to cancel an RPC on the server.
Craig Tiller6046dc32015-01-14 12:55:45 -0800353 Can be called multiple times, from any thread.
Craig Tillerd248c242015-01-14 11:49:12 -0800354 If a status has not been received for the call, set it to the status code
Craig Tiller6046dc32015-01-14 12:55:45 -0800355 and description passed in.
Craig Tillerd248c242015-01-14 11:49:12 -0800356 Importantly, this function does not send status nor description to the
357 remote endpoint. */
Craig Tiller6046dc32015-01-14 12:55:45 -0800358grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
359 grpc_status_code status,
360 const char *description);
Craig Tillerd248c242015-01-14 11:49:12 -0800361
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800362/* Queue a byte buffer for writing.
363 flags is a bit-field combination of the write flags defined above.
364 A write with byte_buffer null is allowed, and will not send any bytes on the
365 wire. If this is performed without GRPC_WRITE_BUFFER_HINT flag it provides
366 a mechanism to flush any previously buffered writes to outgoing flow control.
367 REQUIRES: No other writes are pending on the call. It is only safe to
368 start the next write after the corresponding write_accepted event
369 is received.
370 GRPC_INVOKE_ACCEPTED must have been received by the application
371 prior to calling this on the client. On the server,
nnoble0c475f02014-12-05 15:37:39 -0800372 grpc_call_server_end_of_initial_metadata must have been called
373 successfully.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800374 Produces a GRPC_WRITE_ACCEPTED event. */
375grpc_call_error grpc_call_start_write(grpc_call *call,
376 grpc_byte_buffer *byte_buffer, void *tag,
377 gpr_uint32 flags);
378
379/* Queue a status for writing.
380 REQUIRES: No other writes are pending on the call.
yanggfb3aa262014-12-16 15:29:57 -0800381 grpc_call_server_end_initial_metadata must have been called on the
382 call prior to calling this.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800383 Only callable on the server.
384 Produces a GRPC_FINISH_ACCEPTED event when the status is sent. */
385grpc_call_error grpc_call_start_write_status(grpc_call *call,
ctiller2845cad2014-12-15 15:14:12 -0800386 grpc_status_code status_code,
387 const char *status_message,
388 void *tag);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800389
390/* No more messages to send.
391 REQUIRES: No other writes are pending on the call.
392 Only callable on the client.
393 Produces a GRPC_FINISH_ACCEPTED event when all bytes for the call have passed
394 outgoing flow control. */
395grpc_call_error grpc_call_writes_done(grpc_call *call, void *tag);
396
397/* Initiate a read on a call. Output event contains a byte buffer with the
398 result of the read.
399 REQUIRES: No other reads are pending on the call. It is only safe to start
400 the next read after the corresponding read event is received.
nnoble0c475f02014-12-05 15:37:39 -0800401 On the client:
402 GRPC_INVOKE_ACCEPTED must have been received by the application
403 prior to calling this.
404 On the server:
405 grpc_call_server_accept must be called before calling this.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800406 Produces a single GRPC_READ event. */
407grpc_call_error grpc_call_start_read(grpc_call *call, void *tag);
408
409/* Destroy a call. */
410void grpc_call_destroy(grpc_call *call);
411
412/* Request a call on a server.
413 Allows the server to create a single GRPC_SERVER_RPC_NEW event, with tag
414 tag_new.
415 If the call is subsequently cancelled, the cancellation will occur with tag
416 tag_cancel.
417 REQUIRES: Server must not have been shutdown.
418 NOTE: calling this is the only way to obtain GRPC_SERVER_RPC_NEW events. */
419grpc_call_error grpc_server_request_call(grpc_server *server, void *tag_new);
420
421/* Create a server */
422grpc_server *grpc_server_create(grpc_completion_queue *cq,
423 const grpc_channel_args *args);
424
ctiller570d1f42015-01-12 16:29:52 -0800425/* Add a http2 over tcp listener.
426 Returns bound port number on success, 0 on failure.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800427 REQUIRES: server not started */
428int grpc_server_add_http2_port(grpc_server *server, const char *addr);
429
430/* Add a secure port to server; returns 1 on success, 0 on failure
431 REQUIRES: server not started */
432int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr);
433
434/* Start a server - tells all listeners to start listening */
435void grpc_server_start(grpc_server *server);
436
ctiller9a58df02014-12-11 16:26:49 -0800437/* Begin shutting down a server.
438 After completion, no new calls or connections will be admitted.
439 Existing calls will be allowed to complete. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800440void grpc_server_shutdown(grpc_server *server);
441
ctiller9a58df02014-12-11 16:26:49 -0800442/* Destroy a server.
443 Forcefully cancels all existing calls. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800444void grpc_server_destroy(grpc_server *server);
445
446#ifdef __cplusplus
447}
448#endif
449
Craig Tillerb5dcec52015-01-13 11:13:42 -0800450#endif /* __GRPC_GRPC_H__ */