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 22 #include <grpc/support/port_platform.h> 23 24 #include <grpc/grpc.h> 25 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" 29 30 /* Forward decl of grpc_tcp_server */ 31 typedef struct grpc_tcp_server grpc_tcp_server; 32 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(). */ 38 unsigned port_index; 39 unsigned fd_index; 40 } grpc_tcp_server_acceptor; 41 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); 47 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, 54 void* cb_arg); 55 grpc_error* (*add_port)(grpc_tcp_server* s, const grpc_resolved_address* addr, 56 int* out_port); 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; 65 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); 72 73 /* Start listening to bound ports */ 74 void grpc_tcp_server_start(grpc_tcp_server* server, grpc_pollset** pollsets, 75 size_t pollset_count, 76 grpc_tcp_server_cb on_accept_cb, void* cb_arg); 77 78 /* Add a port to the server, returning the newly allocated port on success, or 79 -1 on failure. 80 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, 89 int* out_port); 90 91 /* Number of fds at the given port_index, or 0 if port_index is out of 92 bounds. */ 93 unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server* s, unsigned port_index); 94 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, 100 unsigned fd_index); 101 102 /* Ref s and return s. */ 103 grpc_tcp_server* grpc_tcp_server_ref(grpc_tcp_server* s); 104 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); 110 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); 114 115 /* Shutdown the fds of listeners. */ 116 void grpc_tcp_server_shutdown_listeners(grpc_tcp_server* s); 117 118 void grpc_tcp_server_global_init(); 119 120 void grpc_set_tcp_server_impl(grpc_tcp_server_vtable* impl); 121 122 #endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_H */ 123