cgrpc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #endif
  114. // directly expose a few grpc library functions
  115. void grpc_init(void);
  116. void grpc_shutdown(void);
  117. const char *grpc_version_string(void);
  118. const char *grpc_g_stands_for(void);
  119. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  120. void grpc_completion_queue_destroy(cgrpc_completion_queue *cq);
  121. // helper
  122. void cgrpc_free_copied_string(char *string);
  123. // channel support
  124. cgrpc_channel *cgrpc_channel_create(const char *address);
  125. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  126. const char *pem_root_certs,
  127. const char *host);
  128. void cgrpc_channel_destroy(cgrpc_channel *channel);
  129. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  130. const char *method,
  131. const char *host,
  132. double timeout);
  133. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel);
  134. grpc_connectivity_state cgrpc_channel_check_connectivity_state(
  135. cgrpc_channel *channel, int try_to_connect);
  136. void cgrpc_channel_watch_connectivity_state(cgrpc_channel *channel,
  137. cgrpc_completion_queue * completion_queue,
  138. grpc_connectivity_state last_observed_state,
  139. double deadline,
  140. void *tag);
  141. // server support
  142. cgrpc_server *cgrpc_server_create(const char *address);
  143. cgrpc_server *cgrpc_server_create_secure(const char *address,
  144. const char *private_key,
  145. const char *cert_chain);
  146. void cgrpc_server_stop(cgrpc_server *server);
  147. void cgrpc_server_destroy(cgrpc_server *s);
  148. void cgrpc_server_start(cgrpc_server *s);
  149. cgrpc_completion_queue *cgrpc_server_get_completion_queue(cgrpc_server *s);
  150. // completion queues
  151. cgrpc_completion_queue *cgrpc_completion_queue_create_for_next();
  152. grpc_event cgrpc_completion_queue_get_next_event(cgrpc_completion_queue *cq,
  153. double timeout);
  154. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  155. void cgrpc_completion_queue_shutdown(cgrpc_completion_queue *cq);
  156. // server request handlers
  157. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server);
  158. void cgrpc_handler_destroy(cgrpc_handler *h);
  159. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h);
  160. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h);
  161. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  162. cgrpc_metadata_array *metadata,
  163. long tag);
  164. char *cgrpc_handler_copy_host(cgrpc_handler *h);
  165. char *cgrpc_handler_copy_method(cgrpc_handler *h);
  166. char *cgrpc_handler_call_peer(cgrpc_handler *h);
  167. // call support
  168. void cgrpc_call_destroy(cgrpc_call *call);
  169. grpc_call_error cgrpc_call_perform(cgrpc_call *call, cgrpc_operations *operations, int64_t tag);
  170. void cgrpc_call_cancel(cgrpc_call *call);
  171. // operations
  172. cgrpc_operations *cgrpc_operations_create(void);
  173. void cgrpc_operations_destroy(cgrpc_operations *operations);
  174. void cgrpc_operations_reserve_space_for_operations(cgrpc_operations *call, int max_operations);
  175. void cgrpc_operations_add_operation(cgrpc_operations *call, cgrpc_observer *observer);
  176. // metadata support
  177. cgrpc_metadata_array *cgrpc_metadata_array_create(void);
  178. void cgrpc_metadata_array_destroy(cgrpc_metadata_array *array);
  179. size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array);
  180. char *cgrpc_metadata_array_copy_key_at_index(cgrpc_metadata_array *array, size_t index);
  181. char *cgrpc_metadata_array_copy_value_at_index(cgrpc_metadata_array *array, size_t index);
  182. void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *dest, cgrpc_metadata_array *src);
  183. void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value);
  184. // mutex support
  185. cgrpc_mutex *cgrpc_mutex_create(void);
  186. void cgrpc_mutex_destroy(cgrpc_mutex *mu);
  187. void cgrpc_mutex_lock(cgrpc_mutex *mu);
  188. void cgrpc_mutex_unlock(cgrpc_mutex *mu);
  189. // byte buffer support
  190. void cgrpc_byte_buffer_destroy(cgrpc_byte_buffer *bb);
  191. cgrpc_byte_buffer *cgrpc_byte_buffer_create_by_copying_data(const void *source, size_t len);
  192. const void *cgrpc_byte_buffer_copy_data(cgrpc_byte_buffer *bb, size_t *length);
  193. // event support
  194. int64_t cgrpc_event_tag(grpc_event ev);
  195. // observers
  196. // constructors
  197. cgrpc_observer_send_initial_metadata *cgrpc_observer_create_send_initial_metadata(cgrpc_metadata_array *metadata);
  198. cgrpc_observer_send_message *cgrpc_observer_create_send_message(void);
  199. cgrpc_observer_send_close_from_client *cgrpc_observer_create_send_close_from_client(void);
  200. cgrpc_observer_send_status_from_server *cgrpc_observer_create_send_status_from_server(cgrpc_metadata_array *metadata);
  201. cgrpc_observer_recv_initial_metadata *cgrpc_observer_create_recv_initial_metadata(void);
  202. cgrpc_observer_recv_message *cgrpc_observer_create_recv_message(void);
  203. cgrpc_observer_recv_status_on_client *cgrpc_observer_create_recv_status_on_client(void);
  204. cgrpc_observer_recv_close_on_server *cgrpc_observer_create_recv_close_on_server(void);
  205. // destructor
  206. void cgrpc_observer_destroy(cgrpc_observer *observer);
  207. // GRPC_OP_SEND_INITIAL_METADATA
  208. // GRPC_OP_SEND_MESSAGE
  209. void cgrpc_observer_send_message_set_message(cgrpc_observer_send_message *observer,
  210. cgrpc_byte_buffer *message);
  211. // GRPC_OP_SEND_CLOSE_FROM_CLIENT
  212. // -- no special handlers --
  213. // GRPC_OP_SEND_STATUS_FROM_SERVER
  214. void cgrpc_observer_send_status_from_server_set_status
  215. (cgrpc_observer_send_status_from_server *observer,
  216. int status);
  217. void cgrpc_observer_send_status_from_server_set_status_details
  218. (cgrpc_observer_send_status_from_server *observer,
  219. const char *statusDetails);
  220. // GRPC_OP_RECV_INITIAL_METADATA
  221. cgrpc_metadata_array *cgrpc_observer_recv_initial_metadata_get_metadata
  222. (cgrpc_observer_recv_initial_metadata *observer);
  223. // GRPC_OP_RECV_MESSAGE
  224. cgrpc_byte_buffer *cgrpc_observer_recv_message_get_message
  225. (cgrpc_observer_recv_message *observer);
  226. // GRPC_OP_RECV_STATUS_ON_CLIENT
  227. cgrpc_metadata_array *cgrpc_observer_recv_status_on_client_get_metadata
  228. (cgrpc_observer_recv_status_on_client *observer);
  229. long cgrpc_observer_recv_status_on_client_get_status
  230. (cgrpc_observer_recv_status_on_client *observer);
  231. char *cgrpc_observer_recv_status_on_client_copy_status_details
  232. (cgrpc_observer_recv_status_on_client *observer);
  233. // GRPC_OP_RECV_CLOSE_ON_SERVER
  234. int cgrpc_observer_recv_close_on_server_was_cancelled
  235. (cgrpc_observer_recv_close_on_server *observer);
  236. #endif