blob: 17e155a929c35ad865f2bd63db9b8fa508330308 [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
110/* Status of a completed call */
111typedef struct grpc_status {
112 grpc_status_code code;
113 char *details;
114} grpc_status;
115
116/* Result of a grpc call. If the caller satisfies the prerequisites of a
117 particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
118 Receiving any other value listed here is an indication of a bug in the
119 caller. */
120typedef enum grpc_call_error {
121 /* everything went ok */
122 GRPC_CALL_OK = 0,
123 /* something failed, we don't know what */
124 GRPC_CALL_ERROR,
125 /* this method is not available on the server */
126 GRPC_CALL_ERROR_NOT_ON_SERVER,
127 /* this method is not available on the client */
128 GRPC_CALL_ERROR_NOT_ON_CLIENT,
129 /* this method must be called before invoke */
130 GRPC_CALL_ERROR_ALREADY_INVOKED,
131 /* this method must be called after invoke */
132 GRPC_CALL_ERROR_NOT_INVOKED,
133 /* this call is already finished
134 (writes_done or write_status has already been called) */
135 GRPC_CALL_ERROR_ALREADY_FINISHED,
136 /* there is already an outstanding read/write operation on the call */
137 GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
138 /* the flags value was illegal for this call */
139 GRPC_CALL_ERROR_INVALID_FLAGS
140} grpc_call_error;
141
142/* Result of a grpc operation */
143typedef enum grpc_op_error {
144 /* everything went ok */
145 GRPC_OP_OK = 0,
146 /* something failed, we don't know what */
147 GRPC_OP_ERROR
148} grpc_op_error;
149
150/* Write Flags: */
151/* Hint that the write may be buffered and need not go out on the wire
152 immediately. GRPC is free to buffer the message until the next non-buffered
153 write, or until writes_done, but it need not buffer completely or at all. */
154#define GRPC_WRITE_BUFFER_HINT (0x00000001u)
155/* Force compression to be disabled for a particular write
156 (start_write/add_metadata). Illegal on invoke/accept. */
157#define GRPC_WRITE_NO_COMPRESS (0x00000002u)
158
159/* A buffer of bytes */
160struct grpc_byte_buffer;
161typedef struct grpc_byte_buffer grpc_byte_buffer;
162
163/* Sample helpers to obtain byte buffers (these will certainly move place */
164grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices);
165size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
166void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
167
168/* Reader for byte buffers. Iterates over slices in the byte buffer */
169struct grpc_byte_buffer_reader;
170typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
171
172grpc_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. */
177int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
178 gpr_slice *slice);
179void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
180
181/* A single metadata element */
182typedef struct grpc_metadata {
183 char *key;
184 char *value;
185 size_t value_length;
186} grpc_metadata;
187
188typedef enum grpc_completion_type {
189 GRPC_QUEUE_SHUTDOWN, /* Shutting down */
190 GRPC_READ, /* A read has completed */
191 GRPC_INVOKE_ACCEPTED, /* An invoke call has been accepted by flow
192 control */
193 GRPC_WRITE_ACCEPTED, /* A write has been accepted by
194 flow control */
195 GRPC_FINISH_ACCEPTED, /* writes_done or write_status has been accepted */
196 GRPC_CLIENT_METADATA_READ, /* The metadata array sent by server received at
197 client */
198 GRPC_FINISHED, /* An RPC has finished. The event contains status.
199 On the server this will be OK or Cancelled. */
200 GRPC_SERVER_RPC_NEW, /* A new RPC has arrived at the server */
201 GRPC_COMPLETION_DO_NOT_USE /* must be last, forces users to include
202 a default: case */
203} grpc_completion_type;
204
205typedef struct grpc_event {
206 grpc_completion_type type;
207 void *tag;
208 grpc_call *call;
209 /* Data associated with the completion type. Field names match the type of
210 completion as listed in grpc_completion_type. */
211 union {
212 /* Contains a pointer to the buffer that was read, or NULL at the end of a
213 stream. */
214 grpc_byte_buffer *read;
215 grpc_op_error write_accepted;
216 grpc_op_error finish_accepted;
217 grpc_op_error invoke_accepted;
218 struct {
219 size_t count;
220 grpc_metadata *elements;
221 } client_metadata_read;
222 grpc_status finished;
223 struct {
224 const char *method;
225 const char *host;
226 gpr_timespec deadline;
227 size_t metadata_count;
228 grpc_metadata *metadata_elements;
229 } server_rpc_new;
230 } data;
231} grpc_event;
232
233/* Initialize the grpc library */
234void grpc_init();
235
236/* Shutdown the grpc library */
237void grpc_shutdown();
238
239grpc_completion_queue *grpc_completion_queue_create();
240
241/* Blocks until an event is available, the completion queue is being shutdown,
242 or deadline is reached. Returns NULL on timeout, otherwise the event that
243 occurred. Callers should call grpc_event_finish once they have processed
244 the event.
245
246 Callers must not call grpc_completion_queue_next and
247 grpc_completion_queue_pluck simultaneously on the same completion queue. */
248grpc_event *grpc_completion_queue_next(grpc_completion_queue *cq,
249 gpr_timespec deadline);
250
251/* Blocks until an event with tag 'tag' is available, the completion queue is
252 being shutdown or deadline is reached. Returns NULL on timeout, or a pointer
253 to the event that occurred. Callers should call grpc_event_finish once they
254 have processed the event.
255
256 Callers must not call grpc_completion_queue_next and
257 grpc_completion_queue_pluck simultaneously on the same completion queue. */
258grpc_event *grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag,
259 gpr_timespec deadline);
260
261/* Cleanup any data owned by the event */
262void grpc_event_finish(grpc_event *event);
263
264/* Begin destruction of a completion queue. Once all possible events are
265 drained it's safe to call grpc_completion_queue_destroy. */
266void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
267
268/* Destroy a completion queue. The caller must ensure that the queue is
269 drained and no threads are executing grpc_completion_queue_next */
270void grpc_completion_queue_destroy(grpc_completion_queue *cq);
271
272/* Create a call given a grpc_channel, in order to call 'method'. The request
273 is not sent until grpc_call_invoke is called. All completions are sent to
274 'completion_queue'. */
275grpc_call *grpc_channel_create_call(grpc_channel *channel, const char *method,
276 const char *host, gpr_timespec deadline);
277
278/* Create a client channel */
279grpc_channel *grpc_channel_create(const char *target,
280 const grpc_channel_args *args);
281
282/* Close and destroy a grpc channel */
283void grpc_channel_destroy(grpc_channel *channel);
284
285/* THREAD-SAFETY for grpc_call
286 The following functions are thread-compatible for any given call:
287 grpc_call_add_metadata
288 grpc_call_invoke
289 grpc_call_start_write
290 grpc_call_writes_done
291 grpc_call_start_read
292 grpc_call_destroy
293 The function grpc_call_cancel is thread-safe, and can be called at any
294 point before grpc_call_destroy is called. */
295
296/* Error handling for grpc_call
297 Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
298 then the operation failed due to some unsatisfied precondition.
299 If a grpc_call fails, it's guaranteed that no change to the call state
300 has been made. */
301
302/* Add a single metadata element to the call, to be sent upon invocation.
303 flags is a bit-field combination of the write flags defined above.
304 REQUIRES: grpc_call_start_invoke/grpc_call_accept have not been called on
305 this call.
306 Produces no events. */
307grpc_call_error grpc_call_add_metadata(grpc_call *call, grpc_metadata *metadata,
308 gpr_uint32 flags);
309
310/* Invoke the RPC. Starts sending metadata and request headers on the wire.
311 flags is a bit-field combination of the write flags defined above.
312 REQUIRES: Can be called at most once per call.
313 Can only be called on the client.
314 Produces a GRPC_INVOKE_ACCEPTED event with invoke_accepted_tag when the
315 call has been invoked (meaning bytes can start flowing to the wire).
316 Produces a GRPC_CLIENT_METADATA_READ event with metadata_read_tag when
317 the servers initial metadata has been read.
318 Produces a GRPC_FINISHED event with finished_tag when the call has been
319 completed (there may be other events for the call pending at this
320 time) */
321grpc_call_error grpc_call_start_invoke(grpc_call *call,
322 grpc_completion_queue *cq,
323 void *invoke_accepted_tag,
324 void *metadata_read_tag,
325 void *finished_tag, gpr_uint32 flags);
326
327/* Accept an incoming RPC, binding a completion queue to it.
328 To be called after adding metadata to the call, but before sending
329 messages.
330 flags is a bit-field combination of the write flags defined above.
331 REQUIRES: Can be called at most once per call.
332 Can only be called on the server.
333 Produces a GRPC_FINISHED event with finished_tag when the call has been
334 completed (there may be other events for the call pending at this
335 time) */
336grpc_call_error grpc_call_accept(grpc_call *call, grpc_completion_queue *cq,
337 void *finished_tag, gpr_uint32 flags);
338
339/* Called by clients to cancel an RPC on the server.
340 Can be called multiple times, from any thread. */
341grpc_call_error grpc_call_cancel(grpc_call *call);
342
343/* Queue a byte buffer for writing.
344 flags is a bit-field combination of the write flags defined above.
345 A write with byte_buffer null is allowed, and will not send any bytes on the
346 wire. If this is performed without GRPC_WRITE_BUFFER_HINT flag it provides
347 a mechanism to flush any previously buffered writes to outgoing flow control.
348 REQUIRES: No other writes are pending on the call. It is only safe to
349 start the next write after the corresponding write_accepted event
350 is received.
351 GRPC_INVOKE_ACCEPTED must have been received by the application
352 prior to calling this on the client. On the server,
353 grpc_call_accept must have been called successfully.
354 Produces a GRPC_WRITE_ACCEPTED event. */
355grpc_call_error grpc_call_start_write(grpc_call *call,
356 grpc_byte_buffer *byte_buffer, void *tag,
357 gpr_uint32 flags);
358
359/* Queue a status for writing.
360 REQUIRES: No other writes are pending on the call.
361 grpc_call_accept must have been called on the call prior to calling
362 this.
363 Only callable on the server.
364 Produces a GRPC_FINISH_ACCEPTED event when the status is sent. */
365grpc_call_error grpc_call_start_write_status(grpc_call *call,
366 grpc_status status, void *tag);
367
368/* No more messages to send.
369 REQUIRES: No other writes are pending on the call.
370 Only callable on the client.
371 Produces a GRPC_FINISH_ACCEPTED event when all bytes for the call have passed
372 outgoing flow control. */
373grpc_call_error grpc_call_writes_done(grpc_call *call, void *tag);
374
375/* Initiate a read on a call. Output event contains a byte buffer with the
376 result of the read.
377 REQUIRES: No other reads are pending on the call. It is only safe to start
378 the next read after the corresponding read event is received.
379 GRPC_INVOKE_ACCEPTED must have been received by the application
380 prior to calling this.
381 Produces a single GRPC_READ event. */
382grpc_call_error grpc_call_start_read(grpc_call *call, void *tag);
383
384/* Destroy a call. */
385void grpc_call_destroy(grpc_call *call);
386
387/* Request a call on a server.
388 Allows the server to create a single GRPC_SERVER_RPC_NEW event, with tag
389 tag_new.
390 If the call is subsequently cancelled, the cancellation will occur with tag
391 tag_cancel.
392 REQUIRES: Server must not have been shutdown.
393 NOTE: calling this is the only way to obtain GRPC_SERVER_RPC_NEW events. */
394grpc_call_error grpc_server_request_call(grpc_server *server, void *tag_new);
395
396/* Create a server */
397grpc_server *grpc_server_create(grpc_completion_queue *cq,
398 const grpc_channel_args *args);
399
400/* Add a http2 over tcp listener; returns 1 on success, 0 on failure
401 REQUIRES: server not started */
402int grpc_server_add_http2_port(grpc_server *server, const char *addr);
403
404/* Add a secure port to server; returns 1 on success, 0 on failure
405 REQUIRES: server not started */
406int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr);
407
408/* Start a server - tells all listeners to start listening */
409void grpc_server_start(grpc_server *server);
410
411/* Begin shutting down a server. */
412void grpc_server_shutdown(grpc_server *server);
413
414/* Destroy a server */
415void grpc_server_destroy(grpc_server *server);
416
417#ifdef __cplusplus
418}
419#endif
420
421#endif /* __GRPC_GRPC_H__ */