channel.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <assert.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. cgrpc_channel *cgrpc_channel_create(const char *address,
  25. grpc_arg *args,
  26. int num_args) {
  27. cgrpc_channel *c = (cgrpc_channel *) malloc(sizeof (cgrpc_channel));
  28. grpc_channel_args channel_args;
  29. channel_args.args = args;
  30. channel_args.num_args = num_args;
  31. c->channel = grpc_insecure_channel_create(address, &channel_args, NULL);
  32. c->completion_queue = grpc_completion_queue_create_for_next(NULL);
  33. return c;
  34. }
  35. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  36. const char *pem_root_certs,
  37. grpc_arg *args,
  38. int num_args) {
  39. cgrpc_channel *c = (cgrpc_channel *) malloc(sizeof (cgrpc_channel));
  40. grpc_channel_args channel_args;
  41. channel_args.args = args;
  42. channel_args.num_args = num_args;
  43. grpc_channel_credentials *creds = grpc_ssl_credentials_create(pem_root_certs, NULL, NULL);
  44. c->channel = grpc_secure_channel_create(creds, address, &channel_args, NULL);
  45. c->completion_queue = grpc_completion_queue_create_for_next(NULL);
  46. return c;
  47. }
  48. void cgrpc_channel_destroy(cgrpc_channel *c) {
  49. grpc_channel_destroy(c->channel);
  50. c->channel = NULL;
  51. free(c);
  52. }
  53. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  54. const char *method,
  55. const char *host,
  56. double timeout) {
  57. // create call
  58. grpc_slice host_slice = grpc_slice_from_copied_string(host);
  59. grpc_slice method_slice = grpc_slice_from_copied_string(method);
  60. gpr_timespec deadline = cgrpc_deadline_in_seconds_from_now(timeout);
  61. // The resulting call will have a retain call of +1. We'll release it in `cgrpc_call_destroy()`.
  62. grpc_call *channel_call = grpc_channel_create_call(channel->channel,
  63. NULL,
  64. GRPC_PROPAGATE_DEFAULTS,
  65. channel->completion_queue,
  66. method_slice,
  67. &host_slice,
  68. deadline,
  69. NULL);
  70. grpc_slice_unref(host_slice);
  71. grpc_slice_unref(method_slice);
  72. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  73. memset(call, 0, sizeof(cgrpc_call));
  74. call->call = channel_call;
  75. return call;
  76. }
  77. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel) {
  78. return channel->completion_queue;
  79. }
  80. grpc_connectivity_state cgrpc_channel_check_connectivity_state(cgrpc_channel *channel, int try_to_connect) {
  81. return grpc_channel_check_connectivity_state(channel->channel, try_to_connect);
  82. }
  83. void cgrpc_channel_watch_connectivity_state(cgrpc_channel *channel, cgrpc_completion_queue *completion_queue, grpc_connectivity_state last_observed_state, double deadline, void *tag) {
  84. gpr_timespec deadline_seconds = cgrpc_deadline_in_seconds_from_now(deadline);
  85. return grpc_channel_watch_connectivity_state(channel->channel, last_observed_state, deadline_seconds, completion_queue, tag);
  86. }