GRPC Core  9.0.0
udp_server.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_IOMGR_UDP_SERVER_H
20 #define GRPC_CORE_LIB_IOMGR_UDP_SERVER_H
21 
23 
27 
28 /* Forward decl of struct grpc_server */
29 /* This is not typedef'ed to avoid a typedef-redefinition error */
30 struct grpc_server;
31 
32 /* Forward decl of grpc_udp_server */
34 
35 /* An interface associated with a socket. udp server delivers I/O event on that
36  * socket to the subclass of this interface which is created through
37  * GrpcUdpHandlerFactory.
38  * Its implementation should do the real IO work, e.g. read packet and write. */
40  public:
41  GrpcUdpHandler(grpc_fd* /* emfd */, void* /* user_data */) {}
42  virtual ~GrpcUdpHandler() {}
43 
44  // Interfaces to be implemented by subclasses to do the actual setup/tear down
45  // or I/O.
46 
47  // Called when data is available to read from the socket. Returns true if
48  // there is more data to read after this call.
49  virtual bool Read() = 0;
50  // Called when socket becomes write unblocked. The given closure should be
51  // scheduled when the socket becomes blocked next time.
52  virtual void OnCanWrite(void* user_data,
53  grpc_closure* notify_on_write_closure) = 0;
54  // Called before the gRPC FD is orphaned. Notify udp server to continue
55  // orphaning fd by scheduling the given closure, afterwards the associated fd
56  // will be closed.
57  virtual void OnFdAboutToOrphan(grpc_closure* orphan_fd_closure,
58  void* user_data) = 0;
59 };
60 
62  public:
64  /* Called when start to listen on a socket.
65  * Return an instance of the implementation of GrpcUdpHandler interface which
66  * will process I/O events for this socket from now on. */
67  virtual GrpcUdpHandler* CreateUdpHandler(grpc_fd* emfd, void* user_data) = 0;
68  virtual void DestroyUdpHandler(GrpcUdpHandler* handler) = 0;
69 };
70 
71 /* Create a server, initially not bound to any ports */
73 
74 /* Start listening to bound ports. user_data is passed to callbacks. */
75 void grpc_udp_server_start(grpc_udp_server* udp_server, grpc_pollset** pollsets,
76  size_t pollset_count, void* user_data);
77 
78 int grpc_udp_server_get_fd(grpc_udp_server* s, unsigned port_index);
79 
80 /* Add a port to the server, returning port number on success, or negative
81  on failure.
82 
83  Create |num_listeners| sockets for given address to listen on using
84  SO_REUSEPORT if supported.
85 
86  The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
87  both IPv4 and IPv6 connections, but :: is the preferred style. This usually
88  creates |num_listeners| sockets, but possibly 2 * |num_listeners| on systems
89  which support IPv6, but not dualstack sockets. */
90 
91 /* TODO(ctiller): deprecate this, and make grpc_udp_server_add_ports to handle
92  all of the multiple socket port matching logic in one place */
94  const grpc_resolved_address* addr,
95  int rcv_buf_size, int snd_buf_size,
96  GrpcUdpHandlerFactory* handler_factory,
97  size_t num_listeners);
98 
100 
101 #endif /* GRPC_CORE_LIB_IOMGR_UDP_SERVER_H */
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Definition: udp_server.h:61
virtual void OnFdAboutToOrphan(grpc_closure *orphan_fd_closure, void *user_data)=0
Definition: resolve_address.h:44
void grpc_udp_server_start(grpc_udp_server *udp_server, grpc_pollset **pollsets, size_t pollset_count, void *user_data)
virtual void OnCanWrite(void *user_data, grpc_closure *notify_on_write_closure)=0
grpc_udp_server * grpc_udp_server_create(const grpc_channel_args *args)
void grpc_udp_server_destroy(grpc_udp_server *server, grpc_closure *on_done)
Definition: pollset_custom.cc:40
virtual bool Read()=0
int grpc_udp_server_get_fd(grpc_udp_server *s, unsigned port_index)
struct grpc_fd grpc_fd
Definition: ev_posix.h:44
GrpcUdpHandler(grpc_fd *, void *)
Definition: udp_server.h:41
virtual ~GrpcUdpHandlerFactory()
Definition: udp_server.h:63
virtual GrpcUdpHandler * CreateUdpHandler(grpc_fd *emfd, void *user_data)=0
Definition: udp_server.h:39
int grpc_udp_server_add_port(grpc_udp_server *s, const grpc_resolved_address *addr, int rcv_buf_size, int snd_buf_size, GrpcUdpHandlerFactory *handler_factory, size_t num_listeners)
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
struct grpc_udp_server grpc_udp_server
Definition: udp_server.h:33
virtual ~GrpcUdpHandler()
Definition: udp_server.h:42
virtual void DestroyUdpHandler(GrpcUdpHandler *handler)=0
Definition: server.cc:222