handler.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. char *cgrpc_handler_copy_host(cgrpc_handler *h) {
  43. size_t length = GRPC_SLICE_LENGTH(h->call_details.host);
  44. char *str = (char *) malloc(length + 1);
  45. memcpy(str, GRPC_SLICE_START_PTR(h->call_details.host), length);
  46. str[length] = 0;
  47. return str;
  48. }
  49. char *cgrpc_handler_copy_method(cgrpc_handler *h) {
  50. size_t length = GRPC_SLICE_LENGTH(h->call_details.method);
  51. char *str = (char *) malloc(length + 1);
  52. memcpy(str, GRPC_SLICE_START_PTR(h->call_details.method), length);
  53. str[length] = 0;
  54. return str;
  55. }
  56. char *cgrpc_handler_call_peer(cgrpc_handler *h) {
  57. return grpc_call_get_peer(h->server_call);
  58. }
  59. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h) {
  60. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  61. memset(call, 0, sizeof(cgrpc_call));
  62. call->call = h->server_call;
  63. return call;
  64. }
  65. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h) {
  66. return h->completion_queue;
  67. }
  68. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  69. cgrpc_metadata_array *metadata,
  70. long tag) {
  71. return grpc_server_request_call(h->server->server,
  72. &(h->server_call),
  73. &(h->call_details),
  74. metadata,
  75. h->completion_queue,
  76. h->server->completion_queue,
  77. cgrpc_create_tag(tag));
  78. }