handler.c 3.2 KB

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