cgrpc.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. typedef struct grpc_event {
  89. /** The type of the completion. */
  90. grpc_completion_type type;
  91. /** non-zero if the operation was successful, 0 upon failure.
  92. Only GRPC_OP_COMPLETE can succeed or fail. */
  93. int success;
  94. /** The tag passed to grpc_call_start_batch etc to start this operation.
  95. Only GRPC_OP_COMPLETE has a tag. */
  96. void *tag;
  97. } grpc_event;
  98. #endif
  99. // directly expose a few grpc library functions
  100. void grpc_init(void);
  101. void grpc_shutdown(void);
  102. const char *grpc_version_string(void);
  103. const char *grpc_g_stands_for(void);
  104. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  105. void grpc_completion_queue_destroy(cgrpc_completion_queue *cq);
  106. // helper
  107. void cgrpc_free_copied_string(char *string);
  108. // channel support
  109. cgrpc_channel *cgrpc_channel_create(const char *address);
  110. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  111. const char *pem_root_certs,
  112. const char *host);
  113. void cgrpc_channel_destroy(cgrpc_channel *channel);
  114. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  115. const char *method,
  116. const char *host,
  117. double timeout);
  118. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel);
  119. // server support
  120. cgrpc_server *cgrpc_server_create(const char *address);
  121. cgrpc_server *cgrpc_server_create_secure(const char *address,
  122. const char *private_key,
  123. const char *cert_chain);
  124. void cgrpc_server_stop(cgrpc_server *server);
  125. void cgrpc_server_destroy(cgrpc_server *s);
  126. void cgrpc_server_start(cgrpc_server *s);
  127. cgrpc_completion_queue *cgrpc_server_get_completion_queue(cgrpc_server *s);
  128. // completion queues
  129. grpc_event cgrpc_completion_queue_get_next_event(cgrpc_completion_queue *cq,
  130. double timeout);
  131. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  132. void cgrpc_completion_queue_shutdown(cgrpc_completion_queue *cq);
  133. // server request handlers
  134. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server);
  135. void cgrpc_handler_destroy(cgrpc_handler *h);
  136. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h);
  137. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h);
  138. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  139. cgrpc_metadata_array *metadata,
  140. long tag);
  141. char *cgrpc_handler_copy_host(cgrpc_handler *h);
  142. char *cgrpc_handler_copy_method(cgrpc_handler *h);
  143. char *cgrpc_handler_call_peer(cgrpc_handler *h);
  144. // call support
  145. void cgrpc_call_destroy(cgrpc_call *call);
  146. grpc_call_error cgrpc_call_perform(cgrpc_call *call, cgrpc_operations *operations, int64_t tag);
  147. void cgrpc_call_cancel(cgrpc_call *call);
  148. // operations
  149. cgrpc_operations *cgrpc_operations_create(void);
  150. void cgrpc_operations_destroy(cgrpc_operations *operations);
  151. void cgrpc_operations_reserve_space_for_operations(cgrpc_operations *call, int max_operations);
  152. void cgrpc_operations_add_operation(cgrpc_operations *call, cgrpc_observer *observer);
  153. // metadata support
  154. cgrpc_metadata_array *cgrpc_metadata_array_create(void);
  155. void cgrpc_metadata_array_destroy(cgrpc_metadata_array *array);
  156. size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array);
  157. char *cgrpc_metadata_array_copy_key_at_index(cgrpc_metadata_array *array, size_t index);
  158. char *cgrpc_metadata_array_copy_value_at_index(cgrpc_metadata_array *array, size_t index);
  159. void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *dest, cgrpc_metadata_array *src);
  160. void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value);
  161. // mutex support
  162. cgrpc_mutex *cgrpc_mutex_create(void);
  163. void cgrpc_mutex_destroy(cgrpc_mutex *mu);
  164. void cgrpc_mutex_lock(cgrpc_mutex *mu);
  165. void cgrpc_mutex_unlock(cgrpc_mutex *mu);
  166. // byte buffer support
  167. void cgrpc_byte_buffer_destroy(cgrpc_byte_buffer *bb);
  168. cgrpc_byte_buffer *cgrpc_byte_buffer_create_by_copying_data(const void *source, size_t len);
  169. const void *cgrpc_byte_buffer_copy_data(cgrpc_byte_buffer *bb, size_t *length);
  170. // event support
  171. int64_t cgrpc_event_tag(grpc_event ev);
  172. // observers
  173. // constructors
  174. cgrpc_observer_send_initial_metadata *cgrpc_observer_create_send_initial_metadata(cgrpc_metadata_array *metadata);
  175. cgrpc_observer_send_message *cgrpc_observer_create_send_message(void);
  176. cgrpc_observer_send_close_from_client *cgrpc_observer_create_send_close_from_client(void);
  177. cgrpc_observer_send_status_from_server *cgrpc_observer_create_send_status_from_server(cgrpc_metadata_array *metadata);
  178. cgrpc_observer_recv_initial_metadata *cgrpc_observer_create_recv_initial_metadata(void);
  179. cgrpc_observer_recv_message *cgrpc_observer_create_recv_message(void);
  180. cgrpc_observer_recv_status_on_client *cgrpc_observer_create_recv_status_on_client(void);
  181. cgrpc_observer_recv_close_on_server *cgrpc_observer_create_recv_close_on_server(void);
  182. // destructor
  183. void cgrpc_observer_destroy(cgrpc_observer *observer);
  184. // GRPC_OP_SEND_INITIAL_METADATA
  185. // GRPC_OP_SEND_MESSAGE
  186. void cgrpc_observer_send_message_set_message(cgrpc_observer_send_message *observer,
  187. cgrpc_byte_buffer *message);
  188. // GRPC_OP_SEND_CLOSE_FROM_CLIENT
  189. // -- no special handlers --
  190. // GRPC_OP_SEND_STATUS_FROM_SERVER
  191. void cgrpc_observer_send_status_from_server_set_status
  192. (cgrpc_observer_send_status_from_server *observer,
  193. int status);
  194. void cgrpc_observer_send_status_from_server_set_status_details
  195. (cgrpc_observer_send_status_from_server *observer,
  196. const char *statusDetails);
  197. // GRPC_OP_RECV_INITIAL_METADATA
  198. cgrpc_metadata_array *cgrpc_observer_recv_initial_metadata_get_metadata
  199. (cgrpc_observer_recv_initial_metadata *observer);
  200. // GRPC_OP_RECV_MESSAGE
  201. cgrpc_byte_buffer *cgrpc_observer_recv_message_get_message
  202. (cgrpc_observer_recv_message *observer);
  203. // GRPC_OP_RECV_STATUS_ON_CLIENT
  204. cgrpc_metadata_array *cgrpc_observer_recv_status_on_client_get_metadata
  205. (cgrpc_observer_recv_status_on_client *observer);
  206. long cgrpc_observer_recv_status_on_client_get_status
  207. (cgrpc_observer_recv_status_on_client *observer);
  208. char *cgrpc_observer_recv_status_on_client_copy_status_details
  209. (cgrpc_observer_recv_status_on_client *observer);
  210. // GRPC_OP_RECV_CLOSE_ON_SERVER
  211. int cgrpc_observer_recv_close_on_server_was_cancelled
  212. (cgrpc_observer_recv_close_on_server *observer);
  213. #endif