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