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" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | |
| 42 | /* Forward decl of grpc_tcp_server */ |
| 43 | typedef struct grpc_tcp_server grpc_tcp_server; |
| 44 | |
Dan Born | 96d9dc3 | 2016-02-02 14:57:36 -0800 | [diff] [blame] | 45 | typedef struct grpc_tcp_server_acceptor { |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 46 | /* grpc_tcp_server_cb functions share a ref on from_server that is valid |
| 47 | until the function returns. */ |
| 48 | grpc_tcp_server *from_server; |
| 49 | /* Indices that may be passed to grpc_tcp_server_port_fd(). */ |
| 50 | unsigned port_index; |
| 51 | unsigned fd_index; |
Dan Born | 96d9dc3 | 2016-02-02 14:57:36 -0800 | [diff] [blame] | 52 | } grpc_tcp_server_acceptor; |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 53 | |
| 54 | /* Called for newly connected TCP connections. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 55 | 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] | 56 | grpc_endpoint *ep, |
Craig Tiller | 418a821 | 2016-05-16 16:27:51 -0700 | [diff] [blame] | 57 | grpc_pollset *accepting_pollset, |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 58 | grpc_tcp_server_acceptor *acceptor); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 60 | /* Create a server, initially not bound to any ports. The caller owns one ref. |
| 61 | If shutdown_complete is not NULL, it will be used by |
Dan Born | 96d9dc3 | 2016-02-02 14:57:36 -0800 | [diff] [blame] | 62 | grpc_tcp_server_unref() when the ref count reaches zero. */ |
Craig Tiller | 0b5857f | 2016-05-04 10:58:06 -0700 | [diff] [blame] | 63 | grpc_error *grpc_tcp_server_create(grpc_closure *shutdown_complete, |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 64 | const grpc_channel_args *args, |
Craig Tiller | 0b5857f | 2016-05-04 10:58:06 -0700 | [diff] [blame] | 65 | grpc_tcp_server **server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | |
| 67 | /* Start listening to bound ports */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *server, |
| 69 | grpc_pollset **pollsets, size_t pollset_count, |
| 70 | grpc_tcp_server_cb on_accept_cb, void *cb_arg); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 72 | /* Add a port to the server, returning the newly allocated port on success, or |
| 73 | -1 on failure. |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 74 | |
| 75 | The :: and 0.0.0.0 wildcard addresses are treated identically, accepting |
| 76 | both IPv4 and IPv6 connections, but :: is the preferred style. This usually |
| 77 | creates one socket, but possibly two on systems which support IPv6, |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 78 | but not dualstack sockets. */ |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 79 | /* TODO(ctiller): deprecate this, and make grpc_tcp_server_add_ports to handle |
| 80 | all of the multiple socket port matching logic in one place */ |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 81 | grpc_error *grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, |
| 82 | size_t addr_len, int *out_port); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 83 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 84 | /* Number of fds at the given port_index, or 0 if port_index is out of |
| 85 | bounds. */ |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 86 | 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] | 87 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 88 | /* Returns the file descriptor of the Mth (fd_index) listening socket of the Nth |
| 89 | (port_index) call to add_port() on this server, or -1 if the indices are out |
| 90 | 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] | 91 | cleaned up when the ref count reaches zero. */ |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 92 | int grpc_tcp_server_port_fd(grpc_tcp_server *s, unsigned port_index, |
| 93 | unsigned fd_index); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 95 | /* Ref s and return s. */ |
| 96 | grpc_tcp_server *grpc_tcp_server_ref(grpc_tcp_server *s); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 98 | /* shutdown_starting is called when ref count has reached zero and the server is |
| 99 | about to be destroyed. The server will be deleted after it returns. Calling |
| 100 | grpc_tcp_server_ref() from it has no effect. */ |
| 101 | void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server *s, |
| 102 | grpc_closure *shutdown_starting); |
| 103 | |
Dan Born | 966a448 | 2016-09-26 15:51:42 -0700 | [diff] [blame] | 104 | /* If the refcount drops to zero, enqueue calls on exec_ctx to |
| 105 | shutdown_listeners and delete s. */ |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 106 | 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] | 107 | |
yang-g | 9275d40 | 2016-07-11 16:51:39 -0700 | [diff] [blame] | 108 | /* Shutdown the fds of listeners. */ |
| 109 | void grpc_tcp_server_shutdown_listeners(grpc_exec_ctx *exec_ctx, |
| 110 | grpc_tcp_server *s); |
| 111 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 112 | #endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_H */ |