tcp_server.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_CORE_LIB_IOMGR_TCP_SERVER_H
  19. #define GRPC_CORE_LIB_IOMGR_TCP_SERVER_H
  20. #include <grpc/support/port_platform.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/impl/codegen/grpc_types.h>
  23. #include "src/core/lib/iomgr/closure.h"
  24. #include "src/core/lib/iomgr/endpoint.h"
  25. #include "src/core/lib/iomgr/resolve_address.h"
  26. /* Forward decl of grpc_tcp_server */
  27. typedef struct grpc_tcp_server grpc_tcp_server;
  28. typedef struct grpc_tcp_server_acceptor {
  29. /* grpc_tcp_server_cb functions share a ref on from_server that is valid
  30. until the function returns. */
  31. grpc_tcp_server* from_server;
  32. /* Indices that may be passed to grpc_tcp_server_port_fd(). */
  33. unsigned port_index;
  34. unsigned fd_index;
  35. /* Data when the connection is passed to tcp_server from external. */
  36. bool external_connection;
  37. int listener_fd;
  38. grpc_byte_buffer* pending_data;
  39. } grpc_tcp_server_acceptor;
  40. /* Called for newly connected TCP connections.
  41. Takes ownership of acceptor. */
  42. typedef void (*grpc_tcp_server_cb)(void* arg, grpc_endpoint* ep,
  43. grpc_pollset* accepting_pollset,
  44. grpc_tcp_server_acceptor* acceptor);
  45. namespace grpc_core {
  46. // An interface for a handler to take a externally connected fd as a internal
  47. // connection.
  48. class TcpServerFdHandler {
  49. public:
  50. virtual ~TcpServerFdHandler() = default;
  51. virtual void Handle(int listener_fd, int fd,
  52. grpc_byte_buffer* pending_read) = 0;
  53. };
  54. } // namespace grpc_core
  55. typedef struct grpc_tcp_server_vtable {
  56. grpc_error* (*create)(grpc_closure* shutdown_complete,
  57. const grpc_channel_args* args,
  58. grpc_tcp_server** server);
  59. void (*start)(grpc_tcp_server* server, grpc_pollset** pollsets,
  60. size_t pollset_count, grpc_tcp_server_cb on_accept_cb,
  61. void* cb_arg);
  62. grpc_error* (*add_port)(grpc_tcp_server* s, const grpc_resolved_address* addr,
  63. int* out_port);
  64. grpc_core::TcpServerFdHandler* (*create_fd_handler)(grpc_tcp_server* s);
  65. unsigned (*port_fd_count)(grpc_tcp_server* s, unsigned port_index);
  66. int (*port_fd)(grpc_tcp_server* s, unsigned port_index, unsigned fd_index);
  67. grpc_tcp_server* (*ref)(grpc_tcp_server* s);
  68. void (*shutdown_starting_add)(grpc_tcp_server* s,
  69. grpc_closure* shutdown_starting);
  70. void (*unref)(grpc_tcp_server* s);
  71. void (*shutdown_listeners)(grpc_tcp_server* s);
  72. } grpc_tcp_server_vtable;
  73. /* Create a server, initially not bound to any ports. The caller owns one ref.
  74. If shutdown_complete is not NULL, it will be used by
  75. grpc_tcp_server_unref() when the ref count reaches zero. */
  76. grpc_error* grpc_tcp_server_create(grpc_closure* shutdown_complete,
  77. const grpc_channel_args* args,
  78. grpc_tcp_server** server);
  79. /* Start listening to bound ports */
  80. void grpc_tcp_server_start(grpc_tcp_server* server, grpc_pollset** pollsets,
  81. size_t pollset_count,
  82. grpc_tcp_server_cb on_accept_cb, void* cb_arg);
  83. /* Add a port to the server, returning the newly allocated port on success, or
  84. -1 on failure.
  85. The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
  86. both IPv4 and IPv6 connections, but :: is the preferred style. This usually
  87. creates one socket, but possibly two on systems which support IPv6,
  88. but not dualstack sockets. */
  89. /* TODO(ctiller): deprecate this, and make grpc_tcp_server_add_ports to handle
  90. all of the multiple socket port matching logic in one place */
  91. grpc_error* grpc_tcp_server_add_port(grpc_tcp_server* s,
  92. const grpc_resolved_address* addr,
  93. int* out_port);
  94. /* Create and return a TcpServerFdHandler so that it can be used by upper layer
  95. to hand over an externally connected fd to the grpc server. */
  96. grpc_core::TcpServerFdHandler* grpc_tcp_server_create_fd_handler(
  97. grpc_tcp_server* s);
  98. /* Number of fds at the given port_index, or 0 if port_index is out of
  99. bounds. */
  100. unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server* s, unsigned port_index);
  101. /* Returns the file descriptor of the Mth (fd_index) listening socket of the Nth
  102. (port_index) call to add_port() on this server, or -1 if the indices are out
  103. of bounds. The file descriptor remains owned by the server, and will be
  104. cleaned up when the ref count reaches zero. */
  105. int grpc_tcp_server_port_fd(grpc_tcp_server* s, unsigned port_index,
  106. unsigned fd_index);
  107. /* Ref s and return s. */
  108. grpc_tcp_server* grpc_tcp_server_ref(grpc_tcp_server* s);
  109. /* shutdown_starting is called when ref count has reached zero and the server is
  110. about to be destroyed. The server will be deleted after it returns. Calling
  111. grpc_tcp_server_ref() from it has no effect. */
  112. void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server* s,
  113. grpc_closure* shutdown_starting);
  114. /* If the refcount drops to zero, enqueue calls on exec_ctx to
  115. shutdown_listeners and delete s. */
  116. void grpc_tcp_server_unref(grpc_tcp_server* s);
  117. /* Shutdown the fds of listeners. */
  118. void grpc_tcp_server_shutdown_listeners(grpc_tcp_server* s);
  119. void grpc_tcp_server_global_init();
  120. void grpc_set_tcp_server_impl(grpc_tcp_server_vtable* impl);
  121. #endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_H */