Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_IOMGR_TCP_SERVER_H |
| 35 | #define GRPC_CORE_LIB_IOMGR_TCP_SERVER_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 37 | #include <grpc/grpc.h> |
| 38 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 39 | #include "src/core/lib/iomgr/closure.h" |
| 40 | #include "src/core/lib/iomgr/endpoint.h" |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/resolve_address.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | |
| 43 | /* Forward decl of grpc_tcp_server */ |
| 44 | typedef struct grpc_tcp_server grpc_tcp_server; |
| 45 | |
Dan Born | 96d9dc3 | 2016-02-02 14:57:36 -0800 | [diff] [blame] | 46 | typedef struct grpc_tcp_server_acceptor { |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 47 | /* grpc_tcp_server_cb functions share a ref on from_server that is valid |
| 48 | until the function returns. */ |
| 49 | grpc_tcp_server *from_server; |
| 50 | /* Indices that may be passed to grpc_tcp_server_port_fd(). */ |
| 51 | unsigned port_index; |
| 52 | unsigned fd_index; |
Dan Born | 96d9dc3 | 2016-02-02 14:57:36 -0800 | [diff] [blame] | 53 | } grpc_tcp_server_acceptor; |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 54 | |
Mark D. Roth | eed3815 | 2016-12-08 13:59:13 -0800 | [diff] [blame] | 55 | /* Called for newly connected TCP connections. |
| 56 | Takes ownership of acceptor. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | typedef void (*grpc_tcp_server_cb)(grpc_exec_ctx *exec_ctx, void *arg, |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 58 | grpc_endpoint *ep, |
Craig Tiller | 418a821 | 2016-05-16 16:27:51 -0700 | [diff] [blame] | 59 | grpc_pollset *accepting_pollset, |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 60 | grpc_tcp_server_acceptor *acceptor); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 62 | /* Create a server, initially not bound to any ports. The caller owns one ref. |
| 63 | If shutdown_complete is not NULL, it will be used by |
Dan Born | 96d9dc3 | 2016-02-02 14:57:36 -0800 | [diff] [blame] | 64 | grpc_tcp_server_unref() when the ref count reaches zero. */ |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 65 | grpc_error *grpc_tcp_server_create(grpc_exec_ctx *exec_ctx, |
| 66 | grpc_closure *shutdown_complete, |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 67 | const grpc_channel_args *args, |
Craig Tiller | 0b5857f | 2016-05-04 10:58:06 -0700 | [diff] [blame] | 68 | grpc_tcp_server **server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | |
| 70 | /* Start listening to bound ports */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *server, |
| 72 | grpc_pollset **pollsets, size_t pollset_count, |
| 73 | grpc_tcp_server_cb on_accept_cb, void *cb_arg); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 75 | /* Add a port to the server, returning the newly allocated port on success, or |
| 76 | -1 on failure. |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 77 | |
| 78 | The :: and 0.0.0.0 wildcard addresses are treated identically, accepting |
| 79 | both IPv4 and IPv6 connections, but :: is the preferred style. This usually |
| 80 | creates one socket, but possibly two on systems which support IPv6, |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 81 | but not dualstack sockets. */ |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 82 | /* TODO(ctiller): deprecate this, and make grpc_tcp_server_add_ports to handle |
| 83 | all of the multiple socket port matching logic in one place */ |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 84 | grpc_error *grpc_tcp_server_add_port(grpc_tcp_server *s, |
| 85 | const grpc_resolved_address *addr, |
| 86 | int *out_port); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 87 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 88 | /* Number of fds at the given port_index, or 0 if port_index is out of |
| 89 | bounds. */ |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 90 | unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server *s, unsigned port_index); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 91 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 92 | /* Returns the file descriptor of the Mth (fd_index) listening socket of the Nth |
| 93 | (port_index) call to add_port() on this server, or -1 if the indices are out |
| 94 | of bounds. The file descriptor remains owned by the server, and will be |
Dan Born | 96d9dc3 | 2016-02-02 14:57:36 -0800 | [diff] [blame] | 95 | cleaned up when the ref count reaches zero. */ |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 96 | int grpc_tcp_server_port_fd(grpc_tcp_server *s, unsigned port_index, |
| 97 | unsigned fd_index); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 99 | /* Ref s and return s. */ |
| 100 | grpc_tcp_server *grpc_tcp_server_ref(grpc_tcp_server *s); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 102 | /* shutdown_starting is called when ref count has reached zero and the server is |
| 103 | about to be destroyed. The server will be deleted after it returns. Calling |
| 104 | grpc_tcp_server_ref() from it has no effect. */ |
| 105 | void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server *s, |
| 106 | grpc_closure *shutdown_starting); |
| 107 | |
Dan Born | 966a448 | 2016-09-26 15:51:42 -0700 | [diff] [blame] | 108 | /* If the refcount drops to zero, enqueue calls on exec_ctx to |
| 109 | shutdown_listeners and delete s. */ |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 110 | void grpc_tcp_server_unref(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 111 | |
yang-g | 9275d40 | 2016-07-11 16:51:39 -0700 | [diff] [blame] | 112 | /* Shutdown the fds of listeners. */ |
| 113 | void grpc_tcp_server_shutdown_listeners(grpc_exec_ctx *exec_ctx, |
| 114 | grpc_tcp_server *s); |
| 115 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 116 | #endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_H */ |