blob: f3c466a031d2ded7195ea229f60ba251d48aed2c [file] [log] [blame]
Robbie Shadea4e21a12015-07-10 14:16:31 -04001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Robbie Shadea4e21a12015-07-10 14:16:31 -04004 * 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_UDP_SERVER_H
35#define GRPC_CORE_LIB_IOMGR_UDP_SERVER_H
Robbie Shadea4e21a12015-07-10 14:16:31 -040036
Craig Tiller9533d042016-03-25 17:11:06 -070037#include "src/core/lib/iomgr/endpoint.h"
Craig Tiller8a034482016-03-28 16:09:04 -070038#include "src/core/lib/iomgr/ev_posix.h"
murgatroid99aa9c5782016-10-10 12:16:13 -070039#include "src/core/lib/iomgr/resolve_address.h"
Robbie Shadea4e21a12015-07-10 14:16:31 -040040
vjpaiaea32072016-03-18 12:22:59 -070041/* Forward decl of struct grpc_server */
42/* This is not typedef'ed to avoid a typedef-redefinition error */
43struct grpc_server;
Robbie Shade147fe702015-09-25 15:04:40 -040044
Robbie Shadea4e21a12015-07-10 14:16:31 -040045/* Forward decl of grpc_udp_server */
46typedef struct grpc_udp_server grpc_udp_server;
47
Robbie Shadea4e21a12015-07-10 14:16:31 -040048/* Called when data is available to read from the socket. */
Robbie Shadec6787b22015-10-07 10:13:53 -040049typedef void (*grpc_udp_server_read_cb)(grpc_exec_ctx *exec_ctx, grpc_fd *emfd,
vjpaiaea32072016-03-18 12:22:59 -070050 struct grpc_server *server);
Robbie Shadea4e21a12015-07-10 14:16:31 -040051
Robbie Shade9aa6f402016-05-12 13:28:04 -040052/* Called when the grpc_fd is about to be orphaned (and the FD closed). */
53typedef void (*grpc_udp_server_orphan_cb)(grpc_fd *emfd);
54
Robbie Shadea4e21a12015-07-10 14:16:31 -040055/* Create a server, initially not bound to any ports */
Craig Tillera82950e2015-09-22 12:33:20 -070056grpc_udp_server *grpc_udp_server_create(void);
Robbie Shadea4e21a12015-07-10 14:16:31 -040057
58/* Start listening to bound ports */
Robbie Shade109a8dc2015-09-25 15:18:49 -040059void grpc_udp_server_start(grpc_exec_ctx *exec_ctx, grpc_udp_server *udp_server,
Robbie Shade147fe702015-09-25 15:04:40 -040060 grpc_pollset **pollsets, size_t pollset_count,
vjpaiaea32072016-03-18 12:22:59 -070061 struct grpc_server *server);
Robbie Shadea4e21a12015-07-10 14:16:31 -040062
Robbie Shade956f1d32016-08-02 16:55:00 -040063int grpc_udp_server_get_fd(grpc_udp_server *s, unsigned port_index);
Robbie Shade3d4fc4a2015-08-04 19:20:00 -040064
Robbie Shadea4e21a12015-07-10 14:16:31 -040065/* Add a port to the server, returning port number on success, or negative
66 on failure.
67
68 The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
69 both IPv4 and IPv6 connections, but :: is the preferred style. This usually
70 creates one socket, but possibly two on systems which support IPv6,
71 but not dualstack sockets. */
72
73/* TODO(ctiller): deprecate this, and make grpc_udp_server_add_ports to handle
74 all of the multiple socket port matching logic in one place */
murgatroid9922d50e92016-10-12 09:54:49 -070075int grpc_udp_server_add_port(grpc_udp_server *s,
76 const grpc_resolved_address *addr,
murgatroid99aa9c5782016-10-10 12:16:13 -070077 grpc_udp_server_read_cb read_cb,
Robbie Shade9aa6f402016-05-12 13:28:04 -040078 grpc_udp_server_orphan_cb orphan_cb);
Robbie Shadea4e21a12015-07-10 14:16:31 -040079
Craig Tillera82950e2015-09-22 12:33:20 -070080void grpc_udp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_udp_server *server,
81 grpc_closure *on_done);
Robbie Shadea4e21a12015-07-10 14:16:31 -040082
Craig Tiller9a4dddd2016-03-25 17:08:13 -070083#endif /* GRPC_CORE_LIB_IOMGR_UDP_SERVER_H */