blob: 9a390699b4586767e67983b241ca2782995eff28 [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"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
42/* Forward decl of grpc_tcp_server */
43typedef struct grpc_tcp_server grpc_tcp_server;
44
Dan Born96d9dc32016-02-02 14:57:36 -080045typedef struct grpc_tcp_server_acceptor {
Dan Born5d81d152016-01-12 20:29:29 -080046 /* 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 Born96d9dc32016-02-02 14:57:36 -080052} grpc_tcp_server_acceptor;
Dan Born5d81d152016-01-12 20:29:29 -080053
54/* Called for newly connected TCP connections. */
Craig Tillera82950e2015-09-22 12:33:20 -070055typedef void (*grpc_tcp_server_cb)(grpc_exec_ctx *exec_ctx, void *arg,
Dan Bornfa6b6062016-01-08 21:01:59 -080056 grpc_endpoint *ep,
Craig Tiller418a8212016-05-16 16:27:51 -070057 grpc_pollset *accepting_pollset,
Dan Born5d81d152016-01-12 20:29:29 -080058 grpc_tcp_server_acceptor *acceptor);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059
Dan Bornfa6b6062016-01-08 21:01:59 -080060/* 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 Born96d9dc32016-02-02 14:57:36 -080062 grpc_tcp_server_unref() when the ref count reaches zero. */
Craig Tiller0b5857f2016-05-04 10:58:06 -070063grpc_error *grpc_tcp_server_create(grpc_closure *shutdown_complete,
Craig Tilleref962642016-05-18 22:57:17 -070064 const grpc_channel_args *args,
Craig Tiller0b5857f2016-05-04 10:58:06 -070065 grpc_tcp_server **server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066
67/* Start listening to bound ports */
Craig Tillera82950e2015-09-22 12:33:20 -070068void 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 Nobleb7ebd3b2014-11-26 16:33:03 -080071
Dan Bornfa6b6062016-01-08 21:01:59 -080072/* Add a port to the server, returning the newly allocated port on success, or
73 -1 on failure.
nnoble0c475f02014-12-05 15:37:39 -080074
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 Noble5eb4e1c2015-11-18 17:19:45 -080078 but not dualstack sockets. */
ctiller570d1f42015-01-12 16:29:52 -080079/* 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 Tillerc027e772016-05-03 16:27:00 -070081grpc_error *grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr,
82 size_t addr_len, int *out_port);
nnoble0c475f02014-12-05 15:37:39 -080083
Dan Bornfa6b6062016-01-08 21:01:59 -080084/* Number of fds at the given port_index, or 0 if port_index is out of
85 bounds. */
Dan Born5d81d152016-01-12 20:29:29 -080086unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server *s, unsigned port_index);
nnoble0c475f02014-12-05 15:37:39 -080087
Dan Bornfa6b6062016-01-08 21:01:59 -080088/* 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 Born96d9dc32016-02-02 14:57:36 -080091 cleaned up when the ref count reaches zero. */
Dan Born5d81d152016-01-12 20:29:29 -080092int grpc_tcp_server_port_fd(grpc_tcp_server *s, unsigned port_index,
93 unsigned fd_index);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080094
Dan Bornfa6b6062016-01-08 21:01:59 -080095/* Ref s and return s. */
96grpc_tcp_server *grpc_tcp_server_ref(grpc_tcp_server *s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097
Dan Born9c12bc22016-01-13 16:52:20 -080098/* 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. */
101void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server *s,
102 grpc_closure *shutdown_starting);
103
Dan Born966a4482016-09-26 15:51:42 -0700104/* If the refcount drops to zero, enqueue calls on exec_ctx to
105 shutdown_listeners and delete s. */
Dan Bornfa6b6062016-01-08 21:01:59 -0800106void grpc_tcp_server_unref(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s);
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800107
yang-g9275d402016-07-11 16:51:39 -0700108/* Shutdown the fds of listeners. */
109void grpc_tcp_server_shutdown_listeners(grpc_exec_ctx *exec_ctx,
110 grpc_tcp_server *s);
111
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700112#endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_H */