2
0

client.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "internal.h"
  34. #include "cgrpc.h"
  35. #include <grpc/support/string_util.h>
  36. #include <assert.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. cgrpc_client *cgrpc_client_create(const char *address) {
  41. cgrpc_client *c = (cgrpc_client *) malloc(sizeof (cgrpc_client));
  42. // create the client
  43. grpc_channel_args client_args;
  44. client_args.num_args = 0;
  45. c->client = grpc_insecure_channel_create(address, &client_args, NULL);
  46. c->completion_queue = grpc_completion_queue_create(NULL);
  47. return c;
  48. }
  49. cgrpc_client *cgrpc_client_create_secure(const char *address,
  50. const char *pem_root_certs,
  51. const char *host) {
  52. cgrpc_client *c = (cgrpc_client *) malloc(sizeof (cgrpc_client));
  53. // create the client
  54. int argCount = 2;
  55. grpc_channel_args *channelArgs = gpr_malloc(sizeof(grpc_channel_args));
  56. channelArgs->num_args = argCount;
  57. channelArgs->args = gpr_malloc(argCount * sizeof(grpc_arg));
  58. grpc_arg *arg = &channelArgs->args[0];
  59. arg->type = GRPC_ARG_STRING;
  60. arg->key = gpr_strdup("grpc.primary_user_agent");
  61. arg->value.string = gpr_strdup("grpc-swift/0.0.1");
  62. arg = &channelArgs->args[1];
  63. arg->type = GRPC_ARG_STRING;
  64. arg->key = gpr_strdup("grpc.ssl_target_name_override");
  65. arg->value.string = gpr_strdup(host);
  66. grpc_channel_credentials *creds = grpc_ssl_credentials_create(pem_root_certs, NULL, NULL);
  67. c->client = grpc_secure_channel_create(creds, address, channelArgs, NULL);
  68. c->completion_queue = grpc_completion_queue_create(NULL);
  69. return c;
  70. }
  71. void cgrpc_client_destroy(cgrpc_client *c) {
  72. grpc_channel_destroy(c->client);
  73. c->client = NULL;
  74. grpc_completion_queue_shutdown(c->completion_queue);
  75. cgrpc_completion_queue_drain(c->completion_queue);
  76. grpc_completion_queue_destroy(c->completion_queue);
  77. free(c);
  78. }
  79. cgrpc_call *cgrpc_client_create_call(cgrpc_client *client,
  80. const char *method,
  81. const char *host,
  82. double timeout) {
  83. // create call
  84. gpr_timespec deadline = cgrpc_deadline_in_seconds_from_now(timeout);
  85. grpc_call *client_call = grpc_channel_create_call(client->client,
  86. NULL,
  87. GRPC_PROPAGATE_DEFAULTS,
  88. client->completion_queue,
  89. method,
  90. host,
  91. deadline,
  92. NULL);
  93. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  94. memset(call, 0, sizeof(cgrpc_call));
  95. call->call = client_call;
  96. return call;
  97. }
  98. cgrpc_completion_queue *cgrpc_client_completion_queue(cgrpc_client *client) {
  99. return client->completion_queue;
  100. }