GRPC C++  1.39.1
tcp_server_utils_posix.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 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_CORE_LIB_IOMGR_TCP_SERVER_UTILS_POSIX_H
20 #define GRPC_CORE_LIB_IOMGR_TCP_SERVER_UTILS_POSIX_H
21 
23 
28 
29 /* one listening port */
30 typedef struct grpc_tcp_listener {
31  int fd;
35  int port;
36  unsigned port_index;
37  unsigned fd_index;
40  struct grpc_tcp_listener* next;
41  /* sibling is a linked list of all listeners for a given port. add_port and
42  clone_port place all new listeners in the same sibling list. A member of
43  the 'sibling' list is also a member of the 'next' list. The head of each
44  sibling list has is_sibling==0, and subsequent members of sibling lists
45  have is_sibling==1. is_sibling allows separate sibling lists to be
46  identified while iterating through 'next'. */
50 
51 /* the overall server */
52 struct grpc_tcp_server {
54  /* Called whenever accept() succeeds on a server port. */
56  void* on_accept_cb_arg;
57 
59 
60  /* active port count: how many ports are actually still listening */
61  size_t active_ports;
62  /* destroyed port count: how many ports are completely destroyed */
64 
65  /* is this server shutting down? */
66  bool shutdown;
67  /* have listeners been shutdown? */
69  /* use SO_REUSEPORT */
70  bool so_reuseport;
71  /* expand wildcard addresses to a list of all local addresses */
73 
74  /* linked list of server ports */
77  unsigned nports;
78 
79  /* List of closures passed to shutdown_starting_add(). */
81 
82  /* shutdown callback */
84 
85  /* all pollsets interested in new connections. The object pointed at is not
86  * owned by this struct */
87  const std::vector<grpc_pollset*>* pollsets;
88 
89  /* next pollset to assign a channel to */
91 
92  /* channel args for this server */
94 
95  /* a handler for external connections, owned */
97 };
98 
99 /* If successful, add a listener to \a s for \a addr, set \a dsmode for the
100  socket, and return the \a listener. */
102  const grpc_resolved_address* addr,
103  unsigned port_index,
104  unsigned fd_index,
105  grpc_dualstack_mode* dsmode,
106  grpc_tcp_listener** listener);
107 
108 /* Get all addresses assigned to network interfaces on the machine and create a
109  listener for each. requested_port is the port to use for every listener, or 0
110  to select one random port that will be used for every listener. Set *out_port
111  to the port selected. Return GRPC_ERROR_NONE only if all listeners were
112  added. */
114  unsigned port_index,
115  int requested_port,
116  int* out_port);
117 
118 /* Prepare a recently-created socket for listening. */
120  grpc_tcp_server*, int fd, const grpc_resolved_address* addr,
121  bool so_reuseport, int* port);
122 /* Ruturn true if the platform supports ifaddrs */
124 
125 #endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_UTILS_POSIX_H */
Definition: tcp_server.h:57
struct grpc_fd grpc_fd
Definition: ev_posix.h:44
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:30
pthread_mutex_t gpr_mu
Definition: sync_posix.h:45
grpc_dualstack_mode
Definition: socket_utils_posix.h:116
Definition: sync_generic.h:36
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Definition: closure.h:41
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
Definition: error_internal.h:41
Definition: resolve_address.h:44
Definition: tcp_server_custom.cc:45
grpc_closure destroyed_closure
Definition: tcp_server_utils_posix.h:39
struct grpc_tcp_listener * next
Definition: tcp_server_custom.cc:53
unsigned fd_index
Definition: tcp_server_utils_posix.h:37
grpc_resolved_address addr
Definition: tcp_server_utils_posix.h:34
struct grpc_tcp_listener * sibling
Definition: tcp_server_utils_posix.h:47
int port
Definition: tcp_server_custom.cc:48
grpc_fd * emfd
Definition: tcp_server_utils_posix.h:32
grpc_tcp_server * server
Definition: tcp_server_custom.cc:46
int is_sibling
Definition: tcp_server_utils_posix.h:48
grpc_closure read_closure
Definition: tcp_server_utils_posix.h:38
int fd
Definition: tcp_server_utils_posix.h:31
unsigned port_index
Definition: tcp_server_custom.cc:47
Definition: tcp_server_custom.cc:58
grpc_tcp_listener * tail
Definition: tcp_server_custom.cc:69
gpr_refcount refs
Definition: tcp_server_custom.cc:59
grpc_channel_args * channel_args
Definition: tcp_server_utils_posix.h:93
grpc_closure_list shutdown_starting
Definition: tcp_server_custom.cc:72
bool shutdown_listeners
Definition: tcp_server_utils_posix.h:68
bool expand_wildcard_addrs
Definition: tcp_server_utils_posix.h:72
size_t destroyed_ports
Definition: tcp_server_utils_posix.h:63
void * on_accept_cb_arg
Definition: tcp_server_custom.cc:63
const std::vector< grpc_pollset * > * pollsets
Definition: tcp_server_utils_posix.h:87
gpr_mu mu
Definition: tcp_server_utils_posix.h:58
unsigned nports
Definition: tcp_server_utils_posix.h:77
size_t active_ports
Definition: tcp_server_utils_posix.h:61
bool so_reuseport
Definition: tcp_server_custom.cc:78
grpc_closure * shutdown_complete
Definition: tcp_server_custom.cc:75
grpc_core::TcpServerFdHandler * fd_handler
Definition: tcp_server_utils_posix.h:96
gpr_atm next_pollset_to_assign
Definition: tcp_server_utils_posix.h:90
grpc_tcp_server_cb on_accept_cb
Definition: tcp_server_custom.cc:62
bool shutdown
Definition: tcp_server_custom.cc:77
grpc_tcp_listener * head
Definition: tcp_server_custom.cc:68
void(* grpc_tcp_server_cb)(void *arg, grpc_endpoint *ep, grpc_pollset *accepting_pollset, grpc_tcp_server_acceptor *acceptor)
Definition: tcp_server.h:51
grpc_error_handle grpc_tcp_server_prepare_socket(grpc_tcp_server *, int fd, const grpc_resolved_address *addr, bool so_reuseport, int *port)
grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server *s, unsigned port_index, int requested_port, int *out_port)
grpc_error_handle grpc_tcp_server_add_addr(grpc_tcp_server *s, const grpc_resolved_address *addr, unsigned port_index, unsigned fd_index, grpc_dualstack_mode *dsmode, grpc_tcp_listener **listener)
bool grpc_tcp_server_have_ifaddrs(void)
struct grpc_tcp_listener grpc_tcp_listener