cgrpc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef cgrpc_h
  17. #define cgrpc_h
  18. #include <stdlib.h>
  19. // This file lists C functions and types used to build Swift gRPC support
  20. #ifndef cgrpc_internal_h
  21. // all domain types are opaque pointers
  22. typedef void cgrpc_byte_buffer;
  23. typedef void cgrpc_call;
  24. typedef void cgrpc_channel;
  25. typedef void cgrpc_completion_queue;
  26. typedef void cgrpc_handler;
  27. typedef void cgrpc_metadata;
  28. typedef void cgrpc_metadata_array;
  29. typedef void cgrpc_mutex;
  30. typedef void cgrpc_observer;
  31. typedef void cgrpc_observer_send_initial_metadata;
  32. typedef void cgrpc_observer_send_message;
  33. typedef void cgrpc_observer_send_close_from_client;
  34. typedef void cgrpc_observer_send_status_from_server;
  35. typedef void cgrpc_observer_recv_initial_metadata;
  36. typedef void cgrpc_observer_recv_message;
  37. typedef void cgrpc_observer_recv_status_on_client;
  38. typedef void cgrpc_observer_recv_close_on_server;
  39. typedef void cgrpc_operations;
  40. typedef void cgrpc_server;
  41. /** Result of a grpc call. If the caller satisfies the prerequisites of a
  42. particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
  43. Receiving any other value listed here is an indication of a bug in the
  44. caller. */
  45. typedef enum grpc_call_error {
  46. /** everything went ok */
  47. GRPC_CALL_OK = 0,
  48. /** something failed, we don't know what */
  49. GRPC_CALL_ERROR,
  50. /** this method is not available on the server */
  51. GRPC_CALL_ERROR_NOT_ON_SERVER,
  52. /** this method is not available on the client */
  53. GRPC_CALL_ERROR_NOT_ON_CLIENT,
  54. /** this method must be called before server_accept */
  55. GRPC_CALL_ERROR_ALREADY_ACCEPTED,
  56. /** this method must be called before invoke */
  57. GRPC_CALL_ERROR_ALREADY_INVOKED,
  58. /** this method must be called after invoke */
  59. GRPC_CALL_ERROR_NOT_INVOKED,
  60. /** this call is already finished
  61. (writes_done or write_status has already been called) */
  62. GRPC_CALL_ERROR_ALREADY_FINISHED,
  63. /** there is already an outstanding read/write operation on the call */
  64. GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
  65. /** the flags value was illegal for this call */
  66. GRPC_CALL_ERROR_INVALID_FLAGS,
  67. /** invalid metadata was passed to this call */
  68. GRPC_CALL_ERROR_INVALID_METADATA,
  69. /** invalid message was passed to this call */
  70. GRPC_CALL_ERROR_INVALID_MESSAGE,
  71. /** completion queue for notification has not been registered with the
  72. server */
  73. GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE,
  74. /** this batch of operations leads to more operations than allowed */
  75. GRPC_CALL_ERROR_BATCH_TOO_BIG,
  76. /** payload type requested is not the type registered */
  77. GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH
  78. } grpc_call_error;
  79. /** The type of completion (for grpc_event) */
  80. typedef enum grpc_completion_type {
  81. /** Shutting down */
  82. GRPC_QUEUE_SHUTDOWN,
  83. /** No event before timeout */
  84. GRPC_QUEUE_TIMEOUT,
  85. /** Operation completion */
  86. GRPC_OP_COMPLETE
  87. } grpc_completion_type;
  88. /** Connectivity state of a channel. */
  89. typedef enum grpc_connectivity_state {
  90. /** channel has just been initialized */
  91. GRPC_CHANNEL_INIT = -1,
  92. /** channel is idle */
  93. GRPC_CHANNEL_IDLE,
  94. /** channel is connecting */
  95. GRPC_CHANNEL_CONNECTING,
  96. /** channel is ready for work */
  97. GRPC_CHANNEL_READY,
  98. /** channel has seen a failure but expects to recover */
  99. GRPC_CHANNEL_TRANSIENT_FAILURE,
  100. /** channel has seen a failure that it cannot recover from */
  101. GRPC_CHANNEL_SHUTDOWN
  102. } grpc_connectivity_state;
  103. typedef struct grpc_event {
  104. /** The type of the completion. */
  105. grpc_completion_type type;
  106. /** non-zero if the operation was successful, 0 upon failure.
  107. Only GRPC_OP_COMPLETE can succeed or fail. */
  108. int success;
  109. /** The tag passed to grpc_call_start_batch etc to start this operation.
  110. Only GRPC_OP_COMPLETE has a tag. */
  111. void *tag;
  112. } grpc_event;
  113. typedef enum grpc_arg_type {
  114. GRPC_ARG_STRING,
  115. GRPC_ARG_INTEGER,
  116. GRPC_ARG_POINTER
  117. } grpc_arg_type;
  118. typedef struct grpc_arg_pointer_vtable {
  119. void *(*copy)(void *p);
  120. void (*destroy)(void *p);
  121. int (*cmp)(void *p, void *q);
  122. } grpc_arg_pointer_vtable;
  123. typedef struct grpc_arg {
  124. grpc_arg_type type;
  125. char *key;
  126. union grpc_arg_value {
  127. char *string;
  128. int integer;
  129. struct grpc_arg_pointer {
  130. void *p;
  131. const grpc_arg_pointer_vtable *vtable;
  132. } pointer;
  133. } value;
  134. } grpc_arg;
  135. #endif
  136. // directly expose a few grpc library functions
  137. void grpc_init(void);
  138. void grpc_shutdown(void);
  139. const char *grpc_version_string(void);
  140. const char *grpc_g_stands_for(void);
  141. char *gpr_strdup(const char *src);
  142. void gpr_free(void *p);
  143. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  144. void grpc_completion_queue_destroy(cgrpc_completion_queue *cq);
  145. // helper
  146. void cgrpc_free_copied_string(char *string);
  147. // channel support
  148. cgrpc_channel *cgrpc_channel_create(const char *address,
  149. grpc_arg *args,
  150. int num_args);
  151. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  152. const char *pem_root_certs,
  153. const char *client_certs,
  154. const char *client_key,
  155. grpc_arg *args,
  156. int num_args);
  157. void cgrpc_channel_destroy(cgrpc_channel *channel);
  158. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  159. const char *method,
  160. const char *host,
  161. double timeout);
  162. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel);
  163. grpc_connectivity_state cgrpc_channel_check_connectivity_state(
  164. cgrpc_channel *channel, int try_to_connect);
  165. void cgrpc_channel_watch_connectivity_state(cgrpc_channel *channel,
  166. cgrpc_completion_queue *completion_queue,
  167. grpc_connectivity_state last_observed_state,
  168. double deadline,
  169. void *tag);
  170. // server support
  171. cgrpc_server *cgrpc_server_create(const char *address);
  172. cgrpc_server *cgrpc_server_create_secure(const char *address,
  173. const char *private_key,
  174. const char *cert_chain,
  175. const char *root_certs,
  176. int force_client_auth);
  177. void cgrpc_server_stop(cgrpc_server *server);
  178. void cgrpc_server_destroy(cgrpc_server *s);
  179. void cgrpc_server_start(cgrpc_server *s);
  180. cgrpc_completion_queue *cgrpc_server_get_completion_queue(cgrpc_server *s);
  181. // completion queues
  182. cgrpc_completion_queue *cgrpc_completion_queue_create_for_next();
  183. grpc_event cgrpc_completion_queue_get_next_event(cgrpc_completion_queue *cq,
  184. double timeout);
  185. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  186. void cgrpc_completion_queue_shutdown(cgrpc_completion_queue *cq);
  187. // server request handlers
  188. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server);
  189. void cgrpc_handler_destroy(cgrpc_handler *h);
  190. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h);
  191. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h);
  192. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  193. cgrpc_metadata_array *metadata,
  194. void *tag);
  195. char *cgrpc_handler_copy_host(cgrpc_handler *h);
  196. char *cgrpc_handler_copy_method(cgrpc_handler *h);
  197. char *cgrpc_handler_call_peer(cgrpc_handler *h);
  198. // call support
  199. void cgrpc_call_destroy(cgrpc_call *call);
  200. grpc_call_error cgrpc_call_perform(cgrpc_call *call, cgrpc_operations *operations, void *tag);
  201. void cgrpc_call_cancel(cgrpc_call *call);
  202. // operations
  203. cgrpc_operations *cgrpc_operations_create(void);
  204. void cgrpc_operations_destroy(cgrpc_operations *operations);
  205. void cgrpc_operations_reserve_space_for_operations(cgrpc_operations *call, int max_operations);
  206. void cgrpc_operations_add_operation(cgrpc_operations *call, cgrpc_observer *observer);
  207. // metadata support
  208. cgrpc_metadata_array *cgrpc_metadata_array_create(void);
  209. void cgrpc_metadata_array_destroy(cgrpc_metadata_array *array);
  210. void cgrpc_metadata_array_unref_fields(cgrpc_metadata_array *array);
  211. size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array);
  212. char *cgrpc_metadata_array_copy_key_at_index(cgrpc_metadata_array *array, size_t index);
  213. char *cgrpc_metadata_array_copy_value_at_index(cgrpc_metadata_array *array, size_t index);
  214. void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *dest, cgrpc_metadata_array *src);
  215. cgrpc_metadata_array *cgrpc_metadata_array_copy(cgrpc_metadata_array *src);
  216. void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value);
  217. // mutex support
  218. cgrpc_mutex *cgrpc_mutex_create(void);
  219. void cgrpc_mutex_destroy(cgrpc_mutex *mu);
  220. void cgrpc_mutex_lock(cgrpc_mutex *mu);
  221. void cgrpc_mutex_unlock(cgrpc_mutex *mu);
  222. // byte buffer support
  223. void cgrpc_byte_buffer_destroy(cgrpc_byte_buffer *bb);
  224. cgrpc_byte_buffer *cgrpc_byte_buffer_create_by_copying_data(const void *source, size_t len);
  225. const void *cgrpc_byte_buffer_copy_data(cgrpc_byte_buffer *bb, size_t *length);
  226. // event support
  227. void *cgrpc_event_tag(grpc_event ev);
  228. // observers
  229. // constructors
  230. cgrpc_observer_send_initial_metadata *cgrpc_observer_create_send_initial_metadata(cgrpc_metadata_array *metadata);
  231. cgrpc_observer_send_message *cgrpc_observer_create_send_message(void);
  232. cgrpc_observer_send_close_from_client *cgrpc_observer_create_send_close_from_client(void);
  233. cgrpc_observer_send_status_from_server *cgrpc_observer_create_send_status_from_server(cgrpc_metadata_array *metadata);
  234. cgrpc_observer_recv_initial_metadata *cgrpc_observer_create_recv_initial_metadata(void);
  235. cgrpc_observer_recv_message *cgrpc_observer_create_recv_message(void);
  236. cgrpc_observer_recv_status_on_client *cgrpc_observer_create_recv_status_on_client(void);
  237. cgrpc_observer_recv_close_on_server *cgrpc_observer_create_recv_close_on_server(void);
  238. // destructor
  239. void cgrpc_observer_destroy(cgrpc_observer *observer);
  240. // GRPC_OP_SEND_INITIAL_METADATA
  241. // GRPC_OP_SEND_MESSAGE
  242. void cgrpc_observer_send_message_set_message(cgrpc_observer_send_message *observer,
  243. cgrpc_byte_buffer *message);
  244. // GRPC_OP_SEND_CLOSE_FROM_CLIENT
  245. // -- no special handlers --
  246. // GRPC_OP_SEND_STATUS_FROM_SERVER
  247. void cgrpc_observer_send_status_from_server_set_status
  248. (cgrpc_observer_send_status_from_server *observer,
  249. int status);
  250. void cgrpc_observer_send_status_from_server_set_status_details
  251. (cgrpc_observer_send_status_from_server *observer,
  252. const char *statusDetails);
  253. // GRPC_OP_RECV_INITIAL_METADATA
  254. cgrpc_metadata_array *cgrpc_observer_recv_initial_metadata_get_metadata
  255. (cgrpc_observer_recv_initial_metadata *observer);
  256. // GRPC_OP_RECV_MESSAGE
  257. cgrpc_byte_buffer *cgrpc_observer_recv_message_get_message
  258. (cgrpc_observer_recv_message *observer);
  259. // GRPC_OP_RECV_STATUS_ON_CLIENT
  260. cgrpc_metadata_array *cgrpc_observer_recv_status_on_client_get_metadata
  261. (cgrpc_observer_recv_status_on_client *observer);
  262. long cgrpc_observer_recv_status_on_client_get_status
  263. (cgrpc_observer_recv_status_on_client *observer);
  264. char *cgrpc_observer_recv_status_on_client_copy_status_details
  265. (cgrpc_observer_recv_status_on_client *observer);
  266. // GRPC_OP_RECV_CLOSE_ON_SERVER
  267. int cgrpc_observer_recv_close_on_server_was_cancelled
  268. (cgrpc_observer_recv_close_on_server *observer);
  269. #endif