handler_shim.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <assert.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server) {
  23. cgrpc_handler *handler = (cgrpc_handler *) malloc(sizeof (cgrpc_handler));
  24. memset(handler, 0, sizeof(cgrpc_handler));
  25. handler->server = server;
  26. grpc_metadata_array_init(&(handler->request_metadata_recv));
  27. grpc_call_details_init(&(handler->call_details));
  28. handler->completion_queue = grpc_completion_queue_create_for_next(NULL);
  29. return handler;
  30. }
  31. void cgrpc_handler_destroy(cgrpc_handler *h) {
  32. grpc_metadata_array_destroy(&(h->request_metadata_recv));
  33. grpc_call_details_destroy(&(h->call_details));
  34. if (h->server_call) {
  35. grpc_call_unref(h->server_call);
  36. }
  37. free(h);
  38. }
  39. char *cgrpc_handler_copy_host(cgrpc_handler *h) {
  40. size_t length = GRPC_SLICE_LENGTH(h->call_details.host);
  41. char *str = (char *) malloc(length + 1);
  42. memcpy(str, GRPC_SLICE_START_PTR(h->call_details.host), length);
  43. str[length] = 0;
  44. return str;
  45. }
  46. char *cgrpc_handler_copy_method(cgrpc_handler *h) {
  47. size_t length = GRPC_SLICE_LENGTH(h->call_details.method);
  48. char *str = (char *) malloc(length + 1);
  49. memcpy(str, GRPC_SLICE_START_PTR(h->call_details.method), length);
  50. str[length] = 0;
  51. return str;
  52. }
  53. char *cgrpc_handler_call_peer(cgrpc_handler *h) {
  54. return grpc_call_get_peer(h->server_call);
  55. }
  56. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h) {
  57. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  58. memset(call, 0, sizeof(cgrpc_call));
  59. call->call = h->server_call;
  60. if (call->call) {
  61. // This retain will be balanced by `cgrpc_call_destroy()`.
  62. grpc_call_ref(call->call);
  63. }
  64. return call;
  65. }
  66. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h) {
  67. return h->completion_queue;
  68. }
  69. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  70. cgrpc_metadata_array *metadata,
  71. void *tag) {
  72. if (h->server_call != NULL) {
  73. return GRPC_CALL_OK;
  74. }
  75. // This fills `h->server_call` with a call with retain count of +1.
  76. // We'll release that retain in `cgrpc_handler_destroy()`.
  77. return grpc_server_request_call(h->server->server,
  78. &(h->server_call),
  79. &(h->call_details),
  80. metadata,
  81. h->completion_queue,
  82. h->server->completion_queue,
  83. cgrpc_create_tag(tag));
  84. }