2
0

channel_shim.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "internal.h"
  17. #include "cgrpc.h"
  18. #include <grpc/support/string_util.h>
  19. #include <grpc/support/alloc.h>
  20. #include <stdlib.h>
  21. cgrpc_channel *cgrpc_channel_create(const char *address,
  22. grpc_arg *args,
  23. int num_args) {
  24. cgrpc_channel *c = (cgrpc_channel *) gpr_zalloc(sizeof (cgrpc_channel));
  25. grpc_channel_args channel_args;
  26. channel_args.args = args;
  27. channel_args.num_args = num_args;
  28. c->channel = grpc_insecure_channel_create(address, &channel_args, NULL);
  29. c->completion_queue = grpc_completion_queue_create_for_next(NULL);
  30. return c;
  31. }
  32. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  33. const char *pem_root_certs,
  34. const char *client_certs,
  35. const char *client_private_key,
  36. grpc_arg *args,
  37. int num_args) {
  38. cgrpc_channel *c = (cgrpc_channel *) gpr_zalloc(sizeof (cgrpc_channel));
  39. grpc_channel_args channel_args;
  40. channel_args.args = args;
  41. channel_args.num_args = num_args;
  42. grpc_ssl_pem_key_cert_pair client_credentials;
  43. grpc_ssl_pem_key_cert_pair *client_credentials_pointer = NULL;
  44. if (client_certs != NULL && client_private_key != NULL) {
  45. client_credentials.cert_chain = client_certs;
  46. client_credentials.private_key = client_private_key;
  47. client_credentials_pointer = &client_credentials;
  48. }
  49. grpc_channel_credentials *creds = grpc_ssl_credentials_create(pem_root_certs, client_credentials_pointer, NULL, NULL);
  50. c->channel = grpc_secure_channel_create(creds, address, &channel_args, NULL);
  51. c->completion_queue = grpc_completion_queue_create_for_next(NULL);
  52. return c;
  53. }
  54. cgrpc_channel *cgrpc_channel_create_google(const char *address,
  55. grpc_arg *args,
  56. int num_args) {
  57. cgrpc_channel *c = (cgrpc_channel *) gpr_zalloc(sizeof (cgrpc_channel));
  58. grpc_channel_args channel_args;
  59. channel_args.args = args;
  60. channel_args.num_args = num_args;
  61. grpc_channel_credentials *google_creds = grpc_google_default_credentials_create();
  62. c->channel = grpc_secure_channel_create(google_creds, address, &channel_args, NULL);
  63. c->completion_queue = grpc_completion_queue_create_for_next(NULL);
  64. return c;
  65. }
  66. void cgrpc_channel_destroy(cgrpc_channel *c) {
  67. grpc_channel_destroy(c->channel);
  68. c->channel = NULL;
  69. gpr_free(c);
  70. }
  71. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  72. const char *method,
  73. const char *host,
  74. double timeout) {
  75. // create call
  76. grpc_slice host_slice = grpc_slice_from_copied_string(host);
  77. grpc_slice method_slice = grpc_slice_from_copied_string(method);
  78. gpr_timespec deadline = cgrpc_deadline_in_seconds_from_now(timeout);
  79. // The resulting call will have a retain call of +1. We'll release it in `cgrpc_call_destroy()`.
  80. grpc_call *channel_call = grpc_channel_create_call(channel->channel,
  81. NULL,
  82. GRPC_PROPAGATE_DEFAULTS,
  83. channel->completion_queue,
  84. method_slice,
  85. &host_slice,
  86. deadline,
  87. NULL);
  88. grpc_slice_unref(host_slice);
  89. grpc_slice_unref(method_slice);
  90. cgrpc_call *call = (cgrpc_call *) gpr_zalloc(sizeof(cgrpc_call));
  91. call->call = channel_call;
  92. return call;
  93. }
  94. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel) {
  95. return channel->completion_queue;
  96. }
  97. grpc_connectivity_state cgrpc_channel_check_connectivity_state(cgrpc_channel *channel, int try_to_connect) {
  98. return grpc_channel_check_connectivity_state(channel->channel, try_to_connect);
  99. }
  100. void cgrpc_channel_watch_connectivity_state(cgrpc_channel *channel, cgrpc_completion_queue *completion_queue, grpc_connectivity_state last_observed_state, double deadline, void *tag) {
  101. gpr_timespec deadline_seconds = cgrpc_deadline_in_seconds_from_now(deadline);
  102. return grpc_channel_watch_connectivity_state(channel->channel, last_observed_state, deadline_seconds, completion_queue, tag);
  103. }