3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_IOMGR_TCP_SERVER_H
20 #define GRPC_CORE_LIB_IOMGR_TCP_SERVER_H
22 #include <grpc/support/port_platform.h>
24 #include <grpc/grpc.h>
26 #include "src/core/lib/iomgr/closure.h"
27 #include "src/core/lib/iomgr/endpoint.h"
28 #include "src/core/lib/iomgr/resolve_address.h"
30 /* Forward decl of grpc_tcp_server */
31 typedef struct grpc_tcp_server grpc_tcp_server;
33 typedef struct grpc_tcp_server_acceptor {
34 /* grpc_tcp_server_cb functions share a ref on from_server that is valid
35 until the function returns. */
36 grpc_tcp_server* from_server;
37 /* Indices that may be passed to grpc_tcp_server_port_fd(). */
40 } grpc_tcp_server_acceptor;
42 /* Called for newly connected TCP connections.
43 Takes ownership of acceptor. */
44 typedef void (*grpc_tcp_server_cb)(void* arg, grpc_endpoint* ep,
45 grpc_pollset* accepting_pollset,
46 grpc_tcp_server_acceptor* acceptor);
48 typedef struct grpc_tcp_server_vtable {
49 grpc_error* (*create)(grpc_closure* shutdown_complete,
50 const grpc_channel_args* args,
51 grpc_tcp_server** server);
52 void (*start)(grpc_tcp_server* server, grpc_pollset** pollsets,
53 size_t pollset_count, grpc_tcp_server_cb on_accept_cb,
55 grpc_error* (*add_port)(grpc_tcp_server* s, const grpc_resolved_address* addr,
57 unsigned (*port_fd_count)(grpc_tcp_server* s, unsigned port_index);
58 int (*port_fd)(grpc_tcp_server* s, unsigned port_index, unsigned fd_index);
59 grpc_tcp_server* (*ref)(grpc_tcp_server* s);
60 void (*shutdown_starting_add)(grpc_tcp_server* s,
61 grpc_closure* shutdown_starting);
62 void (*unref)(grpc_tcp_server* s);
63 void (*shutdown_listeners)(grpc_tcp_server* s);
64 } grpc_tcp_server_vtable;
66 /* Create a server, initially not bound to any ports. The caller owns one ref.
67 If shutdown_complete is not NULL, it will be used by
68 grpc_tcp_server_unref() when the ref count reaches zero. */
69 grpc_error* grpc_tcp_server_create(grpc_closure* shutdown_complete,
70 const grpc_channel_args* args,
71 grpc_tcp_server** server);
73 /* Start listening to bound ports */
74 void grpc_tcp_server_start(grpc_tcp_server* server, grpc_pollset** pollsets,
76 grpc_tcp_server_cb on_accept_cb, void* cb_arg);
78 /* Add a port to the server, returning the newly allocated port on success, or
81 The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
82 both IPv4 and IPv6 connections, but :: is the preferred style. This usually
83 creates one socket, but possibly two on systems which support IPv6,
84 but not dualstack sockets. */
85 /* TODO(ctiller): deprecate this, and make grpc_tcp_server_add_ports to handle
86 all of the multiple socket port matching logic in one place */
87 grpc_error* grpc_tcp_server_add_port(grpc_tcp_server* s,
88 const grpc_resolved_address* addr,
91 /* Number of fds at the given port_index, or 0 if port_index is out of
93 unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server* s, unsigned port_index);
95 /* Returns the file descriptor of the Mth (fd_index) listening socket of the Nth
96 (port_index) call to add_port() on this server, or -1 if the indices are out
97 of bounds. The file descriptor remains owned by the server, and will be
98 cleaned up when the ref count reaches zero. */
99 int grpc_tcp_server_port_fd(grpc_tcp_server* s, unsigned port_index,
102 /* Ref s and return s. */
103 grpc_tcp_server* grpc_tcp_server_ref(grpc_tcp_server* s);
105 /* shutdown_starting is called when ref count has reached zero and the server is
106 about to be destroyed. The server will be deleted after it returns. Calling
107 grpc_tcp_server_ref() from it has no effect. */
108 void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server* s,
109 grpc_closure* shutdown_starting);
111 /* If the refcount drops to zero, enqueue calls on exec_ctx to
112 shutdown_listeners and delete s. */
113 void grpc_tcp_server_unref(grpc_tcp_server* s);
115 /* Shutdown the fds of listeners. */
116 void grpc_tcp_server_shutdown_listeners(grpc_tcp_server* s);
118 void grpc_tcp_server_global_init();
120 void grpc_set_tcp_server_impl(grpc_tcp_server_vtable* impl);
122 #endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_H */