• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #ifndef GRPC_SRC_CORE_LIB_IOMGR_TCP_SERVER_H
20 #define GRPC_SRC_CORE_LIB_IOMGR_TCP_SERVER_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <vector>
25 
26 #include <grpc/event_engine/endpoint_config.h>
27 #include <grpc/grpc.h>
28 #include <grpc/impl/grpc_types.h>
29 
30 #include "src/core/lib/iomgr/closure.h"
31 #include "src/core/lib/iomgr/endpoint.h"
32 #include "src/core/lib/iomgr/resolve_address.h"
33 
34 // Forward decl of grpc_tcp_server
35 typedef struct grpc_tcp_server grpc_tcp_server;
36 
37 typedef struct grpc_tcp_server_acceptor {
38   // grpc_tcp_server_cb functions share a ref on from_server that is valid
39   // until the function returns.
40   grpc_tcp_server* from_server;
41   // Indices that may be passed to grpc_tcp_server_port_fd().
42   unsigned port_index;
43   unsigned fd_index;
44   // Data when the connection is passed to tcp_server from external.
45   bool external_connection;
46   int listener_fd;
47   grpc_byte_buffer* pending_data;
48 } grpc_tcp_server_acceptor;
49 
50 // Called for newly connected TCP connections.
51 // Takes ownership of acceptor.
52 typedef void (*grpc_tcp_server_cb)(void* arg, grpc_endpoint* ep,
53                                    grpc_pollset* accepting_pollset,
54                                    grpc_tcp_server_acceptor* acceptor);
55 namespace grpc_core {
56 // An interface for a handler to take a externally connected fd as a internal
57 // connection.
58 class TcpServerFdHandler {
59  public:
60   virtual ~TcpServerFdHandler() = default;
61   virtual void Handle(int listener_fd, int fd,
62                       grpc_byte_buffer* pending_read) = 0;
63 };
64 }  // namespace grpc_core
65 
66 typedef struct grpc_tcp_server_vtable {
67   grpc_error_handle (*create)(
68       grpc_closure* shutdown_complete,
69       const grpc_event_engine::experimental::EndpointConfig& config,
70       grpc_tcp_server_cb on_accept_cb, void* cb_arg, grpc_tcp_server** server);
71   void (*start)(grpc_tcp_server* server,
72                 const std::vector<grpc_pollset*>* pollsets);
73   grpc_error_handle (*add_port)(grpc_tcp_server* s,
74                                 const grpc_resolved_address* addr,
75                                 int* out_port);
76   grpc_core::TcpServerFdHandler* (*create_fd_handler)(grpc_tcp_server* s);
77   unsigned (*port_fd_count)(grpc_tcp_server* s, unsigned port_index);
78   int (*port_fd)(grpc_tcp_server* s, unsigned port_index, unsigned fd_index);
79   grpc_tcp_server* (*ref)(grpc_tcp_server* s);
80   void (*shutdown_starting_add)(grpc_tcp_server* s,
81                                 grpc_closure* shutdown_starting);
82   void (*unref)(grpc_tcp_server* s);
83   void (*shutdown_listeners)(grpc_tcp_server* s);
84   int (*pre_allocated_fd)(grpc_tcp_server* s);
85   void (*set_pre_allocated_fd)(grpc_tcp_server* s, int fd);
86 } grpc_tcp_server_vtable;
87 
88 // Create a server, initially not bound to any ports. The caller owns one ref.
89 // If shutdown_complete is not NULL, it will be used by
90 // grpc_tcp_server_unref() when the ref count reaches zero.
91 // Takes ownership of the slice_allocator_factory.
92 grpc_error_handle grpc_tcp_server_create(
93     grpc_closure* shutdown_complete,
94     const grpc_event_engine::experimental::EndpointConfig& config,
95     grpc_tcp_server_cb on_accept_cb, void* cb_arg, grpc_tcp_server** server);
96 
97 // Start listening to bound ports
98 void grpc_tcp_server_start(grpc_tcp_server* server,
99                            const std::vector<grpc_pollset*>* pollsets);
100 
101 // Add a port to the server, returning the newly allocated port on success, or
102 // -1 on failure.
103 
104 // The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
105 // both IPv4 and IPv6 connections, but :: is the preferred style.  This usually
106 // creates one socket, but possibly two on systems which support IPv6,
107 // but not dualstack sockets.
108 // TODO(ctiller): deprecate this, and make grpc_tcp_server_add_ports to handle
109 //                all of the multiple socket port matching logic in one place
110 grpc_error_handle grpc_tcp_server_add_port(grpc_tcp_server* s,
111                                            const grpc_resolved_address* addr,
112                                            int* out_port);
113 
114 // Create and return a TcpServerFdHandler so that it can be used by upper layer
115 // to hand over an externally connected fd to the grpc server.
116 grpc_core::TcpServerFdHandler* grpc_tcp_server_create_fd_handler(
117     grpc_tcp_server* s);
118 
119 // Number of fds at the given port_index, or 0 if port_index is out of
120 // bounds.
121 unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server* s, unsigned port_index);
122 
123 // Returns the file descriptor of the Mth (fd_index) listening socket of the Nth
124 // (port_index) call to add_port() on this server, or -1 if the indices are out
125 // of bounds. The file descriptor remains owned by the server, and will be
126 // cleaned up when the ref count reaches zero.
127 int grpc_tcp_server_port_fd(grpc_tcp_server* s, unsigned port_index,
128                             unsigned fd_index);
129 
130 // Ref s and return s.
131 grpc_tcp_server* grpc_tcp_server_ref(grpc_tcp_server* s);
132 
133 // shutdown_starting is called when ref count has reached zero and the server is
134 // about to be destroyed. The server will be deleted after it returns. Calling
135 // grpc_tcp_server_ref() from it has no effect.
136 void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server* s,
137                                            grpc_closure* shutdown_starting);
138 
139 // If the refcount drops to zero, enqueue calls on exec_ctx to
140 // shutdown_listeners and delete s.
141 void grpc_tcp_server_unref(grpc_tcp_server* s);
142 
143 // Shutdown the fds of listeners.
144 void grpc_tcp_server_shutdown_listeners(grpc_tcp_server* s);
145 
146 /* Get pre-allocated FD for server. -1 if none is set */
147 int grpc_tcp_server_pre_allocated_fd(grpc_tcp_server* s);
148 
149 /* Set pre-allocated FD for server */
150 void grpc_tcp_server_set_pre_allocated_fd(grpc_tcp_server* s, int fd);
151 
152 void grpc_tcp_server_global_init();
153 
154 void grpc_set_tcp_server_impl(grpc_tcp_server_vtable* impl);
155 
156 #endif  // GRPC_SRC_CORE_LIB_IOMGR_TCP_SERVER_H
157