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