blob: 437a94beff85afb796e82ad5b139b231ddd8c118 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_IOMGR_TCP_SERVER_H
35#define GRPC_CORE_LIB_IOMGR_TCP_SERVER_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Craig Tilleref962642016-05-18 22:57:17 -070037#include <grpc/grpc.h>
38
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/iomgr/closure.h"
40#include "src/core/lib/iomgr/endpoint.h"
murgatroid997871f732016-09-23 13:49:05 -070041#include "src/core/lib/iomgr/resolve_address.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042
43/* Forward decl of grpc_tcp_server */
44typedef struct grpc_tcp_server grpc_tcp_server;
45
Dan Born96d9dc32016-02-02 14:57:36 -080046typedef struct grpc_tcp_server_acceptor {
Dan Born5d81d152016-01-12 20:29:29 -080047 /* 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 Born96d9dc32016-02-02 14:57:36 -080053} grpc_tcp_server_acceptor;
Dan Born5d81d152016-01-12 20:29:29 -080054
Mark D. Rotheed38152016-12-08 13:59:13 -080055/* Called for newly connected TCP connections.
56 Takes ownership of acceptor. */
Craig Tillera82950e2015-09-22 12:33:20 -070057typedef void (*grpc_tcp_server_cb)(grpc_exec_ctx *exec_ctx, void *arg,
Dan Bornfa6b6062016-01-08 21:01:59 -080058 grpc_endpoint *ep,
Craig Tiller418a8212016-05-16 16:27:51 -070059 grpc_pollset *accepting_pollset,
Dan Born5d81d152016-01-12 20:29:29 -080060 grpc_tcp_server_acceptor *acceptor);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
Dan Bornfa6b6062016-01-08 21:01:59 -080062/* 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 Born96d9dc32016-02-02 14:57:36 -080064 grpc_tcp_server_unref() when the ref count reaches zero. */
Craig Tillere34c2852016-09-23 09:43:32 -070065grpc_error *grpc_tcp_server_create(grpc_exec_ctx *exec_ctx,
66 grpc_closure *shutdown_complete,
Craig Tilleref962642016-05-18 22:57:17 -070067 const grpc_channel_args *args,
Craig Tiller0b5857f2016-05-04 10:58:06 -070068 grpc_tcp_server **server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069
70/* Start listening to bound ports */
Craig Tillera82950e2015-09-22 12:33:20 -070071void 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 Nobleb7ebd3b2014-11-26 16:33:03 -080074
Dan Bornfa6b6062016-01-08 21:01:59 -080075/* Add a port to the server, returning the newly allocated port on success, or
76 -1 on failure.
nnoble0c475f02014-12-05 15:37:39 -080077
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 Noble5eb4e1c2015-11-18 17:19:45 -080081 but not dualstack sockets. */
ctiller570d1f42015-01-12 16:29:52 -080082/* 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 */
murgatroid997871f732016-09-23 13:49:05 -070084grpc_error *grpc_tcp_server_add_port(grpc_tcp_server *s,
85 const grpc_resolved_address *addr,
86 int *out_port);
nnoble0c475f02014-12-05 15:37:39 -080087
Dan Bornfa6b6062016-01-08 21:01:59 -080088/* Number of fds at the given port_index, or 0 if port_index is out of
89 bounds. */
Dan Born5d81d152016-01-12 20:29:29 -080090unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server *s, unsigned port_index);
nnoble0c475f02014-12-05 15:37:39 -080091
Dan Bornfa6b6062016-01-08 21:01:59 -080092/* 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 Born96d9dc32016-02-02 14:57:36 -080095 cleaned up when the ref count reaches zero. */
Dan Born5d81d152016-01-12 20:29:29 -080096int grpc_tcp_server_port_fd(grpc_tcp_server *s, unsigned port_index,
97 unsigned fd_index);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098
Dan Bornfa6b6062016-01-08 21:01:59 -080099/* Ref s and return s. */
100grpc_tcp_server *grpc_tcp_server_ref(grpc_tcp_server *s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101
Dan Born9c12bc22016-01-13 16:52:20 -0800102/* 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. */
105void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server *s,
106 grpc_closure *shutdown_starting);
107
Dan Born966a4482016-09-26 15:51:42 -0700108/* If the refcount drops to zero, enqueue calls on exec_ctx to
109 shutdown_listeners and delete s. */
Dan Bornfa6b6062016-01-08 21:01:59 -0800110void grpc_tcp_server_unref(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s);
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800111
yang-g9275d402016-07-11 16:51:39 -0700112/* Shutdown the fds of listeners. */
113void grpc_tcp_server_shutdown_listeners(grpc_exec_ctx *exec_ctx,
114 grpc_tcp_server *s);
115
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700116#endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_H */