GRPC C++  1.39.1
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 
24 #include <vector>
25 
29 
30 /* Forward decl of struct grpc_server */
31 /* This is not typedef'ed to avoid a typedef-redefinition error */
32 struct grpc_server;
33 
34 /* Forward decl of grpc_udp_server */
35 typedef struct grpc_udp_server grpc_udp_server;
36 
37 /* An interface associated with a socket. udp server delivers I/O event on that
38  * socket to the subclass of this interface which is created through
39  * GrpcUdpHandlerFactory.
40  * Its implementation should do the real IO work, e.g. read packet and write. */
42  public:
43  GrpcUdpHandler(grpc_fd* /* emfd */, void* /* user_data */) {}
44  virtual ~GrpcUdpHandler() {}
45 
46  // Interfaces to be implemented by subclasses to do the actual setup/tear down
47  // or I/O.
48 
49  // Called when data is available to read from the socket. Returns true if
50  // there is more data to read after this call.
51  virtual bool Read() = 0;
52  // Called when socket becomes write unblocked. The given closure should be
53  // scheduled when the socket becomes blocked next time.
54  virtual void OnCanWrite(void* user_data,
55  grpc_closure* notify_on_write_closure) = 0;
56  // Called before the gRPC FD is orphaned. Notify udp server to continue
57  // orphaning fd by scheduling the given closure, afterwards the associated fd
58  // will be closed.
59  virtual void OnFdAboutToOrphan(grpc_closure* orphan_fd_closure,
60  void* user_data) = 0;
61 };
62 
64  public:
66  /* Called when start to listen on a socket.
67  * Return an instance of the implementation of GrpcUdpHandler interface which
68  * will process I/O events for this socket from now on. */
69  virtual GrpcUdpHandler* CreateUdpHandler(grpc_fd* emfd, void* user_data) = 0;
70  virtual void DestroyUdpHandler(GrpcUdpHandler* handler) = 0;
71 };
72 
73 /* Create a server, initially not bound to any ports */
75 
76 /* Start listening to bound ports. user_data is passed to callbacks. */
78  const std::vector<grpc_pollset*>* pollsets,
79  void* user_data);
80 
81 int grpc_udp_server_get_fd(grpc_udp_server* s, unsigned port_index);
82 
83 /* Add a port to the server, returning port number on success, or negative
84  on failure.
85 
86  Create |num_listeners| sockets for given address to listen on using
87  SO_REUSEPORT if supported.
88 
89  The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
90  both IPv4 and IPv6 connections, but :: is the preferred style. This usually
91  creates |num_listeners| sockets, but possibly 2 * |num_listeners| on systems
92  which support IPv6, but not dualstack sockets. */
93 
94 /* TODO(ctiller): deprecate this, and make grpc_udp_server_add_ports to handle
95  all of the multiple socket port matching logic in one place */
97  int rcv_buf_size, int snd_buf_size,
98  GrpcUdpHandlerFactory* handler_factory,
99  size_t num_listeners);
100 
102 
103 #endif /* GRPC_CORE_LIB_IOMGR_UDP_SERVER_H */
Definition: udp_server.h:63
virtual void DestroyUdpHandler(GrpcUdpHandler *handler)=0
virtual ~GrpcUdpHandlerFactory()
Definition: udp_server.h:65
virtual GrpcUdpHandler * CreateUdpHandler(grpc_fd *emfd, void *user_data)=0
Definition: udp_server.h:41
virtual void OnCanWrite(void *user_data, grpc_closure *notify_on_write_closure)=0
virtual bool Read()=0
virtual void OnFdAboutToOrphan(grpc_closure *orphan_fd_closure, void *user_data)=0
virtual ~GrpcUdpHandler()
Definition: udp_server.h:44
GrpcUdpHandler(grpc_fd *, void *)
Definition: udp_server.h:43
struct grpc_fd grpc_fd
Definition: ev_posix.h:44
An array of arguments that can be passed around.
Definition: grpc_types.h:132
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
Definition: resolve_address.h:44
Definition: server.h:450
int grpc_udp_server_add_port(grpc_udp_server *s, grpc_resolved_address *addr, int rcv_buf_size, int snd_buf_size, GrpcUdpHandlerFactory *handler_factory, size_t num_listeners)
void grpc_udp_server_destroy(grpc_udp_server *server, grpc_closure *on_done)
int grpc_udp_server_get_fd(grpc_udp_server *s, unsigned port_index)
grpc_udp_server * grpc_udp_server_create(const grpc_channel_args *args)
void grpc_udp_server_start(grpc_udp_server *udp_server, const std::vector< grpc_pollset * > *pollsets, void *user_data)
struct grpc_udp_server grpc_udp_server
Definition: udp_server.h:35