cgrpc.h 9.7 KB

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