server_chttp2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. *
  3. * Copyright 2015, 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 <grpc/grpc.h>
  34. #include "src/core/channel/http_filter.h"
  35. #include "src/core/channel/http_server_filter.h"
  36. #include "src/core/iomgr/resolve_address.h"
  37. #include "src/core/iomgr/tcp_server.h"
  38. #include "src/core/surface/server.h"
  39. #include "src/core/transport/chttp2_transport.h"
  40. #include <grpc/support/alloc.h>
  41. #include <grpc/support/log.h>
  42. #include <grpc/support/useful.h>
  43. static grpc_transport_setup_result setup_transport(void *server,
  44. grpc_transport *transport,
  45. grpc_mdctx *mdctx) {
  46. static grpc_channel_filter const *extra_filters[] = {&grpc_http_server_filter,
  47. &grpc_http_filter};
  48. return grpc_server_setup_transport(server, transport, extra_filters,
  49. GPR_ARRAY_SIZE(extra_filters), mdctx);
  50. }
  51. static void new_transport(void *server, grpc_endpoint *tcp) {
  52. grpc_create_chttp2_transport(setup_transport, server,
  53. grpc_server_get_channel_args(server), tcp, NULL,
  54. 0, grpc_mdctx_create(), 0);
  55. }
  56. /* Server callback: start listening on our ports */
  57. static void start(grpc_server *server, void *tcpp, grpc_pollset **pollsets, size_t pollset_count) {
  58. grpc_tcp_server *tcp = tcpp;
  59. grpc_tcp_server_start(tcp, pollsets, pollset_count, new_transport, server);
  60. }
  61. /* Server callback: destroy the tcp listener (so we don't generate further
  62. callbacks) */
  63. static void destroy(grpc_server *server, void *tcpp) {
  64. grpc_tcp_server *tcp = tcpp;
  65. grpc_tcp_server_destroy(tcp);
  66. }
  67. int grpc_server_add_http2_port(grpc_server *server, const char *addr) {
  68. grpc_resolved_addresses *resolved = NULL;
  69. grpc_tcp_server *tcp = NULL;
  70. size_t i;
  71. unsigned count = 0;
  72. int port_num = -1;
  73. int port_temp;
  74. resolved = grpc_blocking_resolve_address(addr, "http");
  75. if (!resolved) {
  76. goto error;
  77. }
  78. tcp = grpc_tcp_server_create();
  79. if (!tcp) {
  80. goto error;
  81. }
  82. for (i = 0; i < resolved->naddrs; i++) {
  83. port_temp = grpc_tcp_server_add_port(
  84. tcp, (struct sockaddr *)&resolved->addrs[i].addr,
  85. resolved->addrs[i].len);
  86. if (port_temp >= 0) {
  87. if (port_num == -1) {
  88. port_num = port_temp;
  89. } else {
  90. GPR_ASSERT(port_num == port_temp);
  91. }
  92. count++;
  93. }
  94. }
  95. if (count == 0) {
  96. gpr_log(GPR_ERROR, "No address added out of total %d resolved",
  97. resolved->naddrs);
  98. goto error;
  99. }
  100. if (count != resolved->naddrs) {
  101. gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved",
  102. count, resolved->naddrs);
  103. }
  104. grpc_resolved_addresses_destroy(resolved);
  105. /* Register with the server only upon success */
  106. grpc_server_add_listener(server, tcp, start, destroy);
  107. return port_num;
  108. /* Error path: cleanup and return */
  109. error:
  110. if (resolved) {
  111. grpc_resolved_addresses_destroy(resolved);
  112. }
  113. if (tcp) {
  114. grpc_tcp_server_destroy(tcp);
  115. }
  116. return 0;
  117. }