handler.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. cgrpc_completion_queue_drain(h->completion_queue);
  34. grpc_completion_queue_destroy(h->completion_queue);
  35. grpc_metadata_array_destroy(&(h->request_metadata_recv));
  36. grpc_call_details_destroy(&(h->call_details));
  37. if (h->server_call) {
  38. //grpc_call_destroy(h->server_call);
  39. }
  40. free(h);
  41. }
  42. const char *cgrpc_handler_host(cgrpc_handler *h) {
  43. return (const char *) GRPC_SLICE_START_PTR(h->call_details.host);
  44. }
  45. const char *cgrpc_handler_method(cgrpc_handler *h) {
  46. return (const char *) GRPC_SLICE_START_PTR(h->call_details.method);
  47. }
  48. const char *cgrpc_handler_call_peer(cgrpc_handler *h) {
  49. return grpc_call_get_peer(h->server_call);
  50. }
  51. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h) {
  52. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  53. memset(call, 0, sizeof(cgrpc_call));
  54. call->call = h->server_call;
  55. return call;
  56. }
  57. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h) {
  58. return h->completion_queue;
  59. }
  60. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  61. cgrpc_metadata_array *metadata,
  62. long tag) {
  63. return grpc_server_request_call(h->server->server,
  64. &(h->server_call),
  65. &(h->call_details),
  66. metadata,
  67. h->completion_queue,
  68. h->server->completion_queue,
  69. cgrpc_create_tag(tag));
  70. }